/** * Save post meta. * * @param int $post_id * @param object $post */ public static function save_post($post_id = null, $post = null) { if (!(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)) { if ($post->post_type == 'camp_bulkposter') { if ($post->post_status != 'auto-draft') { # keyword meta post $keyword = isset($_POST['keyword']) ? trim($_POST['keyword']) : ''; delete_post_meta($post_id, 'keyword'); if (!empty($keyword)) { add_post_meta($post_id, 'keyword', $keyword); } # Count meta post $count = isset($_POST['count']) ? trim($_POST['count']) : ''; delete_post_meta($post_id, 'count'); if (!empty($count)) { add_post_meta($post_id, 'count', $count); } # Category ID $category_id = isset($_POST['category_id']) ? trim($_POST['category_id']) : ''; delete_post_meta($post_id, 'category_id'); if (!empty($category_id)) { add_post_meta($post_id, 'category_id', $category_id); } # Post Status $status = isset($_POST['status']) ? trim($_POST['status']) : ''; delete_post_meta($post_id, 'status'); if (!empty($status)) { add_post_meta($post_id, 'status', $status); } # Post Type $ktzplg_post_type = isset($_POST['ktzplg_post_type']) ? trim($_POST['ktzplg_post_type']) : ''; delete_post_meta($post_id, 'ktzplg_post_type'); if (!empty($ktzplg_post_type)) { add_post_meta($post_id, 'ktzplg_post_type', $ktzplg_post_type); } # Schedule Post $ktzplg_next = isset($_POST['ktzplg_next']) ? trim($_POST['ktzplg_next']) : ''; delete_post_meta($post_id, 'ktzplg_next'); if (!empty($ktzplg_next)) { add_post_meta($post_id, 'ktzplg_next', $ktzplg_next); } $ktzplg_next_time = isset($_POST['ktzplg_next_time']) ? trim($_POST['ktzplg_next_time']) : ''; delete_post_meta($post_id, 'ktzplg_next_time'); if (!empty($ktzplg_next_time)) { add_post_meta($post_id, 'ktzplg_next_time', $ktzplg_next_time); } # Logical insert post after Publish campaign if (!empty($count) && !empty($keyword)) { $keyword = explode("\n", $keyword); # Rand Keyword Submission if (count($keyword > 0)) { shuffle($keyword); $keyword = array_slice($keyword, 0, $count); } foreach ($keyword as $kwd) { $title = sanitize_title($kwd); $title = str_replace('-', ' ', $title); $title = ucwords($title); # https://developer.wordpress.org/reference/functions/post_exists/ $post_id = post_exists($title); $content = Ktzagcplugin_Option::get_option('ktzplg_agc_content'); $replace_with_this = ' keyword="' . $title . '"]'; $content_wow = str_replace(']', $replace_with_this, $content); $content = do_shortcode($content_wow); # if not post same keyword do it.. if (!$post_id) { # Insert post => ktzplg_insert_post ( $post_type='', $title = '', $status = '', $category_id = array() , $content = '', $author = '' ); ktzplg_insert_post($ktzplg_post_type, $title, $status, array($category_id), $content, 1); } } # end if foreach ( $keyword as $kwd ) } # end if !empty( $count ) && !empty( $keyword ) } # end if ( $post->post_status != 'auto-draft' ) } } }
function ktzplg_lets_start_autopost($camp_id) { $arr = array($camp_id); $args = array('post_type' => 'camp_bulkposter', 'post_status' => 'publish', 'post__in' => $arr); $myposts = get_posts($args); if ($myposts) { foreach ($myposts as $post) { setup_postdata($post); $keyword = get_post_meta($post->ID, 'keyword', true); if (empty($keyword)) { $keyword = ''; } $count = get_post_meta($post->ID, 'count', true); if (empty($count)) { $count = 1; } # get category id meta post $category_id = get_post_meta($post->ID, 'category_id', true); if (empty($category_id)) { $category_id = 1; } # get status meta post $status = get_post_meta($post->ID, 'status', true); if (empty($status)) { $status = 'publish'; } # get post_type meta post $ktzplg_post_type = get_post_meta($post->ID, 'ktzplg_post_type', true); if (empty($ktzplg_post_type)) { $status = 'post'; } # Logical insert post after Publish campaign if (!empty($count) && !empty($keyword)) { $keyword = explode("\n", $keyword); # Rand Keyword Submission if (count($keyword > 0)) { shuffle($keyword); $keyword = array_slice($keyword, 0, $count); } foreach ($keyword as $kwd) { # get title via keyword $title = sanitize_title($kwd); $title = str_replace('-', ' ', $title); $title = ucwords($title); $args = array('post_type' => $ktzplg_post_type, 'post_status' => 'publish'); $myposts = get_posts($args); # get content via plugin settings $content = Ktzagcplugin_Option::get_option('ktzplg_agc_content'); $replace_with_this = ' keyword="' . $title . '"]'; $content_wow = str_replace(']', $replace_with_this, $content); $content = do_shortcode($content_wow); # if not post same keyword do it.. if (!get_page_by_title($title, 'OBJECT', $ktzplg_post_type)) { # Insert post => ktzplg_insert_post ( $post_type='', $title = '', $status = '', $category_id = array() , $content = '', $author = '' ); ktzplg_insert_post($ktzplg_post_type, $title, $status, array($category_id), $content, 1); } } # end if foreach ( $keyword as $kwd ) } # end if !empty( $count ) && !empty( $keyword ) } # end if foreach ( $myposts as $post ) wp_reset_postdata(); } }