Completed
Push — unify/wordpress-post-widget ( 9319c3 )
by
unknown
09:34
created

Jetpack_Display_Posts_Widget::parse_posts_response()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 9

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 1
dl 26
loc 26
rs 8.5806
c 0
b 0
f 0
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
/**
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.php';
19
20
add_action( 'widgets_init', 'jetpack_display_posts_widget' );
21
function jetpack_display_posts_widget() {
22
	register_widget( 'Jetpack_Display_Posts_Widget' );
23
}
24
25
26
/**
27
 * Cron tasks
28
 */
29
30
add_filter( 'cron_schedules', 'jetpack_display_posts_widget_cron_intervals' );
31
32
/**
33
 * Adds 10 minute running interval to the cron schedules.
34
 *
35
 * @param array $current_schedules Currently defined schedules list.
36
 *
37
 * @return array
38
 */
39 View Code Duplication
function jetpack_display_posts_widget_cron_intervals( $current_schedules ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
41
	/**
42
	 * Only add the 10 minute interval if it wasn't already set.
43
	 */
44
	if ( ! isset( $current_schedules['minutes_10'] ) ) {
45
		$current_schedules['minutes_10'] = array(
46
			'interval' => 10 * MINUTE_IN_SECONDS,
47
			'display'  => 'Every 10 minutes'
48
		);
49
	}
50
51
	return $current_schedules;
52
}
53
54
/**
55
 * Execute the cron task
56
 */
57
add_action( 'jetpack_display_posts_widget_cron_update', 'jetpack_display_posts_update_cron_action' );
58
function jetpack_display_posts_update_cron_action() {
59
	$widget = new Jetpack_Display_Posts_Widget();
60
	$widget->cron_task();
61
}
62
63
/**
64
 * Handle activation procedures for the cron.
65
 *
66
 * `updating_jetpack_version` - Handle cron activation when Jetpack gets updated. It's here
67
 *                              to cover the first cron activation after the update.
68
 *
69
 * `jetpack_activate_module_widgets` - Activate the cron when the Extra Sidebar widgets are activated.
70
 *
71
 * `activated_plugin` - Activate the cron when Jetpack gets activated.
72
 *
73
 */
74
add_action( 'updating_jetpack_version', 'jetpack_display_posts_widget_conditionally_activate_cron' );
75
add_action( 'jetpack_activate_module_widgets', 'Jetpack_Display_Posts_Widget::activate_cron' );
76
add_action( 'activated_plugin', 'jetpack_conditionally_activate_cron_on_plugin_activation' );
77
78
/**
79
 * Executed when Jetpack gets activated. Tries to activate the cron if it is needed.
80
 *
81
 * @param string $plugin_file_name The plugin file that was activated.
82
 */
83
function jetpack_conditionally_activate_cron_on_plugin_activation( $plugin_file_name ) {
84
	if ( plugin_basename( JETPACK__PLUGIN_FILE ) === $plugin_file_name ) {
85
		jetpack_display_posts_widget_conditionally_activate_cron();
86
	}
87
}
88
89
/**
90
 * Activates the cron only when needed.
91
 * @see Jetpack_Display_Posts_Widget::should_cron_be_running
92
 */
93
function jetpack_display_posts_widget_conditionally_activate_cron() {
94
	$widget = new Jetpack_Display_Posts_Widget();
95
	if ( $widget->should_cron_be_running() ) {
96
		$widget->activate_cron();
97
	}
98
99
	unset( $widget );
100
}
101
102
/**
103
 * End of cron activation handling.
104
 */
105
106
107
/**
108
 * Handle deactivation procedures where they are needed.
109
 *
110
 * If Extra Sidebar Widgets module is deactivated, the cron is not needed.
111
 *
112
 * If Jetpack is deactivated, the cron is not needed.
113
 */
114
add_action( 'jetpack_deactivate_module_widgets', 'Jetpack_Display_Posts_Widget::deactivate_cron_static' );
115
register_deactivation_hook( plugin_basename( JETPACK__PLUGIN_FILE ), 'Jetpack_Display_Posts_Widget::deactivate_cron_static' );
116