/** * Main function of this plugin called on publish_post action hook * * * @param int $post_ID ID of the new/updated post * @return void */ public static function handle_old_posts_publish() { $microblogposter_plg_old_posts_active_name = "microblogposter_plg_old_posts_active"; $old_posts_active = get_option($microblogposter_plg_old_posts_active_name, 0); if ($old_posts_active == '0') { return; } $microblogposter_plg_old_posts_nb_posts_name = "microblogposter_plg_old_posts_nb_posts"; $microblogposter_plg_old_posts_min_age_name = "microblogposter_plg_old_posts_min_age"; $microblogposter_plg_old_posts_max_age_name = "microblogposter_plg_old_posts_max_age"; $microblogposter_plg_old_posts_expire_age_name = "microblogposter_plg_old_posts_expire_age"; $excluded_categories_old_name = "microblogposter_excluded_categories_old"; $nb_posts = get_option($microblogposter_plg_old_posts_nb_posts_name, 1); $min_age = get_option($microblogposter_plg_old_posts_min_age_name, 30); $max_age = get_option($microblogposter_plg_old_posts_max_age_name, 180); $expire_age = get_option($microblogposter_plg_old_posts_expire_age_name, 30); $excluded_categories_old_value = get_option($excluded_categories_old_name, ""); $excluded_categories_old = json_decode($excluded_categories_old_value, true); global $wpdb; $table_old_items = $wpdb->prefix . 'microblogposter_old_items'; $table_posts = $wpdb->prefix . 'posts'; $table_term_relationships = $wpdb->prefix . 'term_relationships'; $table_term_taxonomy = $wpdb->prefix . 'term_taxonomy'; $interval = MicroblogPoster_Poster::get_custom_cron_interval(); $sql = "SELECT * FROM {$table_old_items} WHERE publish_datetime > DATE_SUB(NOW(), INTERVAL {$interval} HOUR)"; $old_posts_published = $wpdb->get_results($sql, ARRAY_A); if (is_array($old_posts_published) && !empty($old_posts_published)) { return; } $sql_old = "SELECT * FROM {$table_posts} AS p WHERE p.post_status = 'publish' AND p.post_type = 'post'"; if ($min_age > 0) { $sql_old .= " AND p.post_date < DATE_SUB(NOW(), INTERVAL {$min_age} DAY)"; } if ($max_age > 0) { $sql_old .= " AND p.post_date > DATE_SUB(NOW(), INTERVAL {$max_age} DAY)"; } if (is_array($excluded_categories_old) && !empty($excluded_categories_old)) { $excluded_categories_string = ""; foreach ($excluded_categories_old as $excluded_category_old) { if (intval($excluded_category_old)) { $excluded_categories_string .= $excluded_category_old . ","; } } $excluded_categories_string = rtrim($excluded_categories_string, ","); if ($excluded_categories_string) { $sql_old .= " AND p.ID NOT IN"; $sql_old .= " (SELECT termr.object_id FROM {$table_term_taxonomy} AS termt INNER JOIN {$table_term_relationships} AS termr"; $sql_old .= " ON termt.term_taxonomy_id=termr.term_taxonomy_id"; $sql_old .= " WHERE termt.term_id IN ({$excluded_categories_string}) AND termt.taxonomy='category')"; } } if (MicroblogPoster_Poster::is_method_callable('MicroblogPoster_Poster_Ultimate', 'resolve_sql_allowed_authors')) { $sql_old .= MicroblogPoster_Poster_Ultimate::resolve_sql_allowed_authors(); } $sql_old .= " AND p.ID NOT IN (SELECT item_id from {$table_old_items} WHERE item_type='post')"; $sql_old .= " ORDER BY p.post_date ASC"; $sql_old .= " LIMIT 10"; $old_posts = $wpdb->get_results($sql_old, ARRAY_A); if (is_array($old_posts) && !empty($old_posts)) { for ($i = 0; $i < $nb_posts; $i++) { if (isset($old_posts[$i])) { $post_id = $old_posts[$i]['ID']; $sql = "INSERT INTO {$table_old_items} (item_id,item_type) \n VALUES ('{$post_id}','post')"; $wpdb->query($sql); MicroblogPoster_Poster::update_old_post($post_id); } } } if (intval($expire_age) > 0) { $sql = "DELETE FROM {$table_old_items} WHERE publish_datetime < DATE_SUB(NOW(), INTERVAL {$expire_age} DAY)"; $wpdb->query($sql); } }