Apple_Actions\Index\Push::perform PHP Method

perform() public method

Perform the push action.
public perform ( boolean $doing_async = false, $user_id = null ) : boolean
$doing_async boolean
return boolean
    public function perform($doing_async = false, $user_id = null)
    {
        if ('yes' === $this->settings->get('api_async') && false === $doing_async) {
            // Do not proceed if this is already pending publish
            $pending = get_post_meta($this->id, 'apple_news_api_pending', true);
            if (!empty($pending)) {
                return false;
            }
            // Track this publish event as pending with the timestamp it was sent
            update_post_meta($this->id, 'apple_news_api_pending', time());
            wp_schedule_single_event(time(), \Admin_Apple_Async::ASYNC_PUSH_HOOK, array($this->id, get_current_user_id()));
        } else {
            return $this->push($user_id);
        }
    }

Usage Example

 public function testJSONErrorsFail()
 {
     $this->settings->set('component_alerts', 'none');
     $this->settings->set('json_alerts', 'fail');
     $response = $this->dummy_response();
     $api = $this->prophet->prophesize('\\Apple_Push_API\\API');
     $api->post_article_to_channel(Argument::cetera())->willReturn($response)->shouldNotBeCalled();
     // We need to create an iframe, so run as administrator
     $user_id = $this->set_admin();
     // Create post
     $post_id = $this->factory->post->create(array('post_content' => 'ÂÂîî'));
     $action = new Push($this->settings, $post_id);
     $action->set_api($api->reveal());
     try {
         $action->perform();
     } catch (Action_Exception $e) {
         // An admin error notice was created
         $notices = get_user_meta($user_id, 'apple_news_notice', true);
         $this->assertNotEmpty($notices);
         $component_notice = end($notices);
         $this->assertEquals('The following JSON errors were detected and prevented publishing: Invalid unicode character sequences were found that could cause display issues on Apple News: ÂÂîî', $e->getMessage());
         // The post was not sent to Apple News
         $this->assertEquals(null, get_post_meta($post_id, 'apple_news_api_id', true));
         $this->assertEquals(null, get_post_meta($post_id, 'apple_news_api_created_at', true));
         $this->assertEquals(null, get_post_meta($post_id, 'apple_news_api_modified_at', true));
         $this->assertEquals(null, get_post_meta($post_id, 'apple_news_api_share_url', true));
         $this->assertEquals(null, get_post_meta($post_id, 'apple_news_api_deleted', true));
     }
 }