/**
  * Handles the ajax action to push a post to Apple News.
  *
  * @access public
  */
 public function ajax_push_post()
 {
     // Check the nonce
     check_ajax_referer(self::ACTION);
     // Check capabilities
     if (!current_user_can(apply_filters('apple_news_publish_capability', 'manage_options'))) {
         echo json_encode(array('success' => false, 'error' => __('You do not have permission to publish to Apple News', 'apple-news')));
         wp_die();
     }
     // Sanitize input data
     $id = absint($_GET['id']);
     // TODO: Move push action to shared
     $action = new Apple_Actions\Index\Push($this->settings, $id);
     try {
         $errors = $action->perform();
     } catch (\Apple_Actions\Action_Exception $e) {
         $errors = $e->getMessage();
     }
     if ($errors) {
         echo json_encode(array('success' => false, 'error' => $errors));
     } else {
         echo json_encode(array('success' => true));
     }
     // This is required to terminate immediately and return a valid response
     wp_die();
 }
 /**
  * Handle performing an asynchronous push request.
  *
  * @access public
  * @param int $post_id
  * @param int $user_id
  * @since 1.0.0
  */
 public function async_push($post_id, $user_id)
 {
     // This hook could be used for an ini_set to increase max execution time
     // for asynchronous publishing to handle large push requests.
     // Since some hosts wouldn't support it, the code isn't added directly here.
     //
     // On WordPress VIP this isn't necessary since the plugin
     // will automatically use the jobs system which can handle requests up to 12 hours.
     do_action('apple_news_before_async_push');
     // Ensure that the job can't be picked up twice
     $in_progress = get_post_meta($post_id, 'apple_news_api_async_in_progress', true);
     if (!empty($in_progress)) {
         return;
     }
     update_post_meta($post_id, 'apple_news_api_async_in_progress', time());
     // Ensure that the post is still published
     $post = get_post($post_id);
     if ('publish' != $post->post_status) {
         Admin_Apple_Notice::error(sprintf(__('Article %s is no longer published and cannot be pushed to Apple News.', 'apple-news'), $post->post_title), $user_id);
         return;
     }
     $action = new Apple_Actions\Index\Push($this->settings, $post_id);
     try {
         $action->perform(true, $user_id);
         Admin_Apple_Notice::success(sprintf(__('Article %s has been pushed successfully to Apple News!', 'apple-news'), $post->post_title), $user_id);
     } catch (Apple_Actions\Action_Exception $e) {
         Admin_Apple_Notice::error($e->getMessage(), $user_id);
     }
     do_action('apple_news_after_async_push');
 }
 /**
  * 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_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());
     }
 }
 /**
  * When a post is published, or a published post updated, trigger this
  * function.
  *
  * @since 0.4.0
  * @param int $id
  * @param WP_Post $post
  * @access public
  */
 public function do_publish($id, $post)
 {
     if (!current_user_can(apply_filters('apple_news_publish_capability', 'manage_options'))) {
         return;
     }
     // Proceed based on the current settings for auto publish and update.
     // Also, if the post has been marked as deleted from the API, ignore this update.
     $updated = get_post_meta($id, 'apple_news_api_id', true);
     $deleted = get_post_meta($id, 'apple_news_api_deleted', true);
     if ($deleted) {
         return;
     }
     $action = new Apple_Actions\Index\Push($this->settings, $id);
     try {
         $action->perform();
     } catch (Apple_Actions\Action_Exception $e) {
         Admin_Apple_Notice::error($e->getMessage());
     }
 }
 /**
  * When a post is published, or a published post updated, trigger this function.
  *
  * @since 0.4.0
  * @param int $id
  * @param WP_Post $post
  * @access public
  */
 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());
     }
 }
 /**
  * Handles a push to Apple News action.
  *
  * @param int $id
  * @access private
  */
 private function push_action($id)
 {
     // Ensure the post is published
     if ('publish' != get_post_status($id)) {
         $this->notice_error(sprintf(__('Article %s is not published and cannot be pushed to Apple News.', 'apple-news'), $id));
         return;
     }
     // Push the post
     $action = new Apple_Actions\Index\Push($this->settings, $id);
     try {
         $action->perform();
         // In async mode, success or failure will be displayed later
         if ('yes' !== $this->settings->get('api_async')) {
             $this->notice_success(__('Your article has been pushed successfully!', 'apple-news'));
         } else {
             $this->notice_success(__('Your article will be pushed shortly.', 'apple-news'));
         }
     } catch (Apple_Actions\Action_Exception $e) {
         $this->notice_error($e->getMessage());
     }
 }
 /**
  * Handles a push to Apple News action.
  *
  * @param int $id
  * @access private
  */
 private function push_action($id)
 {
     $action = new Apple_Actions\Index\Push($this->settings, $id);
     try {
         $action->perform();
         // In async mode, success or failure will be displayed later
         if ('yes' !== $this->settings->get('api_async')) {
             $this->notice_success(__('Your article has been pushed successfully!', 'apple-news'));
         } else {
             $this->notice_success(__('Your article will be pushed shortly.', 'apple-news'));
         }
     } catch (Apple_Actions\Action_Exception $e) {
         $this->notice_error($e->getMessage());
     }
 }
 /**
  * Handles a push to Apple News action.
  *
  * @param int $id
  * @access private
  */
 private function push_action($id)
 {
     // Ensure the post is published
     if ('publish' != get_post_status($id)) {
         $this->notice_error(sprintf(__('Article %s is not published and cannot be pushed to Apple News.', 'apple-news'), $id));
         return;
     }
     // Check the nonce.
     // If it isn't set, this isn't a form submission so we need to just display the form.
     if (!isset($_POST['apple_news_nonce'])) {
         return;
     }
     // If invalid, we need to display an error.
     if (!wp_verify_nonce($_POST['apple_news_nonce'], 'publish')) {
         $this->notice_error(__('Invalid nonce.', 'apple-news'));
     }
     // Save fields
     \Admin_Apple_Meta_Boxes::save_post_meta($id);
     $message = __('Settings saved.', 'apple-news');
     // Push the post
     $action = new Apple_Actions\Index\Push($this->settings, $id);
     try {
         $action->perform();
         // In async mode, success or failure will be displayed later
         if ('yes' !== $this->settings->get('api_async')) {
             $this->notice_success(__('Your article has been pushed successfully!', 'apple-news'));
         } else {
             $this->notice_success(__('Your article will be pushed shortly.', 'apple-news'));
         }
     } catch (Apple_Actions\Action_Exception $e) {
         $this->notice_error($e->getMessage());
     }
 }
 /**
  * Handles the ajax action to push a post to Apple News.
  *
  * @access public
  */
 public function ajax_push_post()
 {
     // Check the nonce
     check_ajax_referer(self::ACTION);
     // Check capabilities
     if (!current_user_can(apply_filters('apple_news_publish_capability', 'manage_options'))) {
         echo json_encode(array('success' => false, 'error' => __('You do not have permission to publish to Apple News', 'apple-news')));
         wp_die();
     }
     // Sanitize input data
     $id = absint($_GET['id']);
     // Ensure the post exists and that it's published
     $post = get_post($id);
     if (empty($post)) {
         echo json_encode(array('success' => false, 'error' => __('This post no longer exists.', 'apple-news')));
         wp_die();
     }
     if ('publish' != $post->post_status) {
         echo json_encode(array('success' => false, 'error' => sprintf(__('Article %s is not published and cannot be pushed to Apple News.', 'apple-news'), $id)));
         wp_die();
     }
     $action = new Apple_Actions\Index\Push($this->settings, $id);
     try {
         $errors = $action->perform();
     } catch (\Apple_Actions\Action_Exception $e) {
         $errors = $e->getMessage();
     }
     if ($errors) {
         echo json_encode(array('success' => false, 'error' => $errors));
     } else {
         echo json_encode(array('success' => true));
     }
     // This is required to terminate immediately and return a valid response
     wp_die();
 }