/** * The AJAX function for the 'Fetch Feed Items' row action on the * 'All Feed Sources' page. * * @since 3.3 */ function wprss_fetch_feeds_action_hook() { if (isset($_POST['id']) && !empty($_POST['id'])) { $id = $_POST['id']; update_post_meta($id, 'wprss_force_next_fetch', '1'); // Prepare the schedule args $schedule_args = array(strval($id)); // Get the current schedule - do nothing if not scheduled $next_scheduled = wp_next_scheduled('wprss_fetch_single_feed_hook', $schedule_args); if ($next_scheduled !== FALSE) { // If scheduled, unschedule it wp_unschedule_event($next_scheduled, 'wprss_fetch_single_feed_hook', $schedule_args); // Get the interval option for the feed source $interval = get_post_meta($id, 'wprss_update_interval', TRUE); // if the feed source uses its own interval if ($interval !== '' && $interval !== wprss_get_default_feed_source_update_interval()) { // Add meta in feed source. This is used to notify the source that it needs to reschedule it update_post_meta($id, 'wprss_reschedule_event', $next_scheduled); } } // Schedule the event for 5 seconds from now wp_schedule_single_event(time() + 1, 'wprss_fetch_single_feed_hook', $schedule_args); die; } }
/** * Renders the Feed Processing metabox * * @since 3.7 */ function wprss_feed_processing_meta_box_callback() { global $post; // Get the post meta $state = get_post_meta($post->ID, 'wprss_state', TRUE); $activate = get_post_meta($post->ID, 'wprss_activate_feed', TRUE); $pause = get_post_meta($post->ID, 'wprss_pause_feed', TRUE); $update_interval = get_post_meta($post->ID, 'wprss_update_interval', TRUE); $age_limit = get_post_meta($post->ID, 'wprss_age_limit', FALSE); $age_unit = get_post_meta($post->ID, 'wprss_age_unit', FALSE); $age_limit = count($age_limit) === 0 ? wprss_get_general_setting('limit_feed_items_age') : $age_limit[0]; $age_unit = count($age_unit) === 0 ? wprss_get_general_setting('limit_feed_items_age_unit') : $age_unit[0]; // Set default strings for activate and pause times $default_activate = 'immediately'; $default_pause = 'never'; // Prepare the states $states = array('active' => __('Active', 'wprss'), 'paused' => __('Paused', 'wprss')); // Prepare the schedules $default_interval = __('Default', 'wprss'); $wprss_schedules = apply_filters('wprss_schedules', wprss_get_schedules()); $default_interval_key = wprss_get_default_feed_source_update_interval(); $schedules = array_merge(array($default_interval_key => array('display' => $default_interval, 'interval' => $default_interval)), $wprss_schedules); ?> <div class="wprss-meta-side-setting"> <label for="wprss_state">Feed state:</label> <select id="wprss_state" name="wprss_state"> <?php foreach ($states as $value => $label) { ?> <option value="<?php echo $value; ?> " <?php selected($state, $value); ?> ><?php echo $label; ?> </option> <?php } ?> </select> </div> <div class="wprss-meta-side-setting"> <p> <label for="">Activate feed: </label> <strong id="wprss-activate-feed-viewer"><?php echo $activate !== '' ? $activate : $default_activate; ?> </strong> <a href="#">Edit</a> </p> <div class="wprss-meta-slider" data-collapse-viewer="wprss-activate-feed-viewer" data-default-value="<?php echo $default_activate; ?> "> <input id="wprss_activate_feed" class="wprss-datetimepicker-from-today" name="wprss_activate_feed" value="<?php echo $activate; ?> " /> <label class="description" for="wprss_activate_feed"> Leave blank to activate the feed immediately. </label> <br/><br/> <span class="description"> <b>Note:</b> WordPress uses UTC time for schedules, not local time. Current UTC time is: <code><?php echo date('d/m/Y H:i:s', current_time('timestamp', 1)); ?> </code> </span> </div> </div> <div class="wprss-meta-side-setting"> <p> <label for="">Pause feed: </label> <strong id="wprss-pause-feed-viewer"><?php echo $pause !== '' ? $pause : $default_pause; ?> </strong> <a href="#">Edit</a> </p> <div class="wprss-meta-slider" data-collapse-viewer="wprss-pause-feed-viewer" data-default-value="<?php echo $default_pause; ?> "> <input id="wprss_pause_feed" class="wprss-datetimepicker-from-today" name="wprss_pause_feed" value="<?php echo $pause; ?> " /> <label class="description" for="wprss_pause_feed"> Leave blank to never pause the feed. </label> <br/><br/> <span class="description"> <b>Note:</b> WordPress uses UTC time for schedules, not local time. Current UTC time is: <code><?php echo date('d/m/Y H:i:s', current_time('timestamp', 1)); ?> </code> </span> </div> </div> <div class="wprss-meta-side-setting"> <p> <label for="">Update interval: </label> <strong id="wprss-feed-update-interval-viewer"> <?php if ($update_interval === '' || $update_interval === wprss_get_default_feed_source_update_interval()) { echo $default_interval; } else { echo wprss_interval($schedules[$update_interval]['interval']); } ?> </strong> <a href="#">Edit</a> </p> <div class="wprss-meta-slider" data-collapse-viewer="wprss-feed-update-interval-viewer" data-default-value="<?php echo $default_interval; ?> "> <select id="feed-update-interval" name="wprss_update_interval"> <?php foreach ($schedules as $value => $schedule) { ?> <?php $text = $value === wprss_get_default_feed_source_update_interval() ? $default_interval : wprss_interval($schedule['interval']); ?> <option value="<?php echo $value; ?> " <?php selected($update_interval, $value); ?> ><?php echo $text; ?> </option> <?php } ?> </select> <br/> <span class='description' for='feed-update-interval'> Enter the interval at which to update this feed. The feed will only be updated if it is <strong>active</strong>. </span> </div> </div> <div class="wprss-meta-side-setting"> <p> <label id="wprss-age-limit-feed-label" for="" data-when-empty="Delete old feed items:">Delete feed items older than: </label> <strong id="wprss-age-limit-feed-viewer"><?php echo $age_limit . ' ' . $age_unit; ?> </strong> <a href="#">Edit</a> </p> <div class="wprss-meta-slider" data-collapse-viewer="wprss-age-limit-feed-viewer" data-label="#wprss-age-limit-feed-label" data-default-value="" data-empty-controller="#limit-feed-items-age" data-hybrid="#limit-feed-items-age, #limit-feed-items-age-unit"> <input id="limit-feed-items-age" name="wprss_age_limit" type="number" min="0" class="wprss-number-roller" placeholder="No limit" value="<?php echo $age_limit; ?> " /> <select id="limit-feed-items-age-unit" name="wprss_age_unit"> <?php foreach (wprss_age_limit_units() as $unit) { ?> <option value="<?php echo $unit; ?> " <?php selected($age_unit, $unit); ?> ><?php echo $unit; ?> </option> <?php } ?> </select> <br/> <span class='description' for='limit-feed-items-age'> Enter the maximum age of feed items to be stored in the database. Feed items older than the specified age will be deleted everyday at midnight. <br/> Leave empty for no limit. </span> </div> </div> <?php }
/** * Starts the looping schedule for a feed source. Runs on a schedule * * @param $feed_id The ID of the feed source * @since 3.9 */ function wprss_feed_source_update_start_schedule($feed_id) { // Stop any currently scheduled update operations wprss_feed_source_update_stop_schedule($feed_id); // Prepare the schedule $schedule_args = array(strval($feed_id)); // Get the interval $interval = get_post_meta($feed_id, 'wprss_update_interval', TRUE); // Do nothing if the feed source has no update interval (not sure if possible) or if the interval // is set to global if ($interval === '' || $interval === wprss_get_default_feed_source_update_interval()) { return; } wp_schedule_event(time(), $interval, 'wprss_fetch_single_feed_hook', $schedule_args); }
/** * Fetches all feed items from all feed sources. * Iteratively calls 'wprss_fetch_insert_single_feed_items' for all feed sources. * * This function is used by the cron job or the debugging functions to get all feeds from all feed sources * * @param $all If set to TRUE, the function will pull from all feed sources, regardless of their individual * update interval. If set to FALSE, only feed sources using the global update system will be updated. * (Optional) Default: TRUE. * @since 3.0 */ function wprss_fetch_insert_all_feed_items($all = TRUE) { // Get all feed sources $feed_sources = wprss_get_all_feed_sources(); if ($feed_sources->have_posts()) { // Start by getting one feed source, we will cycle through them one by one, // fetching feed items and adding them to the database in each pass while ($feed_sources->have_posts()) { $feed_sources->the_post(); $interval = get_post_meta(get_the_ID(), 'wprss_update_interval', TRUE); $using_global_interval = $interval === wprss_get_default_feed_source_update_interval() || $interval === ''; // Check if fetching from all, or if feed source uses the global interval if ($all === TRUE || $using_global_interval) { wp_schedule_single_event(time(), 'wprss_fetch_single_feed_hook', array(get_the_ID())); } } wp_reset_postdata(); // Restore the $post global to the current post in the main query } }
/** * */ function wprss_feed_source_updates() { $response = array(); if (!current_user_can('edit_feed_sources')) { return $response; } if (empty($_POST['wprss_heartbeat'])) { return $response; } // Get the wprss heartbeat data and extract the data $wprss_heartbeat = $_POST['wprss_heartbeat']; extract($wprss_heartbeat); // Perform the action specified by the heartbeat data switch ($action) { /* FEED SOURCE UPDATING STATUS * Used to determine whether or not to show the updating icon in the feed source table. */ case 'feed_sources': // Prepare array of IDs for feed sources currently updating $feed_sources_data = array(); // Iterate all feed sources foreach ($params as $feed_id) { $feed_sources_data[$feed_id] = array(); $feed_source_data =& $feed_sources_data[$feed_id]; // Check if the feed source is updating $seconds_for_next_update = wprss_get_next_feed_source_update($feed_id) - time(); $feed_source_data['updating'] = $seconds_for_next_update < 2 && $seconds_for_next_update > 0 || wprss_is_feed_source_updating($feed_id) || wprss_is_feed_source_deleting($feed_id); // Add the number of imported items $items = wprss_get_feed_items_for_source($feed_id); $feed_source_data['items'] = $items->post_count; // Update the meta field update_post_meta($feed_id, 'wprss_items_imported', $items->post_count); // Add the next update time $next_update = wprss_get_next_feed_source_update($feed_id); $update_interval = get_post_meta($feed_id, 'wprss_update_interval', TRUE); // If using the global interval, get the timestamp of the next global update if ($update_interval === wprss_get_default_feed_source_update_interval() || $update_interval === '') { $next_update = wp_next_scheduled('wprss_fetch_all_feeds_hook', array()); } // Set the text appropriately if (!wprss_is_feed_source_active($feed_id)) { $feed_source_data['next-update'] = __('Paused', WPRSS_TEXT_DOMAIN); } elseif ($next_update === FALSE) { $feed_source_data['next-update'] = __('None', WPRSS_TEXT_DOMAIN); } else { $feed_source_data['next-update'] = human_time_diff($next_update, time()); } // Update the meta field update_post_meta($feed_id, 'wprss_next_update', $feed_source_data['next-update']); // Add the last update information $last_update = get_post_meta($feed_id, 'wprss_last_update', TRUE); $last_update_items = get_post_meta($feed_id, 'wprss_last_update_items', TRUE); $feed_source_data['last-update'] = $last_update === '' ? '' : human_time_diff($last_update, time()); $feed_source_data['last-update-imported'] = $last_update_items; // Add any error info $errors = get_post_meta($feed_id, 'wprss_error_last_import', true); $feed_source_data['errors'] = $errors; } // Send back all the IDs $response['wprss_feed_sources_data'] = $feed_sources_data; break; } // Return the response die(json_encode($response)); }
/** * Renders the Feed Processing metabox * * @since 3.7 */ function wprss_feed_processing_meta_box_callback() { global $post; // Get the post meta $state = get_post_meta($post->ID, 'wprss_state', TRUE); $activate = get_post_meta($post->ID, 'wprss_activate_feed', TRUE); $pause = get_post_meta($post->ID, 'wprss_pause_feed', TRUE); $update_interval = get_post_meta($post->ID, 'wprss_update_interval', TRUE); $age_limit = get_post_meta($post->ID, 'wprss_age_limit', FALSE); $age_unit = get_post_meta($post->ID, 'wprss_age_unit', FALSE); $age_limit = count($age_limit) === 0 ? wprss_get_general_setting('limit_feed_items_age') : $age_limit[0]; $age_unit = count($age_unit) === 0 ? wprss_get_general_setting('limit_feed_items_age_unit') : $age_unit[0]; // Set default strings for activate and pause times $default_activate = 'immediately'; $default_pause = 'never'; // Prepare the states $states = array('active' => __('Active', WPRSS_TEXT_DOMAIN), 'paused' => __('Paused', WPRSS_TEXT_DOMAIN)); // Prepare the schedules $default_interval = __('Default', WPRSS_TEXT_DOMAIN); $wprss_schedules = apply_filters('wprss_schedules', wprss_get_schedules()); $default_interval_key = wprss_get_default_feed_source_update_interval(); $schedules = array_merge(array($default_interval_key => array('display' => $default_interval, 'interval' => $default_interval)), $wprss_schedules); // Inline help $help = WPRSS_Help::get_instance(); $help_options = array('tooltip_handle_class_extra' => $help->get_options('tooltip_handle_class_extra') . ' ' . $help->get_options('tooltip_handle_class') . '-side'); ?> <div class="wprss-meta-side-setting"> <label for="wprss_state">Feed state:</label> <select id="wprss_state" name="wprss_state"> <?php foreach ($states as $value => $label) { ?> <option value="<?php echo $value; ?> " <?php selected($state, $value); ?> ><?php echo $label; ?> </option> <?php } ?> </select> <?php echo $help->tooltip('field_wprss_state', null, $help_options); ?> </div> <div class="wprss-meta-side-setting"> <p> <label for="">Activate feed: </label> <strong id="wprss-activate-feed-viewer"><?php echo $activate !== '' ? $activate : $default_activate; ?> </strong> <a href="#">Edit</a> <?php echo $help->tooltip('field_wprss_activate_feed', null, $help_options); ?> </p> <div class="wprss-meta-slider" data-collapse-viewer="wprss-activate-feed-viewer" data-default-value="<?php echo $default_activate; ?> "> <input id="wprss_activate_feed" class="wprss-datetimepicker-from-today" name="wprss_activate_feed" value="<?php echo $activate; ?> " /> <span class="description"> Current UTC time is:<br/><code><?php echo date('d/m/Y H:i:s', current_time('timestamp', 1)); ?> </code> </span> </div> </div> <div class="wprss-meta-side-setting"> <p> <label for="">Pause feed: </label> <strong id="wprss-pause-feed-viewer"><?php echo $pause !== '' ? $pause : $default_pause; ?> </strong> <a href="#">Edit</a> <?php echo $help->tooltip('field_wprss_pause_feed', null, $help_options); ?> </p> <div class="wprss-meta-slider" data-collapse-viewer="wprss-pause-feed-viewer" data-default-value="<?php echo $default_pause; ?> "> <input id="wprss_pause_feed" class="wprss-datetimepicker-from-today" name="wprss_pause_feed" value="<?php echo $pause; ?> " /> <span class="description"> Current UTC time is:<br/><code><?php echo date('d/m/Y H:i:s', current_time('timestamp', 1)); ?> </code> </span> </div> </div> <div class="wprss-meta-side-setting"> <p> <label for="">Update interval: </label> <strong id="wprss-feed-update-interval-viewer"> <?php if ($update_interval === '' || $update_interval === wprss_get_default_feed_source_update_interval()) { echo $default_interval; } else { echo wprss_interval($schedules[$update_interval]['interval']); } ?> </strong> <a href="#">Edit</a> <?php echo $help->tooltip('field_wprss_update_interval', null, $help_options); ?> </p> <div class="wprss-meta-slider" data-collapse-viewer="wprss-feed-update-interval-viewer" data-default-value="<?php echo $default_interval; ?> "> <select id="feed-update-interval" name="wprss_update_interval"> <?php foreach ($schedules as $value => $schedule) { ?> <?php $text = $value === wprss_get_default_feed_source_update_interval() ? $default_interval : wprss_interval($schedule['interval']); ?> <option value="<?php echo $value; ?> " <?php selected($update_interval, $value); ?> ><?php echo $text; ?> </option> <?php } ?> </select> </div> </div> <div class="wprss-meta-side-setting"> <p> <label id="wprss-age-limit-feed-label" for="" data-when-empty="Delete old feed items:">Delete feed items older than: </label> <strong id="wprss-age-limit-feed-viewer"><?php echo $age_limit . ' ' . $age_unit; ?> </strong> <a href="#">Edit</a> <?php echo $help->tooltip('field_wprss_age_limit', null, $help_options); ?> </p> <div class="wprss-meta-slider" data-collapse-viewer="wprss-age-limit-feed-viewer" data-label="#wprss-age-limit-feed-label" data-default-value="" data-empty-controller="#limit-feed-items-age" data-hybrid="#limit-feed-items-age, #limit-feed-items-age-unit"> <input id="limit-feed-items-age" name="wprss_age_limit" type="number" min="0" class="wprss-number-roller" placeholder="No limit" value="<?php echo $age_limit; ?> " /> <select id="limit-feed-items-age-unit" name="wprss_age_unit"> <?php foreach (wprss_age_limit_units() as $unit) { ?> <option value="<?php echo $unit; ?> " <?php selected($age_unit, $unit); ?> ><?php echo $unit; ?> </option> <?php } ?> </select> </div> </div> <?php }
/** * Show up the custom columns for the wprss_feed list * * @since 2.0 */ function wprss_show_custom_columns($column, $post_id) { switch ($column) { case 'url': $url = get_post_meta($post_id, 'wprss_url', true); echo '<a href="' . esc_url($url) . '">' . esc_url($url) . '</a>'; break; case 'description': $description = get_post_meta($post_id, 'wprss_description', true); echo esc_html($description); break; case 'id': echo esc_html($post_id); break; case 'state': $active = wprss_is_feed_source_active($post_id); $text = $active ? 'Active' : 'Paused'; $button = $active ? 'Pause this feed source' : 'Activate this feed source'; $icon = $active ? 'pause' : 'play'; $value = $active ? 'paused' : 'active'; $indicator = $active ? 'green' : 'grey'; ?> <p> <span class="wprss-indicator-<?php echo $indicator; ?> " title="<?php echo $text; ?> "> <i class="fa fa-circle"></i> </span> <input type="hidden" name="wprss-redirect" value="1" /> <button type="submit" class='button-secondary' title="<?php echo $button; ?> " name="wprss-feed-id" value="<?php echo $post_id; ?> "> <i class='fa fa-<?php echo $icon; ?> '></i> </button> </p> <?php break; case 'next-update': $interval = get_post_meta($post_id, 'wprss_update_interval', TRUE); $timestamp = wprss_get_next_feed_source_update($post_id); // If using the global interval, get the timestamp of the next glboal update if ($interval === wprss_get_default_feed_source_update_interval() || $interval === '') { $timestamp = wp_next_scheduled('wprss_fetch_all_feeds_hook', array()); } ?> <p> <code> <?php if (!wprss_is_feed_source_active($post_id)) { ?> Paused <?php } elseif ($timestamp === FALSE) { ?> None <?php } else { ?> <?php echo human_time_diff($timestamp, time()); ?> <?php } ?> </code> </p> <?php break; case 'feed-count': $items = wprss_get_feed_items_for_source($post_id); echo '<p>' . $items->post_count . '</p>'; break; } }
/** * Calculates difference between times * * Taken from the WP-Crontrol plugin * @link http://wordpress.org/extend/plugins/wp-crontrol/ * @since 3.0 * */ function wprss_interval($since) { if ($since === wprss_get_default_feed_source_update_interval()) { return __('Default', WPRSS_TEXT_DOMAIN); } // array of time period chunks $chunks = array(array(60 * 60 * 24 * 365, _n_noop('%s year', '%s years', 'crontrol')), array(60 * 60 * 24 * 30, _n_noop('%s month', '%s months', 'crontrol')), array(60 * 60 * 24 * 7, _n_noop('%s week', '%s weeks', 'crontrol')), array(60 * 60 * 24, _n_noop('%s day', '%s days', 'crontrol')), array(60 * 60, _n_noop('%s hour', '%s hours', 'crontrol')), array(60, _n_noop('%s minute', '%s minutes', 'crontrol')), array(1, _n_noop('%s second', '%s seconds', 'crontrol'))); if ($since <= 0) { return __('now', WPRSS_TEXT_DOMAIN); } // we only want to output two chunks of time here, eg: // x years, xx months // x days, xx hours // so there's only two bits of calculation below: // step one: the first chunk for ($i = 0, $j = count($chunks); $i < $j; $i++) { $seconds = $chunks[$i][0]; $name = $chunks[$i][1]; // finding the biggest chunk (if the chunk fits, break) if (($count = floor($since / $seconds)) != 0) { break; } } // set output var $output = sprintf(_n($name[0], $name[1], $count, WPRSS_TEXT_DOMAIN), $count); // step two: the second chunk if ($i + 1 < $j) { $seconds2 = $chunks[$i + 1][0]; $name2 = $chunks[$i + 1][1]; if (($count2 = floor(($since - $seconds * $count) / $seconds2)) != 0) { // add to output var $output .= ' ' . sprintf(_n($name2[0], $name2[1], $count2, WPRSS_TEXT_DOMAIN), $count2); } } return $output; }