class-fl-builder-export.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Custom export handling.
  4. *
  5. * @since 1.8
  6. */
  7. final class FLBuilderExport {
  8. /**
  9. * @since 1.8
  10. * @return void
  11. */
  12. static public function init() {
  13. add_action( 'admin_enqueue_scripts', 'FLBuilderExport::enqueue_scripts' );
  14. add_action( 'export_filters', 'FLBuilderExport::filters' );
  15. add_action( 'wp_ajax_fl_builder_export_templates_data', 'FLBuilderExport::templates_data' );
  16. add_action( 'export_wp', 'FLBuilderExport::export' );
  17. }
  18. /**
  19. * Enqueues the export scripts and styles.
  20. *
  21. * @since 1.8
  22. * @return void
  23. */
  24. static public function enqueue_scripts() {
  25. global $pagenow;
  26. if ( 'export.php' != $pagenow ) {
  27. return;
  28. }
  29. wp_enqueue_style( 'fl-builder-export', FL_BUILDER_URL . 'css/fl-builder-export.css', array(), FL_BUILDER_VERSION );
  30. wp_enqueue_script( 'fl-builder-export', FL_BUILDER_URL . 'js/fl-builder-export.js', array(), FL_BUILDER_VERSION, true );
  31. wp_localize_script( 'fl-builder-export', 'fl_builder_export_nonce', wp_create_nonce( 'fl_builder_export_nonce' ) );
  32. }
  33. /**
  34. * Renders the export filters markup.
  35. *
  36. * @since 1.8
  37. * @return void
  38. */
  39. static public function filters() {
  40. include FL_BUILDER_DIR . 'includes/export-filters.php';
  41. }
  42. /**
  43. * Called via AJAX and returns the data used for selecting
  44. * templates for export.
  45. *
  46. * @since 1.8
  47. * @return void
  48. */
  49. static public function templates_data() {
  50. check_admin_referer( 'fl_builder_export_nonce' );
  51. $type = isset( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : 'fl-builder-template';
  52. $data = array();
  53. $query = new WP_Query( array(
  54. 'post_type' => $type,
  55. 'orderby' => 'title',
  56. 'order' => 'ASC',
  57. 'posts_per_page' => '-1',
  58. ) );
  59. foreach ( $query->posts as $post ) {
  60. $data[] = array(
  61. 'id' => $post->ID,
  62. 'title' => $post->post_title,
  63. );
  64. }
  65. echo json_encode( $data );
  66. die();
  67. }
  68. /**
  69. * Download the export file.
  70. *
  71. * @since 1.8
  72. * @param array $args
  73. * @return void
  74. */
  75. static public function export( $args ) {
  76. if ( ! current_user_can( 'export' ) ) {
  77. return;
  78. }
  79. if ( ! in_array( $args['content'], array( 'fl-builder-template', 'fl-theme-layout' ) ) ) {
  80. return;
  81. }
  82. if ( ! isset( $_REQUEST['fl-builder-template-export-select'] ) ) {
  83. return;
  84. }
  85. if ( 'all' == $_REQUEST['fl-builder-template-export-select'] ) {
  86. return;
  87. }
  88. if ( ! is_array( $_REQUEST['fl-builder-export-template'] ) ) {
  89. return;
  90. }
  91. require_once FL_BUILDER_DIR . 'includes/export.php';
  92. fl_export_wp( $_REQUEST['fl-builder-export-template'] );
  93. die();
  94. }
  95. }
  96. FLBuilderExport::init();