| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly. |
| 4 |
} |
| 5 |
/** |
| 6 |
* Custom import export. |
| 7 |
* |
| 8 |
* @link https://shapedplugin.com |
| 9 |
* @since 2.0.0 |
| 10 |
* |
| 11 |
* @package Smart_Post_Show. |
| 12 |
* @subpackage Smart_Post_Show/includes. |
| 13 |
*/ |
| 14 |
|
| 15 |
/** |
| 16 |
* Custom import export. |
| 17 |
*/ |
| 18 |
class Smart_Post_Show_Import_Export { |
| 19 |
|
| 20 |
/** |
| 21 |
* Export |
| 22 |
* |
| 23 |
* @param mixed $shortcode_ids Export smart-post-show shortcode ids. |
| 24 |
* @return array |
| 25 |
*/ |
| 26 |
public function export( $shortcode_ids ) { |
| 27 |
$export = array(); |
| 28 |
if ( ! empty( $shortcode_ids ) ) { |
| 29 |
|
| 30 |
$post_in = 'all_shortcodes' === $shortcode_ids ? '' : $shortcode_ids; |
| 31 |
$args = array( |
| 32 |
'post_type' => 'sp_post_carousel', |
| 33 |
'post_status' => array( 'inherit', 'publish' ), |
| 34 |
'orderby' => 'modified', |
| 35 |
'suppress_filters' => 1, // wpml, ignore language filter. |
| 36 |
'posts_per_page' => -1, |
| 37 |
'post__in' => $post_in, |
| 38 |
); |
| 39 |
$shortcodes = get_posts( $args ); |
| 40 |
if ( ! empty( $shortcodes ) ) { |
| 41 |
foreach ( $shortcodes as $shortcode ) { |
| 42 |
$shortcode_export = array( |
| 43 |
'title' => $shortcode->post_title, |
| 44 |
'original_id' => $shortcode->ID, |
| 45 |
'meta' => array(), |
| 46 |
); |
| 47 |
foreach ( get_post_meta( $shortcode->ID ) as $metakey => $value ) { |
| 48 |
$shortcode_export['meta'][ $metakey ] = $value[0]; |
| 49 |
} |
| 50 |
$export['shortcode'][] = $shortcode_export; |
| 51 |
|
| 52 |
unset( $shortcode_export ); |
| 53 |
} |
| 54 |
$export['metadata'] = array( |
| 55 |
'version' => SP_PC_VERSION, |
| 56 |
'date' => gmdate( 'Y/m/d' ), |
| 57 |
); |
| 58 |
} |
| 59 |
return $export; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Export smart_post_show by ajax. |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function export_shortcodes() { |
| 69 |
$nonce = ( ! empty( $_POST['nonce'] ) ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; // phpcs:ignore |
| 70 |
if ( ! wp_verify_nonce( $nonce, 'spf_options_nonce' ) ) { |
| 71 |
die(); |
| 72 |
} |
| 73 |
$shortcode_ids = isset( $_POST['pcp_ids'] ) ? wp_unslash( $_POST['pcp_ids'] ) : ''; // phpcs:ignore |
| 74 |
|
| 75 |
if ( is_array( $shortcode_ids ) ) { |
| 76 |
$shortcode_ids = array_map( 'sanitize_text_field', $shortcode_ids ); |
| 77 |
} else { |
| 78 |
$shortcode_ids = sanitize_text_field( $shortcode_ids ); |
| 79 |
} |
| 80 |
|
| 81 |
$export = $this->export( $shortcode_ids ); |
| 82 |
|
| 83 |
if ( is_wp_error( $export ) ) { |
| 84 |
wp_send_json_error( |
| 85 |
array( |
| 86 |
'message' => $export->get_error_message(), |
| 87 |
), |
| 88 |
400 |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { |
| 93 |
// @codingStandardsIgnoreLine |
| 94 |
echo wp_json_encode($export, JSON_PRETTY_PRINT); |
| 95 |
die; |
| 96 |
} |
| 97 |
|
| 98 |
wp_send_json( $export, 200 ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Import shortcode. |
| 103 |
* |
| 104 |
* @param mixed $shortcodes Import shortcode array. |
| 105 |
* |
| 106 |
* @throws \Exception . |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public function import( $shortcodes ) { |
| 110 |
$errors = array(); |
| 111 |
foreach ( $shortcodes as $index => $shortcode ) { |
| 112 |
$errors[ $index ] = array(); |
| 113 |
$new_shortcode_id = 0; |
| 114 |
try { |
| 115 |
$new_shortcode_id = wp_insert_post( |
| 116 |
array( |
| 117 |
'post_title' => isset( $shortcode['title'] ) ? $shortcode['title'] : '', |
| 118 |
'post_status' => 'publish', |
| 119 |
'post_type' => 'sp_post_carousel', |
| 120 |
), |
| 121 |
true |
| 122 |
); |
| 123 |
|
| 124 |
if ( is_wp_error( $new_shortcode_id ) ) { |
| 125 |
throw new Exception( $new_shortcode_id->get_error_message() ); |
| 126 |
} |
| 127 |
|
| 128 |
if ( isset( $shortcode['meta'] ) && is_array( $shortcode['meta'] ) ) { |
| 129 |
foreach ( $shortcode['meta'] as $key => $value ) { |
| 130 |
update_post_meta( |
| 131 |
$new_shortcode_id, |
| 132 |
$key, |
| 133 |
maybe_unserialize( str_replace( '{#ID#}', $new_shortcode_id, $value ) ) |
| 134 |
); |
| 135 |
} |
| 136 |
} |
| 137 |
} catch ( Exception $e ) { |
| 138 |
array_push( $errors[ $index ], $e->getMessage() ); |
| 139 |
|
| 140 |
// If there was a failure somewhere, clean up. |
| 141 |
wp_trash_post( $new_shortcode_id ); |
| 142 |
} |
| 143 |
|
| 144 |
// If no errors, remove the index. |
| 145 |
if ( ! count( $errors[ $index ] ) ) { |
| 146 |
unset( $errors[ $index ] ); |
| 147 |
} |
| 148 |
|
| 149 |
// External modules manipulate data here. |
| 150 |
do_action( 'sp_smart_post_show_shortcode_imported', $new_shortcode_id ); |
| 151 |
} |
| 152 |
|
| 153 |
$errors = reset( $errors ); |
| 154 |
return isset( $errors[0] ) ? new WP_Error( 'import_sp_smart_post_show_error', $errors[0] ) : ''; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Import smart_post_show by ajax. |
| 159 |
* |
| 160 |
* @return void |
| 161 |
*/ |
| 162 |
public function import_shortcodes() { |
| 163 |
$nonce = ( ! empty( $_POST['nonce'] ) ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; // phpcs:ignore |
| 164 |
if ( ! wp_verify_nonce( $nonce, 'spf_options_nonce' ) ) { |
| 165 |
die(); |
| 166 |
} |
| 167 |
$data = isset( $_POST['shortcode'] ) ? wp_unslash( $_POST['shortcode'] ) : '';// phpcs:ignore |
| 168 |
$data = json_decode( ( $data ) ); |
| 169 |
$data = json_decode( $data, true ); |
| 170 |
$shortcodes = $data['shortcode']; |
| 171 |
if ( ! $data ) { |
| 172 |
wp_send_json_error( |
| 173 |
array( |
| 174 |
'message' => __( 'Nothing to import.', 'smart-post-show' ), |
| 175 |
), |
| 176 |
400 |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
$status = $this->import( $shortcodes ); |
| 181 |
|
| 182 |
if ( is_wp_error( $status ) ) { |
| 183 |
wp_send_json_error( |
| 184 |
array( |
| 185 |
'message' => $status->get_error_message(), |
| 186 |
), |
| 187 |
400 |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
wp_send_json_success( $status, 200 ); |
| 192 |
} |
| 193 |
} |
| 194 |
|