2016-05-22 17:16:34 +02:00
< ? php
2020-05-05 20:13:38 +07:00
namespace MainWP\Child ;
2016-05-22 17:16:34 +02:00
class MainWP_Custom_Post_Type {
2020-03-26 19:45:07 +00:00
public static $instance = null ;
2016-05-22 17:16:34 +02:00
public static $information = array ();
2020-03-26 19:45:07 +00:00
public $plugin_translate = 'mainwp-child' ;
2016-05-22 17:16:34 +02:00
2020-04-21 18:48:24 +02:00
public static function Instance () {
if ( null == self :: $instance ) {
2020-03-26 14:05:04 +00:00
self :: $instance = new MainWP_Custom_Post_Type ();
2016-05-22 17:16:34 +02:00
}
2020-03-26 14:05:04 +00:00
return self :: $instance ;
2016-05-22 17:16:34 +02:00
}
public function action () {
error_reporting ( 0 );
function mainwp_custom_post_type_handle_fatal_error () {
$error = error_get_last ();
if ( isset ( $error [ 'type' ] ) && E_ERROR === $error [ 'type' ] && isset ( $error [ 'message' ] ) ) {
2020-01-17 19:42:57 +07:00
$data = array ( 'error' => 'MainWPChild fatal error : ' . $error [ 'message' ] . ' Line: ' . $error [ 'line' ] . ' File: ' . $error [ 'file' ] );
2016-05-22 17:16:34 +02:00
} else {
2020-03-26 14:05:04 +00:00
$data = self :: $information ;
2016-05-22 17:16:34 +02:00
}
2020-03-26 14:05:04 +00:00
2020-01-17 19:42:57 +07:00
if ( isset ( $_REQUEST [ 'json_result' ] ) && $_REQUEST [ 'json_result' ] ) {
$data = json_encode ( $data );
} else {
2020-04-21 18:48:24 +02:00
$data = serialize ( $data ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.
2020-01-17 19:42:57 +07:00
}
2020-04-21 18:48:24 +02:00
die ( '<mainwp>' . base64_encode ( $data ) . '</mainwp>' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for benign reasons.
2016-05-22 17:16:34 +02:00
}
2020-03-26 14:05:04 +00:00
register_shutdown_function ( 'mainwp_custom_post_type_handle_fatal_error' );
2016-05-22 17:16:34 +02:00
$information = array ();
switch ( $_POST [ 'action' ] ) {
case 'custom_post_type_import' :
$information = $this -> _import ();
break ;
default :
$information = array ( 'error' => 'Unknown action' );
}
2020-03-26 14:05:04 +00:00
self :: $information = $information ;
2016-05-22 17:16:34 +02:00
exit ();
}
private function _import () {
add_filter ( 'http_request_host_is_external' , '__return_true' );
if ( ! isset ( $_POST [ 'data' ] ) || strlen ( $_POST [ 'data' ] ) < 2 ) {
return array ( 'error' => __ ( 'Missing data' , $this -> plugin_translate ) );
}
$data = stripslashes ( $_POST [ 'data' ] );
$data = json_decode ( $data , true );
if ( empty ( $data ) || ! is_array ( $data ) || ! isset ( $data [ 'post' ] ) ) {
return array ( 'error' => __ ( 'Cannot decode data' , $this -> plugin_translate ) );
}
2020-04-21 18:48:24 +02:00
$edit_id = ( isset ( $_POST [ 'post_id' ] ) && ! empty ( $_POST [ 'post_id' ] ) ) ? $_POST [ 'post_id' ] : 0 ;
$return = $this -> _insert_post ( $data , $edit_id , $parent_id = 0 );
if ( isset ( $return [ 'success' ] ) && 1 == $return [ 'success' ] ) {
if ( isset ( $data [ 'product_variation' ] ) && is_array ( $data [ 'product_variation' ] ) ) {
2020-03-27 15:13:11 +00:00
foreach ( $data [ 'product_variation' ] as $product_variation ) {
2020-04-21 18:48:24 +02:00
$return_variantion = $this -> _insert_post ( $product_variation , 0 , $return [ 'post_id' ] );
2020-03-27 15:13:11 +00:00
}
}
}
return $return ;
2017-05-11 21:07:42 +02:00
}
/**
* Search image inside post content and upload it to child
2020-04-21 18:48:24 +02:00
*/
2020-03-27 14:11:21 +00:00
private function _search_images ( $post_content , $upload_dir , $check_image = false ) {
2017-05-11 21:07:42 +02:00
$foundMatches = preg_match_all ( '/(<a[^>]+href=\"(.*?)\"[^>]*>)?(<img[^>\/]*src=\"((.*?)(png|gif|jpg|jpeg))\")/ix' , $post_content , $matches , PREG_SET_ORDER );
if ( $foundMatches > 0 ) {
foreach ( $matches as $match ) {
$hrefLink = $match [ 2 ];
$imgUrl = $match [ 4 ];
if ( ! isset ( $upload_dir [ 'baseurl' ] ) || ( 0 !== strripos ( $imgUrl , $upload_dir [ 'baseurl' ] ) ) ) {
continue ;
}
if ( preg_match ( '/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/' , $imgUrl , $imgMatches ) ) {
$search = $imgMatches [ 0 ];
$replace = '.' . $match [ 6 ];
$originalImgUrl = str_replace ( $search , $replace , $imgUrl );
} else {
$originalImgUrl = $imgUrl ;
}
try {
2020-03-26 14:05:04 +00:00
$downloadfile = MainWP_Helper :: uploadImage ( $originalImgUrl , array (), $check_image );
2017-05-11 21:07:42 +02:00
$localUrl = $downloadfile [ 'url' ];
$linkToReplaceWith = dirname ( $localUrl );
if ( '' !== $hrefLink ) {
$server = get_option ( 'mainwp_child_server' );
2020-04-22 18:37:14 +02:00
$serverHost = wp_parse_url ( $server , PHP_URL_HOST );
2017-05-11 21:07:42 +02:00
if ( ! empty ( $serverHost ) && strpos ( $hrefLink , $serverHost ) !== false ) {
$serverHref = 'href="' . $serverHost ;
2020-04-22 18:37:14 +02:00
$replaceServerHref = 'href="' . wp_parse_url ( $localUrl , PHP_URL_SCHEME ) . '://' . wp_parse_url ( $localUrl , PHP_URL_HOST );
2017-05-11 21:07:42 +02:00
$post_content = str_replace ( $serverHref , $replaceServerHref , $post_content );
2020-03-26 14:11:33 +00:00
} elseif ( strpos ( $hrefLink , 'http' ) !== false ) {
2017-05-11 21:07:42 +02:00
$lnkToReplace = dirname ( $hrefLink );
if ( 'http:' !== $lnkToReplace && 'https:' !== $lnkToReplace ) {
$post_content = str_replace ( $lnkToReplace , $linkToReplaceWith , $post_content );
}
}
}
$lnkToReplace = dirname ( $imgUrl );
if ( 'http:' !== $lnkToReplace && 'https:' !== $lnkToReplace ) {
$post_content = str_replace ( $lnkToReplace , $linkToReplaceWith , $post_content );
}
} catch ( Exception $e ) {
2020-04-21 18:48:24 +02:00
// ok!
2017-05-11 21:07:42 +02:00
}
}
}
return $post_content ;
}
2020-03-27 15:13:11 +00:00
private function _insert_post ( $data , $edit_id , $parent_id = 0 ) {
2016-05-22 17:16:34 +02:00
$data_insert = array ();
$data_post = $data [ 'post' ];
$data_insert [ 'post_author' ] = get_current_user_id ();
$data_keys = array (
'post_date' ,
'post_date_gmt' ,
'post_content' ,
'post_title' ,
'post_excerpt' ,
'post_status' ,
'comment_status' ,
'ping_status' ,
'post_password' ,
'post_name' ,
'to_ping' ,
'pinged' ,
'post_modified' ,
'post_modified_gmt' ,
'post_content_filtered' ,
'menu_order' ,
2020-03-26 14:05:04 +00:00
'post_type' ,
2016-05-22 17:16:34 +02:00
);
foreach ( $data_keys as $key ) {
if ( ! isset ( $data_post [ $key ] ) ) {
return array ( 'error' => _ ( 'Missing' , $this -> plugin_translate ) . ' ' . $key . ' ' . __ ( 'inside post data' , $this -> plugin_translate ) );
}
$data_insert [ $key ] = $data_post [ $key ];
}
if ( ! in_array ( $data_insert [ 'post_type' ], get_post_types ( array ( '_builtin' => false ) ) ) ) {
return array ( 'error' => __ ( 'Please install' , $this -> plugin_translate ) . ' ' . $data_insert [ 'post_type' ] . ' ' . __ ( 'on child and try again' , $this -> plugin_translate ) );
}
$is_woocomerce = false ;
2020-04-21 18:48:24 +02:00
if ( ( 'product' == $data_insert [ 'post_type' ] || 'product_variation' == $data_insert [ 'post_type' ] ) && function_exists ( 'wc_product_has_unique_sku' ) ) {
2016-05-22 17:16:34 +02:00
$is_woocomerce = true ;
}
2020-03-27 15:13:11 +00:00
$check_image_existed = false ;
2019-03-06 22:45:51 +07:00
2020-03-26 17:03:00 +00:00
if ( ! empty ( $edit_id ) ) {
2017-05-11 21:07:42 +02:00
$old_post_id = ( int ) $edit_id ;
2016-05-22 17:16:34 +02:00
$old_post = get_post ( $old_post_id , ARRAY_A );
if ( is_null ( $old_post ) ) {
return array (
'delete_connection' => 1 ,
2020-03-26 14:05:04 +00:00
'error' => __ ( 'Cannot get old post. Probably is deleted now. Please try again for create new post' , $this -> plugin_translate ),
2016-05-22 17:16:34 +02:00
);
}
if ( get_post_status ( $old_post_id ) == 'trash' ) {
return array ( 'error' => __ ( 'This post is inside trash on child website. Please try publish it manually and try again.' , $this -> plugin_translate ) );
}
2020-03-27 15:13:11 +00:00
$check_image_existed = true ;
2020-04-21 18:48:24 +02:00
$data_insert [ 'ID' ] = $old_post_id ;
2016-05-22 17:16:34 +02:00
2020-04-21 18:48:24 +02:00
// Remove all previous post meta.
// Get all unique meta_key.
2016-05-22 17:16:34 +02:00
foreach ( get_post_meta ( $old_post_id ) as $temp_meta_key => $temp_meta_val ) {
if ( ! delete_post_meta ( $old_post_id , $temp_meta_key ) ) {
return array ( 'error' => __ ( 'Cannot delete old post meta values' , $this -> plugin_translate ) );
}
}
2020-04-21 18:48:24 +02:00
// Remove all previous taxonomy.
2016-05-22 17:16:34 +02:00
wp_delete_object_term_relationships ( $old_post_id , get_object_taxonomies ( $data_insert [ 'post_type' ] ) );
}
2019-03-06 22:45:51 +07:00
2020-03-27 15:13:11 +00:00
$data_insert [ 'post_content' ] = $this -> _search_images ( $data_insert [ 'post_content' ], $data [ 'extras' ][ 'upload_dir' ], $check_image_existed );
2019-03-06 22:45:51 +07:00
2020-04-21 18:48:24 +02:00
if ( ! empty ( $parent_id ) ) {
$data_insert [ 'post_parent' ] = $parent_id ;
2020-03-27 15:13:11 +00:00
}
2016-05-22 17:16:34 +02:00
$post_id = wp_insert_post ( $data_insert , true );
if ( is_wp_error ( $post_id ) ) {
return array ( 'error' => __ ( 'Error when insert new post:' , $this -> plugin_translate ) . ' ' . $post_id -> get_error_message () );
}
2020-04-21 18:48:24 +02:00
// Insert post meta.
2016-05-22 17:16:34 +02:00
if ( ! empty ( $data [ 'postmeta' ] ) && is_array ( $data [ 'postmeta' ] ) ) {
foreach ( $data [ 'postmeta' ] as $key ) {
if ( isset ( $key [ 'meta_key' ] ) && isset ( $key [ 'meta_value' ] ) ) {
if ( $is_woocomerce ) {
2020-04-21 18:48:24 +02:00
if ( '_sku' == $key [ 'meta_key' ] ) {
2016-05-22 17:16:34 +02:00
if ( ! wc_product_has_unique_sku ( $post_id , $key [ 'meta_value' ] ) ) {
return array ( 'error' => __ ( 'Product SKU must be unique' , $this -> plugin_translate ) );
}
}
2020-04-21 18:48:24 +02:00
if ( '_product_image_gallery' == $key [ 'meta_key' ] ) {
2016-05-22 17:16:34 +02:00
$product_image_gallery = array ();
2020-04-21 18:48:24 +02:00
if ( isset ( $data [ 'extras' ][ 'woocommerce' ][ 'product_images' ] ) ) {
2016-05-22 17:16:34 +02:00
foreach ( $data [ 'extras' ][ 'woocommerce' ][ 'product_images' ] as $product_image ) {
try {
2019-03-06 22:45:51 +07:00
$upload_featured_image = MainWP_Helper :: uploadImage ( $product_image , array (), $check_image_existed );
2016-05-22 17:16:34 +02:00
if ( null !== $upload_featured_image ) {
$product_image_gallery [] = $upload_featured_image [ 'id' ];
} else {
return array ( 'error' => __ ( 'Cannot add product image' , $this -> plugin_translate ) );
}
} catch ( Exception $e ) {
continue ;
}
}
$key [ 'meta_value' ] = implode ( $product_image_gallery , ',' );
} else {
continue ;
}
}
}
2020-04-21 18:48:24 +02:00
if ( '_thumbnail_id' == $key [ 'meta_key' ] ) {
if ( isset ( $data [ 'extras' ][ 'featured_image' ] ) ) {
2016-05-22 17:16:34 +02:00
try {
2019-03-06 22:45:51 +07:00
$upload_featured_image = MainWP_Helper :: uploadImage ( $data [ 'extras' ][ 'featured_image' ], array (), $check_image_existed );
2016-05-22 17:16:34 +02:00
if ( null !== $upload_featured_image ) {
$key [ 'meta_value' ] = $upload_featured_image [ 'id' ];
} else {
return array ( 'error' => __ ( 'Cannot add featured image' , $this -> plugin_translate ) );
}
} catch ( Exception $e ) {
continue ;
}
} else {
continue ;
}
}
2020-03-27 15:13:11 +00:00
$meta_value = maybe_unserialize ( $key [ 'meta_value' ] );
2017-05-11 21:07:42 +02:00
if ( add_post_meta ( $post_id , $key [ 'meta_key' ], $meta_value ) === false ) {
2016-05-22 17:16:34 +02:00
return array ( 'error' => __ ( 'Error when adding post meta' , $this -> plugin_translate ) . ' `' . esc_html ( $key [ 'meta_key' ] ) . '`' );
}
}
}
}
2020-04-21 18:48:24 +02:00
// MainWP Categories.
2016-05-22 17:16:34 +02:00
if ( ! empty ( $data [ 'categories' ] ) && is_array ( $data [ 'categories' ] ) ) {
2020-04-21 18:48:24 +02:00
// Contains wp_create_categories.
2020-03-26 14:11:33 +00:00
include_once ABSPATH . 'wp-admin/includes/taxonomy.php' ;
2016-05-22 17:16:34 +02:00
$categories = $data [ 'categories' ];
2020-04-21 18:48:24 +02:00
if ( '0' == $data [ 'post_only_existing' ] ) {
2016-05-22 17:16:34 +02:00
$post_category = wp_create_categories ( $categories , $post_id );
} else {
$cat_ids = array ();
foreach ( $categories as $cat ) {
2020-04-21 18:48:24 +02:00
$id = category_exists ( $cat );
if ( $id ) {
2016-05-22 17:16:34 +02:00
$cat_ids [] = $id ;
}
}
if ( count ( $cat_ids ) > 0 ) {
wp_set_post_categories ( $post_id , $cat_ids );
}
}
}
2020-04-21 18:48:24 +02:00
// Insert post terms except categories.
2016-05-22 17:16:34 +02:00
if ( ! empty ( $data [ 'terms' ] ) && is_array ( $data [ 'terms' ] ) ) {
foreach ( $data [ 'terms' ] as $key ) {
if ( ! taxonomy_exists ( $key [ 'taxonomy' ] ) ) {
return array ( 'error' => __ ( 'Missing taxonomy' , $this -> plugin_translate ) . ' `' . esc_html ( $key [ 'taxonomy' ] ) . '`' );
}
2020-04-21 18:48:24 +02:00
$term = wp_insert_term (
$key [ 'name' ],
$key [ 'taxonomy' ],
array (
'description' => $key [ 'description' ],
'slug' => $key [ 'slug' ],
)
);
2016-05-22 17:16:34 +02:00
$term_taxonomy_id = 0 ;
if ( is_wp_error ( $term ) ) {
if ( isset ( $term -> error_data [ 'term_exists' ] ) ) {
$term_taxonomy_id = ( int ) $term -> error_data [ 'term_exists' ];
}
} else {
if ( isset ( $term [ 'term_taxonomy_id' ] ) ) {
$term_taxonomy_id = ( int ) $term [ 'term_taxonomy_id' ];
}
}
if ( $term_taxonomy_id > 0 ) {
$term_taxonomy_ids = wp_set_object_terms ( $post_id , $term_taxonomy_id , $key [ 'taxonomy' ], true );
if ( is_wp_error ( $term_taxonomy_ids ) ) {
return array ( 'error' => __ ( 'Error when adding taxonomy to post' , $this -> plugin_translate ) );
}
}
}
}
2020-03-26 15:29:54 +00:00
return array (
'success' => 1 ,
'post_id' => $post_id ,
);
2016-05-22 17:16:34 +02:00
}
2017-05-11 21:07:42 +02:00
}