Admin_Apple_Meta_Boxes::do_publish PHP Method

do_publish() public method

Check for a publish action from the meta box.
Since: 0.9.0
public do_publish ( integer $post_id, WP_Post $post )
$post_id integer
$post WP_Post
    public function do_publish($post_id, $post)
    {
        // Check if the values we want are present in $_REQUEST params.
        if (empty($_POST['apple_news_nonce']) || empty($_POST['post_ID'])) {
            return;
        }
        // Check the nonce
        if (!wp_verify_nonce($_POST['apple_news_nonce'], $this->publish_action)) {
            return;
        }
        // Save meta box fields
        $post_id = absint($_POST['post_ID']);
        self::save_post_meta($post_id);
        // If this is set to autosync or no action is set, we're done here
        if ('yes' == $this->settings->get('api_autosync') || 'publish' != $post->post_status || empty($_POST['apple_news_publish_action']) || $this->publish_action != $_POST['apple_news_publish_action']) {
            return;
        }
        // Proceed with the push
        $action = new Apple_Actions\Index\Push($this->settings, $post_id);
        try {
            $action->perform();
            // In async mode, success or failure will be displayed later
            if ('yes' !== $this->settings->get('api_async')) {
                Admin_Apple_Notice::success(__('Your article has been pushed successfully to Apple News!', 'apple-news'));
            } else {
                Admin_Apple_Notice::success(__('Your article will be pushed shortly to Apple News.', 'apple-news'));
            }
        } catch (Apple_Actions\Action_Exception $e) {
            Admin_Apple_Notice::error($e->getMessage());
        }
    }

Usage Example

 public function testSaveWithAutoSync()
 {
     // Set API settings to not auto sync and to enable the meta box
     $this->settings->set('api_autosync', 'yes');
     $this->settings->set('show_metabox', 'yes');
     // Create post
     $post_id = $this->factory->post->create();
     // Create post data
     $_POST['post_ID'] = $post_id;
     $_POST['apple_news_sections'] = array('https://u48r14.digitalhub.com/sections/1234567890');
     $_POST['apple_news_is_preview'] = 0;
     $_POST['apple_news_pullquote'] = 'test pullquote';
     $_POST['apple_news_pullquote_position'] = 'middle';
     $_POST['apple_news_nonce'] = wp_create_nonce('apple_news_publish');
     $_POST['apple_news_publish_action'] = 'apple_news_publish';
     // Create the meta box class and simulate a save
     $meta_box = new Admin_Apple_Meta_Boxes($this->settings);
     if ('yes' == $this->settings->get('show_metabox')) {
         $meta_box->do_publish($post_id, get_post($post_id));
     }
     // Check the meta values
     $this->assertEquals(array('https://u48r14.digitalhub.com/sections/1234567890'), get_post_meta($post_id, 'apple_news_sections', true));
     $this->assertEquals(false, get_post_meta($post_id, 'apple_news_is_preview', true));
     $this->assertEquals('test pullquote', get_post_meta($post_id, 'apple_news_pullquote', true));
     $this->assertEquals('middle', get_post_meta($post_id, 'apple_news_pullquote_position', true));
 }