2016-05-22 17:16:34 +02:00
< ? php
2020-05-29 02:48:54 -04:00
/**
* MainWP Custom Post Type
*
* This file handles all custom post types .
*/
2020-05-05 20:13:38 +07:00
namespace MainWP\Child ;
2020-05-29 02:48:54 -04:00
/**
* Class MainWP_Custom_Post_Type
2020-05-29 06:59:34 +00:00
*
2020-05-29 02:48:54 -04:00
* @ package MainWP\Child
*/
2016-05-22 17:16:34 +02:00
class MainWP_Custom_Post_Type {
2020-05-29 06:59:34 +00:00
/**
* @ static
* @ var null Holds the Public static instance of MainWP_Custom_Post_Type .
*/
public static $instance = null ;
2020-05-29 02:48:54 -04:00
2020-05-29 06:59:34 +00:00
/** @var array Response array. */
public static $information = array ();
2020-05-29 02:48:54 -04:00
2020-05-29 06:59:34 +00:00
/** @var string Plugin slug. */
public $plugin_translate = 'mainwp-child' ;
2020-05-29 02:48:54 -04:00
2020-05-29 06:59:34 +00:00
/**
* Create a public static instance of MainWP_Custom_Post_Type .
*
* @ return MainWP_Custom_Post_Type | null
*/
public static function instance () {
2020-04-21 18:48:24 +02:00
if ( null == self :: $instance ) {
2020-05-07 01:03:56 +07:00
self :: $instance = new self ();
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
}
2020-05-29 06:59:34 +00:00
/**
* Custom post type action .
*/
public function action () {
2020-05-29 02:54:19 -04:00
2020-05-29 06:59:34 +00:00
/**
* Method mainwp_custom_post_type_handle_fatal_error ()
*
* Custom post type fatal error handler .
*/
2016-05-22 17:16:34 +02:00
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' ] ) {
2020-05-13 18:48:37 +07:00
$data = wp_json_encode ( $data );
2020-01-17 19:42:57 +07:00
} else {
2020-05-07 23:22:05 +07:00
$data = serialize ( $data ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions
2020-01-17 19:42:57 +07:00
}
2020-05-29 02:48:54 -04:00
die ( '<mainwp>' . base64_encode ( $data ) . '</mainwp>' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for http encode compatibility.
2016-05-22 17:16:34 +02:00
}
2020-05-30 01:13:29 +07:00
register_shutdown_function ( 'MainWP\Child\mainwp_custom_post_type_handle_fatal_error' );
2016-05-22 17:16:34 +02:00
2020-05-29 02:48:54 -04:00
/** @var $information @deprecated Unused local variable 'information'. The value of the variable is overwritten immediately. */
2016-05-22 17:16:34 +02:00
$information = array ();
switch ( $_POST [ 'action' ] ) {
case 'custom_post_type_import' :
2020-05-07 19:34:36 +07:00
$information = $this -> import_custom_post ();
2016-05-22 17:16:34 +02:00
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 ();
}
2020-05-29 06:59:34 +00:00
/**
* Import custom post type .
*
* @ return array | string [] $return Response array , or error message on failure .
*/
private function import_custom_post () {
2020-05-29 02:48:54 -04:00
2016-05-22 17:16:34 +02:00
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 ;
2020-05-07 19:34:36 +07:00
$return = $this -> insert_post ( $data , $edit_id , $parent_id = 0 );
2020-04-21 18:48:24 +02:00
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-05-07 19:34:36 +07: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
}
2020-05-29 06:59:34 +00:00
/**
* Search for images inside post content and upload it to Child Site .
*
* @ param string $post_content Post content to search .
* @ param string $upload_dir Upload directory .
* @ param bool $check_image Check if file exists . Default : false .
* @ return string | string [] Error message or post content string .
*/
2020-05-07 19:34:36 +07: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-05-15 01:04:08 +07:00
$downloadfile = MainWP_Utility :: upload_image ( $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 );
}
2020-05-07 01:03:56 +07:00
} catch ( \Exception $e ) {
2020-04-21 18:48:24 +02:00
// ok!
2017-05-11 21:07:42 +02:00
}
}
}
return $post_content ;
}
2020-05-29 06:59:34 +00:00
/**
* Insert data into published post .
*
* @ param string $data Data to insert .
* @ param int $edit_id Post ID to edit .
* @ param int $parent_id Post parent ID .
*
* @ return array | bool | string [] Response array , true | false , Error message .
*/
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-05-07 19:34:36 +07: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' ] ) ) {
2020-05-19 20:08:55 +07:00
$ret = $this -> insert_postmeta ( $post_id , $data , $check_image_existed , $is_woocomerce );
2020-05-19 13:24:43 +00:00
if ( true !== $ret ) {
return $ret ;
}
2016-05-22 17:16:34 +02:00
}
2020-05-19 13:24:43 +00:00
2020-05-19 20:08:55 +07:00
$ret = $this -> insert_custom_data ( $post_id , $data );
2020-05-19 13:24:43 +00:00
if ( true !== $ret ) {
2020-05-19 20:08:55 +07:00
return $ret ;
2020-05-19 13:24:43 +00:00
}
2016-05-22 17:16:34 +02:00
2020-05-19 20:08:55 +07:00
return array (
'success' => 1 ,
'post_id' => $post_id ,
);
}
2020-05-19 13:24:43 +00:00
2020-05-29 06:59:34 +00:00
/**
* Insert custom post data .
*
* @ param int $post_id Post ID to update .
* @ param string $data Custom data to add .
* @ return array | bool | string [] Response array , true | false , Error message .
*/
private function insert_custom_data ( $post_id , $data ) {
2020-05-19 13:24:43 +00:00
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-05-19 20:08:55 +07:00
return true ;
}
2020-05-19 13:24:43 +00:00
2020-05-29 06:59:34 +00:00
/**
* Insert post meta .
*
* @ param int $post_id Post ID to update .
* @ param string $data Meta datat add .
* @ param bool $check_image_existed Whether or not to check if image exists . true | false .
* @ param bool $is_woocomerce Whether or not the post is a woocommerce product . true | false .
*
* @ return array | bool | string [] Response array , true | false , Error message .
*/
private function insert_postmeta ( $post_id , $data , $check_image_existed , $is_woocomerce ) {
2020-05-19 20:08:55 +07:00
foreach ( $data [ 'postmeta' ] as $key ) {
if ( isset ( $key [ 'meta_key' ] ) && isset ( $key [ 'meta_value' ] ) ) {
2020-05-20 16:51:25 +07:00
$meta_value = $key [ 'meta_value' ];
2020-05-19 20:08:55 +07:00
if ( $is_woocomerce ) {
if ( '_sku' == $key [ 'meta_key' ] ) {
2020-05-20 16:51:25 +07:00
if ( ! wc_product_has_unique_sku ( $post_id , $meta_value ) ) {
2020-05-19 20:08:55 +07:00
return array ( 'error' => __ ( 'Product SKU must be unique' , $this -> plugin_translate ) );
}
}
2020-05-20 09:52:01 +00:00
if ( '_product_image_gallery' == $key [ 'meta_key' ] ) {
2020-05-19 20:08:55 +07:00
if ( isset ( $data [ 'extras' ][ 'woocommerce' ][ 'product_images' ] ) ) {
2020-05-20 16:51:25 +07:00
$ret = $this -> upload_postmeta_image ( $data [ 'extras' ][ 'woocommerce' ][ 'product_images' ], $meta_value , $check_image_existed );
2020-05-20 09:52:01 +00:00
if ( true !== $ret ) {
2020-05-20 16:51:25 +07:00
return $ret ;
2020-05-20 09:52:01 +00:00
}
2020-05-19 20:08:55 +07:00
} else {
continue ;
}
}
}
if ( '_thumbnail_id' == $key [ 'meta_key' ] ) {
if ( isset ( $data [ 'extras' ][ 'featured_image' ] ) ) {
try {
$upload_featured_image = MainWP_Utility :: upload_image ( $data [ 'extras' ][ 'featured_image' ], array (), $check_image_existed );
if ( null !== $upload_featured_image ) {
2020-05-20 16:51:25 +07:00
$meta_value = $upload_featured_image [ 'id' ];
2020-05-19 20:08:55 +07:00
} else {
return array ( 'error' => __ ( 'Cannot add featured image' , $this -> plugin_translate ) );
}
} catch ( \Exception $e ) {
continue ;
}
} else {
continue ;
}
}
2020-05-20 16:51:25 +07:00
$meta_value = maybe_unserialize ( $meta_value );
2020-05-19 20:08:55 +07:00
if ( add_post_meta ( $post_id , $key [ 'meta_key' ], $meta_value ) === false ) {
return array ( 'error' => __ ( 'Error when adding post meta' , $this -> plugin_translate ) . ' `' . esc_html ( $key [ 'meta_key' ] ) . '`' );
}
}
}
return true ;
2016-05-22 17:16:34 +02:00
}
2020-05-20 09:52:01 +00:00
2020-05-29 06:59:34 +00:00
/**
* Upload post meta image .
*
* @ param array $product_images Woocomerce product images .
* @ param $meta_value
* @ param $check_image_existed
* @ return array | bool Error message array or TRUE on success .
*/
private function upload_postmeta_image ( $product_images , & $meta_value , $check_image_existed ) {
2020-05-20 16:51:25 +07:00
$product_image_gallery = array ();
foreach ( $product_images as $product_image ) {
try {
$upload_featured_image = MainWP_Utility :: upload_image ( $product_image , array (), $check_image_existed );
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 ;
}
}
$meta_value = implode ( $product_image_gallery , ',' );
return true ;
}
2017-05-11 21:07:42 +02:00
}