Admin_Apple_Post_Sync::do_publish PHP Method

do_publish() public method

When a post is published, or a published post updated, trigger this function.
Since: 0.4.0
public do_publish ( integer $id, WP_Post $post )
$id integer
$post WP_Post
    public function do_publish($id, $post)
    {
        if ('publish' != $post->post_status || !in_array($post->post_type, $this->settings->get('post_types')) || !current_user_can(apply_filters('apple_news_publish_capability', 'manage_options')) && !(defined('DOING_CRON') && DOING_CRON)) {
            return;
        }
        // If the post has been marked as deleted from the API, ignore this update.
        $deleted = get_post_meta($id, 'apple_news_api_deleted', true);
        if ($deleted) {
            return;
        }
        // Proceed based on the current settings for auto publish and update.
        $updated = get_post_meta($id, 'apple_news_api_id', true);
        if ($updated && 'yes' != $this->settings->get('api_autosync_update') || !$updated && 'yes' != $this->settings->get('api_autosync')) {
            return;
        }
        // Proceed with the push
        $action = new Apple_Actions\Index\Push($this->settings, $id);
        try {
            $action->perform();
        } catch (Apple_Actions\Action_Exception $e) {
            Admin_Apple_Notice::error($e->getMessage());
        }
    }

Usage Example

 /**
  * Check for a publish action from the meta box.
  *
  * @since 0.9.0
  * @param int $post_id
  * @param WP_Post $post
  * @access public
  */
 public function do_publish($post_id, $post)
 {
     // Check if the values we want are present in $_REQUEST params.
     if (empty($_POST['apple_news_publish_action']) || empty($_POST['apple_news_publish_nonce']) || empty($_POST['post_ID'])) {
         return;
     }
     // Check the nonce
     if (!wp_verify_nonce($_POST['apple_news_publish_nonce'], $this->publish_action)) {
         return;
     }
     // Do the publish
     $post_sync = new Admin_Apple_Post_Sync($this->settings);
     $post_sync->do_publish($post_id, $post);
 }