static function Instance()
 {
     if (null === MainWP_Child_Robot::$instance) {
         MainWP_Child_Robot::$instance = new MainWP_Child_Robot();
     }
     return MainWP_Child_Robot::$instance;
 }
 static function createPost($new_post, $post_custom, $post_category, $post_featured_image, $upload_dir, $post_tags)
 {
     global $current_user;
     $wprocket_fields = array('lazyload', 'lazyload_iframes', 'minify_html', 'minify_css', 'minify_js', 'cdn');
     $wprocket_activated = false;
     if (MainWP_Child_WP_Rocket::isActivated()) {
         if (function_exists('get_rocket_option')) {
             $wprocket_activated = true;
             foreach ($wprocket_fields as $field) {
                 if (!isset($post_custom['_rocket_exclude_' . $field])) {
                     // check not exclude only
                     if (!get_rocket_option($field)) {
                         $post_custom['_rocket_exclude_' . $field] = array(true);
                         // set as excluded
                     }
                 }
             }
         }
     }
     if (!$wprocket_activated) {
         foreach ($wprocket_fields as $field) {
             if (isset($post_custom['_rocket_exclude_' . $field])) {
                 unset($post_custom['_rocket_exclude_' . $field]);
             }
         }
     }
     //Set up a new post (adding addition information)
     $usr = get_user_by('login', $_POST['user']);
     //$new_post['post_author'] = $current_user->ID;
     $is_robot_post = false;
     if (isset($_POST['isMainWPRobot']) && !empty($_POST['isMainWPRobot'])) {
         $is_robot_post = true;
     }
     $post_author = isset($new_post['post_author']) ? $new_post['post_author'] : $usr->ID;
     if ($is_robot_post) {
         if (1 === $post_author) {
             $new_post['post_author'] = $usr->ID;
         } else {
             if (!is_numeric($post_author)) {
                 $user_author = get_user_by('login', $post_author);
                 if ($user_author) {
                     $post_author = $user_author->ID;
                 } else {
                     $random_password = wp_generate_password($length = 12, $include_standard_special_chars = false);
                     $post_author = wp_create_user($post_author, $random_password, $post_author . '@asdf.com');
                 }
             }
         }
     } else {
         if (isset($new_post['custom_post_author']) && !empty($new_post['custom_post_author'])) {
             $_author = get_user_by('login', $new_post['custom_post_author']);
             if (!empty($_author)) {
                 $new_post['post_author'] = $_author->ID;
             } else {
                 $new_post['post_author'] = $usr->ID;
             }
             unset($new_post['custom_post_author']);
         }
     }
     $post_author = !empty($post_author) ? $post_author : $usr->ID;
     $new_post['post_author'] = $post_author;
     $is_ezine_post = !empty($post_custom['_ezine_post_article_source']) ? true : false;
     $terms = $new_post['_ezin_post_category'];
     unset($new_post['_ezin_post_category']);
     $is_post_plus = isset($post_custom['_mainwp_post_plus']) ? true : false;
     $wp_error = null;
     if ($is_ezine_post || $is_post_plus) {
         if (isset($new_post['post_date_gmt']) && !empty($new_post['post_date_gmt'])) {
             $post_date_timestamp = strtotime($new_post['post_date_gmt']) + get_option('gmt_offset') * 60 * 60;
             $new_post['post_date'] = date('Y-m-d H:i:s', $post_date_timestamp);
             $new_post['post_status'] = $post_date_timestamp <= current_time('timestamp') ? 'publish' : 'future';
         } else {
             $new_post['post_status'] = 'publish';
         }
     }
     $wpr_options = isset($_POST['wpr_options']) ? $_POST['wpr_options'] : array();
     //Search for all the images added to the new post
     //some images have a href tag to click to navigate to the image.. we need to replace this too
     $foundMatches = preg_match_all('/(<a[^>]+href=\\"(.*?)\\"[^>]*>)?(<img[^>\\/]*src=\\"((.*?)(png|gif|jpg|jpeg))\\")/ix', $new_post['post_content'], $matches, PREG_SET_ORDER);
     if (($foundMatches > 0 || $is_robot_post && isset($wpr_options['wpr_save_images']) && 'Yes' === $wpr_options['wpr_save_images']) && !$is_ezine_post) {
         //We found images, now to download them so we can start balbal
         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 {
                 $downloadfile = MainWP_Helper::uploadImage($originalImgUrl);
                 $localUrl = $downloadfile['url'];
                 $linkToReplaceWith = dirname($localUrl);
                 if ('' !== $hrefLink) {
                     $server = get_option('mainwp_child_server');
                     $serverHost = parse_url($server, PHP_URL_HOST);
                     if (!empty($serverHost) && strpos($hrefLink, $serverHost) !== false) {
                         $serverHref = 'href="' . $serverHost;
                         $replaceServerHref = 'href="' . parse_url($localUrl, PHP_URL_SCHEME) . '://' . parse_url($localUrl, PHP_URL_HOST);
                         $new_post['post_content'] = str_replace($serverHref, $replaceServerHref, $new_post['post_content']);
                     } else {
                         if (strpos($hrefLink, 'http') !== false) {
                             $lnkToReplace = dirname($hrefLink);
                             if ('http:' !== $lnkToReplace && 'https:' !== $lnkToReplace) {
                                 $new_post['post_content'] = str_replace($lnkToReplace, $linkToReplaceWith, $new_post['post_content']);
                             }
                         }
                     }
                 }
                 $lnkToReplace = dirname($imgUrl);
                 if ('http:' !== $lnkToReplace && 'https:' !== $lnkToReplace) {
                     $new_post['post_content'] = str_replace($lnkToReplace, $linkToReplaceWith, $new_post['post_content']);
                 }
             } catch (Exception $e) {
             }
         }
     }
     if ($is_post_plus) {
         $random_publish_date = isset($post_custom['_saved_draft_random_publish_date']) ? $post_custom['_saved_draft_random_publish_date'] : false;
         $random_publish_date = is_array($random_publish_date) ? current($random_publish_date) : null;
         if (!empty($random_publish_date)) {
             $random_date_from = isset($post_custom['_saved_draft_publish_date_from']) ? $post_custom['_saved_draft_publish_date_from'] : 0;
             $random_date_from = is_array($random_date_from) ? current($random_date_from) : 0;
             $random_date_to = isset($post_custom['_saved_draft_publish_date_to']) ? $post_custom['_saved_draft_publish_date_to'] : 0;
             $random_date_to = is_array($random_date_to) ? current($random_date_to) : 0;
             $now = current_time('timestamp');
             if (empty($random_date_from)) {
                 $random_date_from = $now;
             }
             if (empty($random_date_to)) {
                 $random_date_to = $now;
             }
             if ($random_date_from === $now && $random_date_from === $random_date_to) {
                 $random_date_to = $now + 7 * 24 * 3600;
             }
             if ($random_date_from > $random_date_to) {
                 $tmp = $random_date_from;
                 $random_date_from = $random_date_to;
                 $random_date_to = $tmp;
             }
             $random_timestamp = rand($random_date_from, $random_date_to);
             $post_status = $random_timestamp <= current_time('timestamp') ? 'publish' : 'future';
             $new_post['post_status'] = $post_status;
             $new_post['post_date'] = date('Y-m-d H:i:s', $random_timestamp);
         }
     }
     if (isset($post_tags) && '' !== $post_tags) {
         $new_post['tags_input'] = $post_tags;
     }
     //Save the post to the wp
     remove_filter('content_save_pre', 'wp_filter_post_kses');
     // to fix brake scripts or html
     $post_status = $new_post['post_status'];
     $new_post['post_status'] = 'auto-draft';
     $new_post_id = wp_insert_post($new_post, $wp_error);
     //Show errors if something went wrong
     if (is_wp_error($wp_error)) {
         return $wp_error->get_error_message();
     }
     if (empty($new_post_id)) {
         return 'Undefined error';
     }
     wp_update_post(array('ID' => $new_post_id, 'post_status' => $post_status));
     if (!empty($terms)) {
         wp_set_object_terms($new_post_id, array_map(intval, $terms), 'category');
     }
     $permalink = get_permalink($new_post_id);
     $seo_ext_activated = false;
     if (class_exists('WPSEO_Meta') && class_exists('WPSEO_Admin')) {
         $seo_ext_activated = true;
     }
     //Set custom fields
     $not_allowed = array('_slug', '_tags', '_edit_lock', '_selected_sites', '_selected_groups', '_selected_by', '_categories', '_edit_last', '_sticky');
     $not_allowed[] = '_mainwp_boilerplate_sites_posts';
     $not_allowed[] = '_ezine_post_keyword';
     $not_allowed[] = '_ezine_post_display_sig';
     $not_allowed[] = '_ezine_post_remove_link';
     $not_allowed[] = '_ezine_post_grab_image';
     $not_allowed[] = '_ezine_post_grab_image_placement';
     $not_allowed[] = '_ezine_post_template_id';
     $not_allowed[] = '_mainwp_post_plus';
     $not_allowed[] = '_saved_as_draft';
     $not_allowed[] = '_saved_draft_categories';
     $not_allowed[] = '_saved_draft_tags';
     $not_allowed[] = '_saved_draft_random_privelege';
     $not_allowed[] = '_saved_draft_random_category';
     $not_allowed[] = '_saved_draft_random_publish_date';
     $not_allowed[] = '_saved_draft_publish_date_from';
     $not_allowed[] = '_saved_draft_publish_date_to';
     $not_allowed[] = '_post_to_only_existing_categories';
     $not_allowed[] = '_mainwp_robot_post_comments';
     $post_to_only_existing_categories = false;
     foreach ($post_custom as $meta_key => $meta_values) {
         if (!in_array($meta_key, $not_allowed)) {
             foreach ($meta_values as $meta_value) {
                 if (strpos($meta_key, "_mainwp_spinner_") === 0) {
                     continue;
                 }
                 // not save
                 if (!$seo_ext_activated) {
                     // if Wordpress SEO plugin is not activated do not save yoast post meta
                     if (strpos($meta_key, '_yoast_wpseo_') === false) {
                         add_post_meta($new_post_id, $meta_key, $meta_value);
                     }
                 } else {
                     add_post_meta($new_post_id, $meta_key, $meta_value);
                 }
             }
         } else {
             if ('_sticky' === $meta_key) {
                 foreach ($meta_values as $meta_value) {
                     if ('sticky' === base64_decode($meta_value)) {
                         stick_post($new_post_id);
                     }
                 }
             } else {
                 if ('_post_to_only_existing_categories' === $meta_key) {
                     if (isset($meta_values[0]) && $meta_values[0]) {
                         $post_to_only_existing_categories = true;
                     }
                 }
             }
         }
     }
     // yoast seo extension
     if ($seo_ext_activated) {
         $_seo_opengraph_image = isset($post_custom[WPSEO_Meta::$meta_prefix . 'opengraph-image']) ? $post_custom[WPSEO_Meta::$meta_prefix . 'opengraph-image'] : array();
         $_seo_opengraph_image = current($_seo_opengraph_image);
         $_server_domain = '';
         $_server = get_option('mainwp_child_server');
         if (preg_match('/(https?:\\/\\/[^\\/]+\\/).+/', $_server, $matchs)) {
             $_server_domain = isset($matchs[1]) ? $matchs[1] : '';
         }
         // upload image if it on the server
         if (!empty($_seo_opengraph_image) && strpos($_seo_opengraph_image, $_server_domain) !== false) {
             try {
                 $upload = MainWP_Helper::uploadImage($_seo_opengraph_image);
                 //Upload image to WP
                 if (null !== $upload) {
                     update_post_meta($new_post_id, WPSEO_Meta::$meta_prefix . 'opengraph-image', $upload['url']);
                     //Add the image to the post!
                 }
             } catch (Exception $e) {
             }
         }
     }
     //If categories exist, create them (second parameter of wp_create_categories adds the categories to the post)
     include_once ABSPATH . 'wp-admin/includes/taxonomy.php';
     //Contains wp_create_categories
     if (isset($post_category) && '' !== $post_category) {
         $categories = explode(',', $post_category);
         if (count($categories) > 0) {
             if (!$post_to_only_existing_categories) {
                 $post_category = wp_create_categories($categories, $new_post_id);
             } else {
                 $cat_ids = array();
                 foreach ($categories as $cat) {
                     if ($id = category_exists($cat)) {
                         $cat_ids[] = $id;
                     }
                 }
                 if (count($cat_ids) > 0) {
                     wp_set_post_categories($new_post_id, $cat_ids);
                 }
             }
         }
     }
     //If featured image exists - set it
     if (null !== $post_featured_image) {
         try {
             $upload = MainWP_Helper::uploadImage($post_featured_image);
             //Upload image to WP
             if (null !== $upload) {
                 update_post_meta($new_post_id, '_thumbnail_id', $upload['id']);
                 //Add the thumbnail to the post!
             }
         } catch (Exception $e) {
         }
     }
     // post plus extension process
     if ($is_post_plus) {
         $random_privelege = isset($post_custom['_saved_draft_random_privelege']) ? $post_custom['_saved_draft_random_privelege'] : null;
         $random_privelege = is_array($random_privelege) ? current($random_privelege) : null;
         $random_privelege_base = base64_decode($random_privelege);
         $random_privelege = maybe_unserialize($random_privelege_base);
         if (is_array($random_privelege) && count($random_privelege) > 0) {
             $random_post_authors = array();
             foreach ($random_privelege as $role) {
                 $users = get_users(array('role' => $role));
                 foreach ($users as $user) {
                     $random_post_authors[] = $user->ID;
                 }
             }
             if (count($random_post_authors) > 0) {
                 shuffle($random_post_authors);
                 $key = array_rand($random_post_authors);
                 wp_update_post(array('ID' => $new_post_id, 'post_author' => $random_post_authors[$key]));
             }
         }
         $random_category = isset($post_custom['_saved_draft_random_category']) ? $post_custom['_saved_draft_random_category'] : false;
         $random_category = is_array($random_category) ? current($random_category) : null;
         if (!empty($random_category)) {
             $cats = get_categories(array('type' => 'post', 'hide_empty' => 0));
             $random_cats = array();
             if (is_array($cats)) {
                 foreach ($cats as $cat) {
                     $random_cats[] = $cat->term_id;
                 }
             }
             if (count($random_cats) > 0) {
                 shuffle($random_cats);
                 $key = array_rand($random_cats);
                 wp_set_post_categories($new_post_id, array($random_cats[$key]), false);
             }
         }
     }
     // end of post plus
     // MainWP Robot
     if ($is_robot_post) {
         $all_comments = $post_custom['_mainwp_robot_post_comments'];
         MainWP_Child_Robot::Instance()->wpr_insertcomments($new_post_id, $all_comments);
     }
     $ret['success'] = true;
     $ret['link'] = $permalink;
     $ret['added_id'] = $new_post_id;
     return $ret;
 }