class-fl-builder-template-data-exporter.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /**
  3. * Template data exporter for the builder.
  4. *
  5. * @since 1.8
  6. */
  7. final class FLBuilderTemplateDataExporter {
  8. /**
  9. * Initializes the exporter.
  10. *
  11. * @since 1.8
  12. * @return void
  13. */
  14. static public function init() {
  15. add_action( 'plugins_loaded', __CLASS__ . '::init_hooks' );
  16. add_action( 'after_setup_theme', __CLASS__ . '::register_user_access_setting' );
  17. }
  18. /**
  19. * Init actions and filters.
  20. *
  21. * @since 1.8
  22. * @return void
  23. */
  24. static public function init_hooks() {
  25. if ( ! is_admin() ) {
  26. return;
  27. }
  28. add_action( 'admin_menu', __CLASS__ . '::menu' );
  29. if ( isset( $_REQUEST['page'] ) && 'fl-builder-template-data-exporter' == $_REQUEST['page'] ) {
  30. add_action( 'admin_enqueue_scripts', __CLASS__ . '::styles_scripts' );
  31. add_action( 'init', __CLASS__ . '::export' );
  32. }
  33. }
  34. /**
  35. * Registers the user access setting.
  36. *
  37. * @since 1.8
  38. * @return void
  39. */
  40. static public function register_user_access_setting() {
  41. FLBuilderUserAccess::register_setting( 'template_data_exporter', array(
  42. 'default' => false,
  43. 'group' => __( 'Admin', 'vamtam-elements-b' ),
  44. 'label' => __( 'Template Data Exporter', 'vamtam-elements-b' ),
  45. 'description' => __( 'The selected roles will be able to access the template data exporter under Tools > Template Exporter.', 'vamtam-elements-b' ),
  46. 'order' => '120',
  47. ) );
  48. }
  49. /**
  50. * Checks to see whether the exporter is enabled or not.
  51. *
  52. * @since 1.8
  53. * @return void
  54. */
  55. static public function is_enabled() {
  56. return FLBuilderUserAccess::current_user_can( 'template_data_exporter' );
  57. }
  58. /**
  59. * Enqueues scripts and styles for the exporter.
  60. *
  61. * @since 1.8
  62. * @return void
  63. */
  64. static public function styles_scripts() {
  65. wp_enqueue_script(
  66. 'fl-builder-template-data-exporter',
  67. VAMTAMEL_B_TEMPLATE_DATA_EXPORTER_URL . 'js/fl-builder-template-data-exporter.js',
  68. array(),
  69. FL_BUILDER_VERSION
  70. );
  71. }
  72. /**
  73. * Renders the admin settings menu.
  74. *
  75. * @since 1.8
  76. * @return void
  77. */
  78. static public function menu() {
  79. if ( self::is_enabled() && current_user_can( 'delete_users' ) ) {
  80. $title = __( 'Template Exporter', 'vamtam-elements-b' );
  81. $cap = 'delete_users';
  82. $slug = 'fl-builder-template-data-exporter';
  83. $func = __CLASS__ . '::render';
  84. add_submenu_page( 'tools.php', $title, $title, $cap, $slug, $func );
  85. }
  86. }
  87. /**
  88. * Renders the exporter UI.
  89. *
  90. * @since 1.8
  91. * @return void
  92. */
  93. static public function render() {
  94. $theme = self::get_ui_data( 'theme' );
  95. $layouts = self::get_ui_data();
  96. $rows = self::get_ui_data( 'row' );
  97. $modules = self::get_ui_data( 'module' );
  98. include VAMTAMEL_B_TEMPLATE_DATA_EXPORTER_DIR . 'includes/template-data-exporter.php';
  99. }
  100. /**
  101. * Run the exporter.
  102. *
  103. * @since 1.8
  104. * @return void
  105. */
  106. static public function export() {
  107. if ( ! current_user_can( 'delete_users' ) ) {
  108. return;
  109. }
  110. if ( ! isset( $_POST['fl-builder-template-data-exporter-nonce'] ) ) {
  111. return;
  112. }
  113. if ( ! wp_verify_nonce( $_POST['fl-builder-template-data-exporter-nonce'], 'fl-builder-template-data-exporter' ) ) {
  114. return;
  115. }
  116. $templates = array();
  117. if ( isset( $_POST['fl-builder-export-theme'] ) && is_array( $_POST['fl-builder-export-theme'] ) ) {
  118. $templates = self::get_theme_layout_export_data( $templates );
  119. }
  120. if ( isset( $_POST['fl-builder-export-layout'] ) && is_array( $_POST['fl-builder-export-layout'] ) ) {
  121. $templates['layout'] = self::get_template_export_data( $_POST['fl-builder-export-layout'] );
  122. }
  123. if ( isset( $_POST['fl-builder-export-row'] ) && is_array( $_POST['fl-builder-export-row'] ) ) {
  124. $templates['row'] = self::get_template_export_data( $_POST['fl-builder-export-row'] );
  125. }
  126. if ( isset( $_POST['fl-builder-export-module'] ) && is_array( $_POST['fl-builder-export-module'] ) ) {
  127. $templates['module'] = self::get_template_export_data( $_POST['fl-builder-export-module'] );
  128. }
  129. header( 'X-Robots-Tag: noindex, nofollow', true );
  130. header( 'Content-Type: application/octet-stream' );
  131. header( 'Content-Description: File Transfer' );
  132. header( 'Content-Disposition: attachment; filename="templates.dat";' );
  133. header( 'Content-Transfer-Encoding: binary' );
  134. echo serialize( $templates );
  135. die();
  136. }
  137. /**
  138. * Returns user template data of a certain type for the UI.
  139. *
  140. * @since 1.8
  141. * @access private
  142. * @param string $type
  143. * @return array
  144. */
  145. static private function get_ui_data( $type = 'layout' ) {
  146. $templates = array();
  147. if ( 'theme' == $type ) {
  148. $args = array(
  149. 'post_type' => 'fl-theme-layout',
  150. 'orderby' => 'title',
  151. 'order' => 'ASC',
  152. 'posts_per_page' => '-1',
  153. );
  154. } else {
  155. $args = array(
  156. 'post_type' => 'fl-builder-template',
  157. 'orderby' => 'title',
  158. 'order' => 'ASC',
  159. 'posts_per_page' => '-1',
  160. 'tax_query' => array(
  161. array(
  162. 'taxonomy' => 'fl-builder-template-type',
  163. 'field' => 'slug',
  164. 'terms' => $type,
  165. ),
  166. ),
  167. );
  168. }
  169. foreach ( get_posts( $args ) as $post ) {
  170. $templates[] = array(
  171. 'id' => $post->ID,
  172. 'name' => $post->post_title,
  173. );
  174. }
  175. return $templates;
  176. }
  177. /**
  178. * Returns theme layout export data for the specified post ids.
  179. *
  180. * @since 1.10
  181. * @access private
  182. * @param array $templates
  183. * @return array
  184. */
  185. static private function get_theme_layout_export_data( $templates ) {
  186. $posts = get_posts( array(
  187. 'post_type' => 'fl-theme-layout',
  188. 'orderby' => 'menu_order title',
  189. 'order' => 'ASC',
  190. 'posts_per_page' => '-1',
  191. 'post__in' => array_map( 'sanitize_text_field', $_POST['fl-builder-export-theme'] ),
  192. ) );
  193. // Get all theme layouts.
  194. $data = self::get_export_data( $posts );
  195. // Store in the templates array by type.
  196. foreach ( $data as $template ) {
  197. if ( ! isset( $templates[ $template->type ] ) ) {
  198. $templates[ $template->type ] = array();
  199. }
  200. $templates[ $template->type ][] = $template;
  201. }
  202. // Reset the index for each template.
  203. foreach ( $templates as $data ) {
  204. foreach ( $data as $index => $template ) {
  205. $template->index = $index;
  206. }
  207. }
  208. return $templates;
  209. }
  210. /**
  211. * Returns template export data for the specified post ids.
  212. *
  213. * @since 1.10
  214. * @access private
  215. * @param array $post_ids
  216. * @return array
  217. */
  218. static private function get_template_export_data( $post_ids = array() ) {
  219. if ( empty( $post_ids ) ) {
  220. return array();
  221. }
  222. $posts = get_posts( array(
  223. 'post_type' => 'fl-builder-template',
  224. 'orderby' => 'menu_order title',
  225. 'order' => 'ASC',
  226. 'posts_per_page' => '-1',
  227. 'post__in' => $post_ids,
  228. ) );
  229. return self::get_export_data( $posts );
  230. }
  231. /**
  232. * Returns export data for the specified posts.
  233. *
  234. * @since 1.8
  235. * @access private
  236. * @param array $posts
  237. * @return array
  238. */
  239. static private function get_export_data( $posts ) {
  240. if ( empty( $posts ) ) {
  241. return array();
  242. }
  243. $templates = array();
  244. $index = 0;
  245. foreach ( $posts as $post ) {
  246. // Build the template object.
  247. $template = new StdClass();
  248. $template->name = $post->post_title;
  249. $template->slug = $post->post_name;
  250. $template->index = $index++;
  251. $template->global = false;
  252. $template->image = '';
  253. $template->categories = array();
  254. $template->nodes = FLBuilderModel::generate_new_node_ids( FLBuilderModel::get_layout_data( 'published', $post->ID ) );
  255. $template->settings = FLBuilderModel::get_layout_settings( 'published', $post->ID );
  256. // Get the template type.
  257. if ( 'fl-theme-layout' == $post->post_type ) {
  258. $template->type = get_post_meta( $post->ID, '_fl_theme_layout_type', true );
  259. } else {
  260. $template->type = FLBuilderModel::get_user_template_type( $post->ID );
  261. }
  262. // Get the template categories.
  263. $categories = wp_get_post_terms( $post->ID, 'fl-builder-template-category' );
  264. if ( 0 === count( $categories ) || is_wp_error( $categories ) ) {
  265. $template->categories['uncategorized'] = 'Uncategorized';
  266. } else {
  267. foreach ( $categories as $category ) {
  268. $template->categories[ $category->slug ] = $category->name;
  269. }
  270. }
  271. // Get the template thumbnail.
  272. if ( has_post_thumbnail( $post->ID ) ) {
  273. $attachment_image_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium_large' );
  274. $template->image = apply_filters( 'fl_builder_exporter_template_thumb_src', $attachment_image_src[0], $post, $template );
  275. }
  276. // Add the template to the templates array.
  277. $templates[] = apply_filters( 'fl_builder_exporter_template', $template, $post );
  278. }
  279. return $templates;
  280. }
  281. }
  282. FLBuilderTemplateDataExporter::init();