wordpress-post-widget.php 3.5 KB

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