/** * 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'); }
/** * Shows an error message. * * @param string $message * @access public */ private function notice_error($message) { Admin_Apple_Notice::error($message); wp_safe_redirect(menu_page_url($this->plugin_slug . '_index', false)); exit; }
/** * When a post is deleted, remove it from Apple News. * * @since 0.4.0 * @param int $id * @access public */ public function do_delete($id) { if (!current_user_can(apply_filters('apple_news_delete_capability', 'manage_options'))) { return; } // If it does not have a remote API ID just ignore if (!get_post_meta($id, 'apple_news_api_id', true)) { return; } $action = new Apple_Actions\Index\Delete($this->settings, $id); try { $action->perform(); } catch (Apple_Actions\Action_Exception $e) { Admin_Apple_Notice::error($e->getMessage()); } }
/** * Shows an error message. * * @param string $message * @access public */ private function notice_error($message) { Admin_Apple_Notice::error($message); $this->do_redirect(); }
/** * Processes errors, halts publishing if needed. * * @param array $errors * @access private */ private function process_errors($errors) { // Get the current alert settings $component_alerts = $this->get_setting('component_alerts'); $json_alerts = $this->get_setting('json_alerts'); // Initialize the alert message $alert_message = ''; // Get the current user id if (empty($user_id)) { $user_id = get_current_user_id(); } // Build the component alert error message, if required if (!empty($errors[0]['component_errors'])) { // Build an list of the components that caused errors $component_names = implode(', ', $errors[0]['component_errors']); if ('warn' === $component_alerts) { $alert_message .= sprintf(__('The following components are unsupported by Apple News and were removed: %s', 'apple-news'), $component_names); } elseif ('fail' === $component_alerts) { $alert_message .= sprintf(__('The following components are unsupported by Apple News and prevented publishing: %s', 'apple-news'), $component_names); } } // Check for JSON errors if (!empty($errors[0]['json_errors'])) { if (!empty($alert_message)) { $alert_message .= '|'; } // Merge all errors into a single message $json_errors = implode(', ', $errors[0]['json_errors']); // Add these to the message if ('warn' === $json_alerts) { $alert_message .= sprintf(__('The following JSON errors were detected: %s', 'apple-news'), $json_errors); } elseif ('fail' === $json_alerts) { $alert_message .= sprintf(__('The following JSON errors were detected and prevented publishing: %s', 'apple-news'), $json_errors); } } // See if we found any errors if (empty($alert_message)) { return; } // Proceed based on component alert settings if ('fail' === $component_alerts && !empty($errors[0]['component_errors']) || 'fail' === $json_alerts && !empty($errors[0]['json_errors'])) { // 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'); // Clean the workspace $this->clean_workspace(); // Throw an exception throw new \Apple_Actions\Action_Exception($alert_message); } else { if ('warn' === $component_alerts && !empty($errors[0]['component_errors']) || 'warn' === $json_alerts && !empty($errors[0]['json_errors'])) { \Admin_Apple_Notice::error($alert_message, $user_id); } } }
/** * Add the Apple News publish meta box * * @since 0.9.0 * @param WP_Post $post * @access public */ public function publish_meta_box($post) { // Only show the publish feature if the user is authorized and auto sync is not enabled. // Also check if the post has been previously published and/or deleted. $api_id = get_post_meta($post->ID, 'apple_news_api_id', true); $deleted = get_post_meta($post->ID, 'apple_news_api_deleted', true); $pending = get_post_meta($post->ID, 'apple_news_api_pending', true); $is_preview = get_post_meta($post->ID, 'apple_news_is_preview', true); $pullquote = get_post_meta($post->ID, 'apple_news_pullquote', true); $pullquote_position = get_post_meta($post->ID, 'apple_news_pullquote_position', true); // Set the default value if (empty($pullquote_position)) { $pullquote_position = 'middle'; } ?> <div id="apple-news-publish"> <?php wp_nonce_field($this->publish_action, 'apple_news_nonce'); ?> <?php $section = new Apple_Actions\Index\Section($this->settings); try { $sections = $section->get_sections(); } catch (Apple_Actions\Action_Exception $e) { Admin_Apple_Notice::error($e->getMessage()); } if (!empty($sections)) { ?> <h3><?php esc_html_e('Sections', 'apple-news'); ?> </h3> <?php self::build_sections_field($sections, $post->ID); } ?> <p class="description"><?php esc_html_e('Select the sections in which to publish this article. Uncheck them all for a standalone article.', 'apple-news'); ?> </p> <h3><?php esc_html_e('Preview?', 'apple-news'); ?> </h3> <input id="apple-news-is-preview" name="apple_news_is_preview" type="checkbox" value="1" <?php checked($is_preview); ?> > <p class="description"><?php esc_html_e('Check this to publish the article as a draft.', 'apple-news'); ?> </p> <h3><?php esc_html_e('Pull quote', 'apple-news'); ?> </h3> <textarea name="apple_news_pullquote" placeholder="<?php esc_attr_e('A pull quote is a key phrase, quotation, or excerpt that has been pulled from an article and used as a graphic element, serving to entice readers into the article or to highlight a key topic.', 'apple-news'); ?> " rows="6" class="large-text"><?php echo esc_textarea($pullquote); ?> </textarea> <p class="description"><?php esc_html_e('This is optional and can be left blank.', 'apple-news'); ?> </p> <h3><?php esc_html_e('Pull quote position', 'apple-news'); ?> </h3> <select name="apple_news_pullquote_position"> <option <?php selected($pullquote_position, 'top'); ?> value="top"><?php esc_html_e('top', 'apple-news'); ?> </option> <option <?php selected($pullquote_position, 'middle'); ?> value="middle"><?php esc_html_e('middle', 'apple-news'); ?> </option> <option <?php selected($pullquote_position, 'bottom'); ?> value="bottom"><?php esc_html_e('bottom', 'apple-news'); ?> </option> </select> <p class="description"><?php esc_html_e('The position in the article where the pull quote will appear.', 'apple-news'); ?> </p> <?php if ('yes' != $this->settings->get('api_autosync') && current_user_can(apply_filters('apple_news_publish_capability', 'manage_options')) && 'publish' === $post->post_status && empty($api_id) && empty($deleted) && empty($pending)) { ?> <input type="hidden" id="apple-news-publish-action" name="apple_news_publish_action" value=""> <input type="button" id="apple-news-publish-submit" name="apple_news_publish_submit" value="<?php esc_attr_e('Publish to Apple News', 'apple-news'); ?> " class="button-primary" /> <?php } elseif ('yes' == $this->settings->get('api_autosync') && empty($api_id) && empty($deleted) && empty($pending)) { ?> <p><?php esc_html_e('This post will be automatically sent to Apple News on publish.', 'apple-news'); ?> </p> <?php } elseif ('yes' == $this->settings->get('api_async') && !empty($pending)) { ?> <p><?php esc_html_e('This post is currently pending publishing to Apple News.', 'apple-news'); ?> </p> <?php } // Add data about the article if it exists if (!empty($deleted)) { ?> <p><b><?php esc_html_e('This post has been deleted from Apple News', 'apple-news'); ?> </b></p> <?php } if (!empty($api_id)) { $state = \Admin_Apple_News::get_post_status($post->ID); $share_url = get_post_meta($post->ID, 'apple_news_api_share_url', true); $created_at = get_post_meta($post->ID, 'apple_news_api_created_at', true); $created_at = empty($created_at) ? __('None', 'apple-news') : get_date_from_gmt(date('Y-m-d H:i:s', strtotime($created_at)), 'F j, h:i a'); $modified_at = get_post_meta($post->ID, 'apple_news_api_modified_at', true); $modified_at = empty($modified_at) ? __('None', 'apple-news') : get_date_from_gmt(date('Y-m-d H:i:s', strtotime($modified_at)), 'F j, h:i a'); ?> <p><b><?php esc_html_e('Apple News Publish Information', 'apple-news'); ?> </b> <br/><?php esc_html_e('ID', 'apple-news'); ?> : <?php echo esc_html($api_id); ?> <br/><?php esc_html_e('Created at', 'apple-news'); ?> : <?php echo esc_html($created_at); ?> <br/><?php esc_html_e('Modified at', 'apple-news'); ?> : <?php echo esc_html($modified_at); ?> <br/><?php esc_html_e('Share URL', 'apple-news'); ?> : <a href="<?php echo esc_url($share_url); ?> " target="_blank"><?php echo esc_html($share_url); ?> </a> <br/><?php esc_html_e('Revision', 'apple-news'); ?> : <?php echo esc_html(get_post_meta($post->ID, 'apple_news_api_revision', true)); ?> <br/><?php esc_html_e('State', 'apple-news'); ?> : <?php echo esc_html($state); ?> <?php } ?> </div> <?php }