| 1 |
<?php |
| 2 |
namespace Easyel\EasyElements\Extensions\SettingExport; |
| 3 |
/** |
| 4 |
* Easy Export - Export Elementor Pages as JSON |
| 5 |
* |
| 6 |
* @package EasyElements |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
final class EasyelExport { |
| 14 |
|
| 15 |
private static $instance = null; |
| 16 |
|
| 17 |
private function __construct() { |
| 18 |
|
| 19 |
if ( function_exists( 'easyel_element_is_enabled' ) && ! easyel_element_is_enabled( 'enable_postpage_export' ) ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
add_action( 'admin_bar_menu', [ $this, 'easyel_add_export_button' ], 100 ); |
| 24 |
|
| 25 |
add_action( 'admin_action_easyel_export_elementor', [ $this, 'easyel_export_elementor_json' ] ); |
| 26 |
|
| 27 |
add_filter( 'post_row_actions', [ $this, 'easyel_add_export_link'], 10, 2 ); |
| 28 |
add_filter( 'page_row_actions', [ $this,'easyel_add_export_link'], 10, 2 ); |
| 29 |
} |
| 30 |
|
| 31 |
public static function get_instance() : self { |
| 32 |
if ( null === self::$instance ) { |
| 33 |
self::$instance = new self(); |
| 34 |
} |
| 35 |
return self::$instance; |
| 36 |
} |
| 37 |
|
| 38 |
function easyel_add_export_link( $actions, $post ) { |
| 39 |
|
| 40 |
if ( ! did_action( 'elementor/loaded' ) ) { |
| 41 |
return $actions; |
| 42 |
} |
| 43 |
|
| 44 |
if ( 'elementor_library' === $post->post_type ) { |
| 45 |
return $actions; |
| 46 |
} |
| 47 |
|
| 48 |
$elementor_data = get_post_meta( $post->ID, '_elementor_data', true ); |
| 49 |
if ( empty( $elementor_data ) ) { |
| 50 |
return $actions; |
| 51 |
} |
| 52 |
|
| 53 |
$export_url = wp_nonce_url( |
| 54 |
add_query_arg( |
| 55 |
[ |
| 56 |
'action' => 'easyel_export_elementor', |
| 57 |
'post' => $post->ID, |
| 58 |
], |
| 59 |
admin_url( 'admin.php' ) |
| 60 |
), |
| 61 |
'easyel_export_nonce_' . $post->ID |
| 62 |
); |
| 63 |
|
| 64 |
$actions['easy_export'] = '<a href="' . esc_url( $export_url ) . '">' . __( 'Easy Export', 'easy-elements' ) . '</a>'; |
| 65 |
|
| 66 |
return $actions; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Add Easy Export button on the admin bar if Elementor data exists |
| 71 |
*/ |
| 72 |
public function easyel_add_export_button( $admin_bar ) { |
| 73 |
global $post; |
| 74 |
|
| 75 |
if ( ! is_admin() || ! isset( $post->ID ) ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
if ( ! did_action( 'elementor/loaded' ) ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$elementor_data = get_post_meta( $post->ID, '_elementor_data', true ); |
| 84 |
if ( empty( $elementor_data ) ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
$export_url = wp_nonce_url( |
| 89 |
add_query_arg( |
| 90 |
[ |
| 91 |
'action' => 'easyel_export_elementor', |
| 92 |
'post' => $post->ID, |
| 93 |
], |
| 94 |
admin_url( 'admin.php' ) |
| 95 |
), |
| 96 |
'easyel_export_nonce_' . $post->ID |
| 97 |
); |
| 98 |
|
| 99 |
$admin_bar->add_node( [ |
| 100 |
'id' => 'easyel-export', |
| 101 |
'title' => __( 'Easy Export', 'easy-elements' ), |
| 102 |
'href' => esc_url( $export_url ), |
| 103 |
'meta' => [ |
| 104 |
'title' => __( 'Export Elementor JSON', 'easy-elements' ), |
| 105 |
], |
| 106 |
] ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Handle Elementor JSON export |
| 111 |
*/ |
| 112 |
public function easyel_export_elementor_json() { |
| 113 |
$post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; |
| 114 |
$nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : ''; |
| 115 |
|
| 116 |
if ( ! $post_id || ! wp_verify_nonce( $nonce, 'easyel_export_nonce_' . $post_id ) ) { |
| 117 |
wp_die( esc_html__( 'Invalid request.', 'easy-elements' ) ); |
| 118 |
} |
| 119 |
|
| 120 |
$elementor_data = get_post_meta( $post_id, '_elementor_data', true ); |
| 121 |
|
| 122 |
if ( empty( $elementor_data ) ) { |
| 123 |
wp_die( esc_html__( 'No Elementor data found for this post.', 'easy-elements' ) ); |
| 124 |
} |
| 125 |
|
| 126 |
$elements = $elementor_data; |
| 127 |
|
| 128 |
$elements = json_decode( $elementor_data, true ); |
| 129 |
if ( ! is_array( $elements ) ) { |
| 130 |
wp_die( 'Invalid Elementor data format.' ); |
| 131 |
} |
| 132 |
|
| 133 |
$export_array = [ |
| 134 |
'content' => $elements, |
| 135 |
'type' => 'container', |
| 136 |
'title' => get_the_title( $post_id ), |
| 137 |
'version' => '1.0', |
| 138 |
'page_settings' => [], |
| 139 |
]; |
| 140 |
|
| 141 |
$json_data = wp_json_encode( $export_array, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ); |
| 142 |
|
| 143 |
$filename = sanitize_title( get_the_title( $post_id ) ) . '-easy-export.json'; |
| 144 |
|
| 145 |
header( 'Content-Type: application/json; charset=utf-8' ); |
| 146 |
header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); |
| 147 |
|
| 148 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 149 |
echo $json_data; |
| 150 |
exit; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
} |