/** * Pushes the saved post to the relevant portal blogs * * Assembles the post data required for submitting a new post in the portal sites, grabs a list * of portal sites to push to and then runs through each, submitting the post data as a new post. * * @param int $orig_post_id ID of the saved post. * @param object $orig_post WP_Post object for the saved post. * * @return void */ protected function push_post_data_to_blogs($orig_post_id, $orig_post) { global $current_blog; if ($this->recursing) { return; } $this->recursing = true; // Prepare the post data. $orig_post_data = $this->prepare_post_data($orig_post_id); // Prepare the metadata. $orig_meta_data = $this->prepare_meta_data($orig_post_id, $current_blog); // Prepare terms. $orig_terms = $this->prepare_terms($orig_post_id, $orig_post); // Prepare featured image. $featured_image = $this->prepare_featured_image($orig_post_id); // Get the array of sites to sync to. $sync_destinations = $this->aggregator->get_portals($current_blog->blog_id); // Loop through all destinations to perform the sync. foreach ($sync_destinations as $sync_destination) { // Check this destination is (probably) okay to push to. if (!$this->check_destination($sync_destination)) { continue; } // Get the relevant sync job, if there is one. $this->job = new Aggregator_Job($sync_destination, $current_blog->blog_id); if (!$this->job->job_id) { continue; // There is no job for this destination. } // Check if we should be pushing this post, don't if not. if (!$this->allowed_post_type($orig_post->post_type, $this->job->get_post_types())) { return; } // Take the list of associated taxonomy terms and remove any taxonomies not allowed. $destination_terms = $this->allowed_taxonomies($orig_terms); // Take the list of associated taxonomy terms and remove any terms not allowed. $destination_terms = $this->allowed_terms($destination_terms); if (is_wp_error($destination_terms)) { continue; // See allowed_terms(). } // Okay, fine, switch sites and do the synchronisation dance. // @codingStandardsIgnoreStart switch_to_blog($sync_destination); // @codingStandardsIgnoreEnd // Make we use the right author. $orig_post_data['author'] = $this->prepare_author_id($orig_post_data['author'], $sync_destination); // Acquire ID and update post (or insert post and acquire ID). $portal_target_post_id = $this->get_portal_blog_post_id($orig_post_id, $current_blog->blog_id); if (false !== $portal_target_post_id) { $target_post_id = $orig_post_data['ID']; wp_update_post($orig_post_data); } else { $target_post_id = wp_insert_post($orig_post_data); } // Push the meta data. $this->push_meta_data($target_post_id, $orig_meta_data); // Push the featured image. if ($featured_image) { $this->push_featured_image($target_post_id, $featured_image); } // Push taxonomies and terms. $this->push_taxonomy_terms($target_post_id, $destination_terms); $portal_site_url = get_home_url($sync_destination); // Filter url of portal_site_url. $portal_site_url = apply_filters('aggregator_remote_get_url', $portal_site_url); // Args for wp_remote_get function. $args = array('blocking' => false); // If WP_CRON_LOCK_TIMEOUT is set and a number, set the curl timeout to a higher value. if (defined(WP_CRON_LOCK_TIMEOUT) && is_numeric(WP_CRON_LOCK_TIMEOUT)) { // Add 1 to time, give it a little extra time. $timeout = intval(WP_CRON_LOCK_TIMEOUT) + 1; $args['timeout'] = $timeout; } // Filter args for wp_remote_get. $args = apply_filters('aggregator_remote_get_args', $args, $portal_site_url); // Ping the cron on the portal site to trigger term import now. if (function_exists('vip_safe_wp_remote_get')) { vip_safe_wp_remote_get($portal_site_url . '/wp-cron.php', $args); } else { // @codingStandardsIgnoreStart wp_remote_get($portal_site_url . '/wp-cron.php', $args); // @codingStandardsIgnoreEnd } // Finally, set the post status. $new_post_data = get_post($target_post_id); /** * Filter the status of a syncing post. * * Allows for the status of a synced post to be overridden. Before * this filter runs, the status will be 'pending'. The value of * `$status` is the status of the original post being synced, which * will be passed to `wp_update_post()`. * * @var string $status The final status of the post. */ $new_post_data->post_status = apply_filters('aggregator_post_status', $orig_post_data['post_status']); wp_update_post($new_post_data); // Get boolean on if post is updated or brand new. $updating = false !== $portal_target_post_id; $new_post_id = $updating ? $portal_target_post_id : $new_post_data->ID; /** * Do an action after the post has been successfully created on the destination site. * * @param int $new_post_id Fresh post ID * @param int $orig_post_id Original post ID * @param int $sync_destination Destination site ID * @param int $current_blog Original site ID * @param bool $updating Whether post is updating (true) or a new post */ do_action('aggregator_after_push_post_data', $new_post_id, $orig_post_id, $sync_destination, $current_blog->blog_id, $updating); // Switch back to source blog. restore_current_blog(); } $this->recursing = false; }
</select> </p> <?php submit_button(esc_html__('Save & Continue')); ?> </form> </div> <?php break; case 'delete': // Check we have valid portal and source IDs. if (!$portal_id || !$source_id) { wp_die(esc_html__('Invalid site ID(s).')); } // Get the job to be deleted. $job = new Aggregator_Job($portal_id, $source_id); // Do the deletion. $job->delete_job(); // Return to Aggregator Setup and print a message. wp_redirect(network_admin_url('settings.php?page=aggregator&deleted=1')); break; } if (!isset($action) || 'edit' !== $action && 'add' !== $action) { echo '<div class="wrap">'; echo '<h2>' . esc_html(get_admin_page_title()); // Allow network admins to add new Aggregator Jobs. if (current_user_can('manage_sites')) { ?> <a href="<?php echo esc_url(network_admin_url('settings.php?page=aggregator&action=add')); ?>
/** * Save the aggregator job. * * @param int $post_id ID of the Aggregator post. * @param WP_Post $post Post object of the Aggregator post. */ public function publish_aggregator_job($post_id, $post) { // Find the portal ID. if (isset($_POST['portal'])) { // Input var okay. $portal = intval($_POST['portal']); // Input var okay. } else { return; } // Create a new aggregator job. If one already exists for this porta/source combination // the following will load the existing settings. $sync_job = new Aggregator_Job($portal, get_current_blog_id()); // Set the post ID. If a post already exists for this job, it will be deleted. $sync_job->set_post_id($post_id); // Defaults for the post types and taxonomies. $cpts = $sync_job->get_post_types(); $taxos = $sync_job->get_taxonomies(); // Get any selected post types. if (isset($_POST['cpts'])) { // Input var okay. $cpts = array_map('sanitize_text_field', wp_unslash($_POST['cpts'])); // Input var okay. } // Save the new/changed post types. $sync_job->set_post_types($cpts); // Get any selected taxonomies. if (isset($_POST['taxos'])) { // Input var okay. $taxos = array_map('sanitize_text_field', wp_unslash($_POST['taxos'])); // Input var okay. } // Save the new/changed taxonomies. $sync_job->set_taxonomies($taxos); // Save the portal ID as post meta. $sync_job->set_portal_blog_id_meta(); // Set the author. $sync_job->set_author($post->post_author); // Update the network options for network admin pages. $sync_job->update_network_options(); // Redirect back to network admin settings, with a success message. wp_redirect(network_admin_url('settings.php?page=aggregator')); exit; }