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