wordpress-post-widget.php
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Name: Display Recent WordPress Posts Widget |
| 4 | * Description: Displays recent posts from a WordPress.com or Jetpack-enabled self-hosted WordPress site. |
| 5 | * Version: 1.0 |
| 6 | * Author: Brad Angelcyk, Kathryn Presner, Justin Shreve, Carolyn Sonnek |
| 7 | * Author URI: https://automattic.com |
| 8 | * License: GPL2 |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * Disable direct access/execution to/of the widget code. |
| 13 | */ |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | require dirname( __FILE__ ) . '/wordpress-post-widget/class.jetpack-display-posts-widget-base.php'; |
| 19 | require dirname( __FILE__ ) . '/wordpress-post-widget/class.jetpack-display-posts-widget.php'; |
| 20 | |
| 21 | add_action( 'widgets_init', 'jetpack_display_posts_widget' ); |
| 22 | function jetpack_display_posts_widget() { |
| 23 | register_widget( 'Jetpack_Display_Posts_Widget' ); |
| 24 | } |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * Cron tasks |
| 29 | */ |
| 30 | |
| 31 | add_filter( 'cron_schedules', 'jetpack_display_posts_widget_cron_intervals' ); |
| 32 | |
| 33 | /** |
| 34 | * Adds 10 minute running interval to the cron schedules. |
| 35 | * |
| 36 | * @param array $current_schedules Currently defined schedules list. |
| 37 | * |
| 38 | * @return array |
| 39 | */ |
| 40 | function jetpack_display_posts_widget_cron_intervals( $current_schedules ) { |
| 41 | |
| 42 | /** |
| 43 | * Only add the 10 minute interval if it wasn't already set. |
| 44 | */ |
| 45 | if ( ! isset( $current_schedules['minutes_10'] ) ) { |
| 46 | $current_schedules['minutes_10'] = array( |
| 47 | 'interval' => 10 * MINUTE_IN_SECONDS, |
| 48 | 'display' => 'Every 10 minutes', |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | return $current_schedules; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Execute the cron task |
| 57 | */ |
| 58 | add_action( 'jetpack_display_posts_widget_cron_update', 'jetpack_display_posts_update_cron_action' ); |
| 59 | function jetpack_display_posts_update_cron_action() { |
| 60 | $widget = new Jetpack_Display_Posts_Widget(); |
| 61 | $widget->cron_task(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Handle activation procedures for the cron. |
| 66 | * |
| 67 | * `updating_jetpack_version` - Handle cron activation when Jetpack gets updated. It's here |
| 68 | * to cover the first cron activation after the update. |
| 69 | * |
| 70 | * `jetpack_activate_module_widgets` - Activate the cron when the Extra Sidebar widgets are activated. |
| 71 | * |
| 72 | * `activated_plugin` - Activate the cron when Jetpack gets activated. |
| 73 | * |
| 74 | */ |
| 75 | add_action( 'updating_jetpack_version', 'jetpack_display_posts_widget_conditionally_activate_cron' ); |
| 76 | add_action( 'jetpack_activate_module_widgets', 'Jetpack_Display_Posts_Widget::activate_cron' ); |
| 77 | add_action( 'activated_plugin', 'jetpack_conditionally_activate_cron_on_plugin_activation' ); |
| 78 | |
| 79 | /** |
| 80 | * Executed when Jetpack gets activated. Tries to activate the cron if it is needed. |
| 81 | * |
| 82 | * @param string $plugin_file_name The plugin file that was activated. |
| 83 | */ |
| 84 | function jetpack_conditionally_activate_cron_on_plugin_activation( $plugin_file_name ) { |
| 85 | if ( plugin_basename( JETPACK__PLUGIN_FILE ) === $plugin_file_name ) { |
| 86 | jetpack_display_posts_widget_conditionally_activate_cron(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Activates the cron only when needed. |
| 92 | * @see Jetpack_Display_Posts_Widget::should_cron_be_running |
| 93 | */ |
| 94 | function jetpack_display_posts_widget_conditionally_activate_cron() { |
| 95 | $widget = new Jetpack_Display_Posts_Widget(); |
| 96 | if ( $widget->should_cron_be_running() ) { |
| 97 | $widget->activate_cron(); |
| 98 | } |
| 99 | |
| 100 | unset( $widget ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * End of cron activation handling. |
| 105 | */ |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Handle deactivation procedures where they are needed. |
| 110 | * |
| 111 | * If Extra Sidebar Widgets module is deactivated, the cron is not needed. |
| 112 | * |
| 113 | * If Jetpack is deactivated, the cron is not needed. |
| 114 | */ |
| 115 | add_action( 'jetpack_deactivate_module_widgets', 'Jetpack_Display_Posts_Widget::deactivate_cron_static' ); |
| 116 | register_deactivation_hook( plugin_basename( JETPACK__PLUGIN_FILE ), 'Jetpack_Display_Posts_Widget::deactivate_cron_static' ); |
| 117 |