Apple_Actions\Index\Push::push PHP Method

push() private method

Push the post using the API data.
private push ( integer $user_id = null )
$user_id integer
    private function push($user_id = null)
    {
        if (!$this->is_api_configuration_valid()) {
            throw new \Apple_Actions\Action_Exception(__('Your Apple News API settings seem to be empty. Please fill in the API key, API secret and API channel fields in the plugin configuration page.', 'apple-news'));
        }
        /**
         * Should the post be skipped and not pushed to apple news.
         *
         * Default is false, but filterable.
         */
        if (apply_filters('apple_news_skip_push', false, $this->id)) {
            return;
        }
        // Ignore if the post is already in sync
        if ($this->is_post_in_sync()) {
            return;
        }
        // generate_article uses Exporter->generate, so we MUST clean the workspace
        // before and after its usage.
        $this->clean_workspace();
        list($json, $bundles, $errors) = $this->generate_article();
        // Process errors
        $this->process_errors($errors);
        // Validate the data before using since it's filterable.
        // JSON should just be a string.
        // Apple News format is complex and has too many options to validate otherwise.
        // Let's just make sure it's not doing anything bad and is the right data type.
        $json = sanitize_text_field($json);
        // Bundles should be an array of URLs
        if (!empty($bundles) && is_array($bundles)) {
            $bundles = array_map('esc_url_raw', $bundles);
        } else {
            $bundles = array();
        }
        try {
            // If there's an API ID, update, otherwise create.
            $remote_id = get_post_meta($this->id, 'apple_news_api_id', true);
            $result = null;
            do_action('apple_news_before_push', $this->id);
            // Populate optional metadata
            $meta = array('data' => array());
            // Get sections
            $sections = get_post_meta($this->id, 'apple_news_sections', true);
            if (is_array($sections)) {
                $meta['data']['links'] = array('sections' => $sections);
            }
            // Get the isPreview setting
            $is_preview = (bool) get_post_meta($this->id, 'apple_news_is_preview', true);
            if (true === $is_preview) {
                $meta['data']['isPreview'] = $is_preview;
            }
            if ($remote_id) {
                // Update the current article from the API in case the revision changed
                $this->get();
                // Get the current revision
                $revision = get_post_meta($this->id, 'apple_news_api_revision', true);
                $result = $this->get_api()->update_article($remote_id, $revision, $json, $bundles, $meta);
            } else {
                $result = $this->get_api()->post_article_to_channel($json, $this->get_setting('api_channel'), $bundles, $meta);
            }
            // Save the ID that was assigned to this post in by the API.
            update_post_meta($this->id, 'apple_news_api_id', sanitize_text_field($result->data->id));
            update_post_meta($this->id, 'apple_news_api_created_at', sanitize_text_field($result->data->createdAt));
            update_post_meta($this->id, 'apple_news_api_modified_at', sanitize_text_field($result->data->modifiedAt));
            update_post_meta($this->id, 'apple_news_api_share_url', sanitize_text_field($result->data->shareUrl));
            update_post_meta($this->id, 'apple_news_api_revision', sanitize_text_field($result->data->revision));
            // If it's marked as deleted, remove the mark. Ignore otherwise.
            delete_post_meta($this->id, 'apple_news_api_deleted');
            // Remove the pending designation if it exists
            delete_post_meta($this->id, 'apple_news_api_pending');
            // Remove the async in progress flag
            delete_post_meta($this->id, 'apple_news_api_async_in_progress');
            // Clear the cache for post status
            delete_transient('apple_news_post_state_' . $this->id);
            do_action('apple_news_after_push', $this->id, $result);
        } catch (\Apple_Push_API\Request\Request_Exception $e) {
            // Remove the pending designation if it exists
            delete_post_meta($this->id, 'apple_news_api_pending');
            // Remove the async in progress flag
            delete_post_meta($this->id, 'apple_news_api_async_in_progress');
            $this->clean_workspace();
            if (preg_match('#WRONG_REVISION#', $e->getMessage())) {
                throw new \Apple_Actions\Action_Exception(__('It seems like the article was updated by another call. If the problem persists, try removing and pushing again.', 'apple-news'));
            } else {
                throw new \Apple_Actions\Action_Exception(__('There has been an error with the API: ', 'apple-news') . $e->getMessage());
            }
        }
        $this->clean_workspace();
    }