Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_Beta often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Beta, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Jetpack_Beta { |
||
17 | |||
18 | /** |
||
19 | * Singleton Jetpack_Beta class instance. |
||
20 | * |
||
21 | * @var Jetpack_Beta |
||
22 | */ |
||
23 | protected static $instance = null; |
||
24 | |||
25 | /** |
||
26 | * WP Options string: jetpack_beta_active |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected static $option = 'jetpack_beta_active'; |
||
31 | |||
32 | /** |
||
33 | * WP Options string: jetpack_beta_dev_currently_installed |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected static $option_dev_installed = 'jetpack_beta_dev_currently_installed'; |
||
38 | |||
39 | /** |
||
40 | * WP Options string: jp_beta_autoupdate |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected static $option_autoupdate = 'jp_beta_autoupdate'; |
||
45 | |||
46 | /** |
||
47 | * WP Options string: jp_beta_email_notifications |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected static $option_email_notif = 'jp_beta_email_notifications'; |
||
52 | |||
53 | /** |
||
54 | * WP-Cron string: jetpack_beta_autoupdate_hourly_cron |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected static $auto_update_cron_hook = 'jetpack_beta_autoupdate_hourly_cron'; |
||
59 | |||
60 | /** |
||
61 | * Main Instance |
||
62 | */ |
||
63 | public static function instance() { |
||
70 | |||
71 | /** |
||
72 | * Constructor |
||
73 | */ |
||
74 | public function __construct() { |
||
75 | add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'maybe_plugins_update_transient' ) ); |
||
76 | add_filter( 'upgrader_post_install', array( $this, 'upgrader_post_install' ), 10, 3 ); |
||
77 | |||
78 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ) ); |
||
79 | add_action( 'deactivate_plugin', array( $this, 'plugin_deactivated' ), 10, 2 ); |
||
80 | |||
81 | add_action( 'upgrader_process_complete', array( $this, 'upgrader_process_complete' ), 10, 2 ); |
||
82 | |||
83 | add_filter( 'plugin_action_links_' . JETPACK_PLUGIN_FILE, array( $this, 'remove_activate_stable' ) ); |
||
84 | add_filter( 'plugin_action_links_' . JETPACK_DEV_PLUGIN_FILE, array( $this, 'remove_activate_dev' ) ); |
||
85 | |||
86 | add_filter( 'network_admin_plugin_action_links_' . JETPACK_PLUGIN_FILE, array( $this, 'remove_activate_stable' ) ); |
||
87 | add_filter( 'network_admin_plugin_action_links_' . JETPACK_DEV_PLUGIN_FILE, array( $this, 'remove_activate_dev' ) ); |
||
88 | |||
89 | add_filter( 'all_plugins', array( $this, 'update_all_plugins' ) ); |
||
90 | |||
91 | add_filter( 'plugins_api', array( $this, 'get_plugin_info' ), 10, 3 ); |
||
92 | |||
93 | add_action( 'jetpack_beta_autoupdate_hourly_cron', array( 'Jetpack_Beta', 'run_autoupdate' ) ); |
||
94 | |||
95 | add_filter( 'jetpack_options_whitelist', array( $this, 'add_to_options_whitelist' ) ); |
||
96 | |||
97 | if ( is_admin() ) { |
||
98 | require JPBETA__PLUGIN_DIR . 'class-jetpack-beta-admin.php'; |
||
99 | self::maybe_schedule_autoupdate(); |
||
100 | Jetpack_Beta_Admin::init(); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Fired when the upgrader process is complete; sets option jetpack_beta_dev_currently_installed |
||
106 | * |
||
107 | * @param WP_Upgrader $upgrader - An upgrader instance. |
||
108 | * @param array $updates_completed - Array of bulk item update data. |
||
109 | */ |
||
110 | public function upgrader_process_complete( $upgrader, $updates_completed ) { |
||
111 | if ( ! isset( $updates_completed['plugins'] ) ) { |
||
112 | return; |
||
113 | } |
||
114 | |||
115 | if ( 'update' === $updates_completed['action'] && |
||
116 | 'plugin' === $updates_completed['type'] && |
||
117 | in_array( JETPACK_DEV_PLUGIN_FILE, $updates_completed['plugins'], true ) ) { |
||
118 | list( $branch, $section ) = self::get_branch_and_section_dev(); |
||
119 | if ( self::should_update_dev_to_master() ) { |
||
120 | list( $branch, $section ) = array( 'master', 'master' ); |
||
121 | } |
||
122 | update_option( self::$option_dev_installed, array( $branch, $section, self::get_manifest_data( $branch, $section ) ) ); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * If Jetpack or JP Dev plugin is network activated, update active_plugins option. |
||
128 | */ |
||
129 | public static function is_network_enabled() { |
||
130 | if ( self::is_network_active() ) { |
||
131 | add_filter( 'option_active_plugins', array( 'Jetpack_Beta', 'override_active_plugins' ) ); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * This filter is only applied if Jetpack is network activated, |
||
137 | * makes sure that you can't have Jetpack or Jetpack Dev plugins versions loaded. |
||
138 | * |
||
139 | * @param array $active_plugins - Currently activated plugins. |
||
140 | * |
||
141 | * @return array Updated array of active plugins. |
||
142 | */ |
||
143 | public static function override_active_plugins( $active_plugins ) { |
||
144 | $new_active_plugins = array(); |
||
145 | foreach ( $active_plugins as $active_plugin ) { |
||
146 | if ( ! self::is_jetpack_plugin( $active_plugin ) ) { |
||
147 | $new_active_plugins[] = $active_plugin; |
||
148 | } |
||
149 | } |
||
150 | return $new_active_plugins; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Actions taken when the Jetpack Beta plugin is deactivated. |
||
155 | * |
||
156 | * @param string $plugin - Plugin path being deactivated. |
||
157 | */ |
||
158 | public function plugin_deactivated( $plugin ) { |
||
159 | if ( ! self::is_jetpack_plugin( $plugin ) ) { |
||
160 | return; |
||
161 | } |
||
162 | |||
163 | delete_option( self::$option ); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Checks if passed plugin matches JP or JP Dev paths. |
||
168 | * |
||
169 | * @param string $plugin - A plugin path. |
||
170 | */ |
||
171 | public static function is_jetpack_plugin( $plugin ) { |
||
172 | return in_array( $plugin, array( JETPACK_PLUGIN_FILE, JETPACK_DEV_PLUGIN_FILE ), true ); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Filter JP Dev plugin action links. |
||
177 | * |
||
178 | * @param array $actions - Array of plugin action links. |
||
179 | */ |
||
180 | public function remove_activate_dev( $actions ) { |
||
181 | View Code Duplication | if ( is_plugin_active( JETPACK_PLUGIN_FILE ) || self::is_network_active() ) { |
|
182 | $actions['activate'] = __( 'Plugin Already Active', 'jetpack-beta' ); |
||
183 | } |
||
184 | return $actions; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Filter JP Stable plugin action links. |
||
189 | * |
||
190 | * @param array $actions - Array of plugin action links. |
||
191 | */ |
||
192 | public function remove_activate_stable( $actions ) { |
||
193 | View Code Duplication | if ( is_plugin_active( JETPACK_DEV_PLUGIN_FILE ) || self::is_network_active() ) { |
|
194 | $actions['activate'] = __( 'Plugin Already Active', 'jetpack-beta' ); |
||
195 | } |
||
196 | return $actions; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Filters plugins to list in the Plugins list table. |
||
201 | * |
||
202 | * @param array $plugins - Array of arrays of plugin data. |
||
203 | * |
||
204 | * @return array Updated array of plugin data. |
||
205 | */ |
||
206 | public function update_all_plugins( $plugins ) { |
||
207 | // WP.com requests away show regular plugin. |
||
208 | if ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) { |
||
209 | // Ensure that Jetpack reports the version it's using on account of the Jetpack Beta plugin to Calypso. |
||
210 | if ( is_plugin_active( JETPACK_DEV_PLUGIN_FILE ) ) { |
||
211 | $plugins[ JETPACK_PLUGIN_FILE ]['Version'] = $plugins[ JETPACK_DEV_PLUGIN_FILE ]['Version']; |
||
212 | } |
||
213 | unset( $plugins[ JETPACK_DEV_PLUGIN_FILE ] ); |
||
214 | return $plugins; |
||
215 | } |
||
216 | |||
217 | if ( is_plugin_active( JETPACK_DEV_PLUGIN_FILE ) ) { |
||
218 | unset( $plugins[ JETPACK_PLUGIN_FILE ] ); |
||
219 | } else { |
||
220 | unset( $plugins[ JETPACK_DEV_PLUGIN_FILE ] ); |
||
221 | } |
||
222 | return $plugins; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Filter WordPress.org Plugins API results. |
||
227 | * |
||
228 | * @param false|object|array $false - The result object or array. Default false. |
||
229 | * @param string $action - The type of information being requested from the Plugin Installation API. |
||
230 | * @param object $response - Plugin API arguments. |
||
231 | */ |
||
232 | public function get_plugin_info( $false, $action, $response ) { |
||
233 | |||
234 | // Check if this call API is for the right plugin. |
||
235 | if ( ! isset( $response->slug ) || JETPACK_DEV_PLUGIN_SLUG !== $response->slug ) { |
||
236 | return false; |
||
237 | } |
||
238 | $update_date = null; |
||
239 | $download_zip = null; |
||
240 | $dev_data = self::get_dev_installed(); |
||
241 | if ( isset( $dev_data[2] ) ) { |
||
242 | $update_date = $dev_data[2]->update_date; |
||
243 | $download_zip = $dev_data[2]->download_url; |
||
244 | } |
||
245 | // Update tags. |
||
246 | $response->slug = JETPACK_DEV_PLUGIN_SLUG; |
||
247 | $response->plugin = JETPACK_DEV_PLUGIN_SLUG; |
||
248 | $response->name = 'Jetpack | ' . self::get_jetpack_plugin_pretty_version( true ); |
||
249 | $response->plugin_name = 'Jetpack | ' . self::get_jetpack_plugin_pretty_version( true ); |
||
250 | $response->version = self::get_jetpack_plugin_version( true ); |
||
251 | $response->author = 'Automattic'; |
||
252 | $response->homepage = 'https://jetpack.com/contact-support/beta-group/'; |
||
253 | $response->downloaded = false; |
||
254 | $response->last_updated = $update_date; |
||
255 | $response->sections = array( 'description' => Jetpack_Beta_Admin::to_test_content() ); |
||
256 | $response->download_link = $download_zip; |
||
257 | return $response; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Run on activation to flush update cache. |
||
262 | */ |
||
263 | public static function activate() { |
||
264 | // Don't do anyting funnly. |
||
265 | if ( defined( 'DOING_CRON' ) ) { |
||
266 | return; |
||
267 | } |
||
268 | delete_site_transient( 'update_plugins' ); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Returns active Jetpack plugin file partial path string (jetpack/jetpack.php|jetpack-dev/jetpack.php). |
||
273 | */ |
||
274 | public static function get_plugin_file() { |
||
275 | return self::get_plugin_slug() . '/jetpack.php'; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Returns active plugin slug string (jetpack|jetpack-dev). |
||
280 | */ |
||
281 | View Code Duplication | public static function get_plugin_slug() { |
|
282 | $installed = self::get_branch_and_section(); |
||
283 | if ( empty( $installed ) || 'stable' === $installed[1] || 'tags' === $installed[1] ) { |
||
284 | return 'jetpack'; |
||
285 | } |
||
286 | return JETPACK_DEV_PLUGIN_SLUG; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Handler ran for Jetpack Beta plugin deactivation hook. |
||
291 | */ |
||
292 | public static function deactivate() { |
||
293 | // Don't do anyting funnly. |
||
294 | if ( defined( 'DOING_CRON' ) ) { |
||
295 | return; |
||
296 | } |
||
297 | |||
298 | self::clear_autoupdate_cron(); |
||
299 | self::delete_all_transiants(); |
||
300 | add_action( 'shutdown', array( __CLASS__, 'switch_active' ), 5 ); |
||
301 | add_action( 'shutdown', array( __CLASS__, 'remove_dev_plugin' ), 20 ); |
||
302 | delete_option( self::$option ); |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * When Jetpack Beta plugin is deactivated, remove the jetpack-dev plugin directory and cleanup. |
||
307 | */ |
||
308 | public static function remove_dev_plugin() { |
||
309 | if ( is_multisite() ) { |
||
310 | return; |
||
311 | } |
||
312 | |||
313 | // Delete the jetpack dev plugin. |
||
314 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
||
315 | $creds = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() ); |
||
316 | if ( ! WP_Filesystem( $creds ) ) { |
||
317 | // Any problems and we exit. |
||
318 | return; |
||
319 | } |
||
320 | global $wp_filesystem; |
||
321 | if ( ! $wp_filesystem ) { |
||
322 | return; |
||
323 | } |
||
324 | |||
325 | $working_dir = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . JETPACK_DEV_PLUGIN_SLUG; |
||
326 | // Delete the folder JETPACK_BETA_PLUGIN_FOLDER. |
||
327 | if ( $wp_filesystem->is_dir( $working_dir ) ) { |
||
328 | $wp_filesystem->delete( $working_dir, true ); |
||
329 | } |
||
330 | // Since we are removing this dev plugin we should also clean up this data. |
||
331 | delete_option( self::$option_dev_installed ); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Builds URL to the admin area for the current site and specified query param. |
||
336 | * |
||
337 | * @param string $query - Path relative to the admin URL. |
||
338 | */ |
||
339 | public static function admin_url( $query = '?page=jetpack-beta' ) { |
||
344 | |||
345 | /** |
||
346 | * Build the "Jetpack Beta" admin bar menu items. |
||
347 | */ |
||
348 | public function admin_bar_menu() { |
||
349 | global $wp_admin_bar; |
||
350 | |||
351 | if ( ! is_object( $wp_admin_bar ) ) { |
||
352 | return; |
||
353 | } |
||
354 | |||
355 | // Nothing got activated yet. |
||
356 | if ( ! self::get_option() ) { |
||
357 | return; |
||
358 | } |
||
359 | |||
402 | |||
403 | /** |
||
404 | * Filters `update_plugins` transient. |
||
405 | * |
||
406 | * @param object $transient - Plugin update data. |
||
407 | */ |
||
408 | public function maybe_plugins_update_transient( $transient ) { |
||
446 | |||
447 | /** |
||
448 | * Determine if JP dev version should be updated. |
||
449 | */ |
||
450 | public static function should_update_dev_version() { |
||
453 | |||
454 | /** |
||
455 | * Build plugin update data response for dev plugin. |
||
456 | */ |
||
457 | public static function get_jepack_dev_update_response() { |
||
467 | |||
468 | /** |
||
469 | * Build plugin update data response for JP dev master. |
||
470 | */ |
||
471 | public static function get_jepack_dev_master_update_response() { |
||
480 | |||
481 | /** |
||
482 | * Moves the newly downloaded folder into jetpack-dev. |
||
483 | * |
||
484 | * @param bool $worked - Installation response. |
||
485 | * @param array $hook_extras - Extra args passed to hooked filters. |
||
486 | * @param array $result - Installation result data. |
||
487 | * |
||
488 | * @return WP_Error |
||
489 | */ |
||
490 | public function upgrader_post_install( $worked, $hook_extras, $result ) { |
||
506 | |||
507 | /** |
||
508 | * Get the active JP or JP Dev plugin version. |
||
509 | * |
||
510 | * @param bool $is_dev_version - If dev plugin version is being queried. |
||
511 | * |
||
512 | * @return string|0 Plugin version. |
||
|
|||
513 | */ |
||
514 | public static function get_jetpack_plugin_version( $is_dev_version = false ) { |
||
523 | |||
524 | /** |
||
525 | * Get WP Option: jetpack_beta_active |
||
526 | */ |
||
527 | public static function get_option() { |
||
530 | |||
531 | /** |
||
532 | * Get WP Option: jetpack_beta_dev_currently_installed |
||
533 | */ |
||
534 | public static function get_dev_installed() { |
||
537 | |||
538 | /** |
||
539 | * Get active Jetpack branch/section. |
||
540 | */ |
||
541 | public static function get_branch_and_section() { |
||
553 | |||
554 | /** |
||
555 | * Check if Jetpack version is 'stable' version. |
||
556 | */ |
||
557 | View Code Duplication | public static function is_on_stable() { |
|
564 | |||
565 | /** |
||
566 | * Check if Jetpack active version is a tag version. |
||
567 | */ |
||
568 | public static function is_on_tag() { |
||
575 | |||
576 | /** |
||
577 | * Get active Jetpack Dev branch/section. |
||
578 | */ |
||
579 | public static function get_branch_and_section_dev() { |
||
589 | |||
590 | /** |
||
591 | * Massage JP plugin version string. |
||
592 | * |
||
593 | * @param bool $is_dev_version - If JP Dev version is being queried. |
||
594 | */ |
||
595 | public static function get_jetpack_plugin_pretty_version( $is_dev_version = false ) { |
||
633 | |||
634 | /** |
||
635 | * Fetch latest Jetpack version. |
||
636 | * |
||
637 | * @param bool $is_dev_version - If JP Dev version is being queried. |
||
638 | */ |
||
639 | public static function get_new_jetpack_version( $is_dev_version = false ) { |
||
663 | |||
664 | /** |
||
665 | * Get JP Dev plugin repo URL. |
||
666 | */ |
||
667 | public static function get_url_dev() { |
||
671 | |||
672 | /** |
||
673 | * Get JP plugin repo URL. |
||
674 | * |
||
675 | * @param string $branch - Branch. |
||
676 | * @param string $section - Section. |
||
677 | */ |
||
678 | public static function get_url( $branch = null, $section = null ) { |
||
699 | |||
700 | /** |
||
701 | * Get install URL for JP dev. |
||
702 | */ |
||
703 | public static function get_install_url_dev() { |
||
707 | |||
708 | /** |
||
709 | * Get install URL for JP. |
||
710 | * |
||
711 | * @param string $branch - Branch. |
||
712 | * @param string $section - Section. |
||
713 | */ |
||
714 | public static function get_install_url( $branch = null, $section = null ) { |
||
750 | |||
751 | /** |
||
752 | * Get stable JP version plugin data. |
||
753 | */ |
||
754 | public static function get_jetpack_plugin_info_stable() { |
||
757 | |||
758 | /** |
||
759 | * Get dev JP version plugin data. |
||
760 | */ |
||
761 | public static function get_jetpack_plugin_info_dev() { |
||
764 | |||
765 | /** |
||
766 | * Get JP plugin data. |
||
767 | * |
||
768 | * @param mixed $plugin_file - JP or JP Dev plugin path. |
||
769 | */ |
||
770 | public static function get_jetpack_plugin_info( $plugin_file = null ) { |
||
787 | |||
788 | /** |
||
789 | * Switch active JP plugin version when JP Beta plugin is deactivated. |
||
790 | * This needs to happen on `shutdown`, otherwise it doesn't work. |
||
791 | */ |
||
792 | public static function switch_active() { |
||
795 | |||
796 | /** |
||
797 | * Fetch the Jetpack beta manifest. |
||
798 | * |
||
799 | * @param bool $force_refresh - Whether to bypass cached response. |
||
800 | */ |
||
801 | public static function get_beta_manifest( $force_refresh = false ) { |
||
804 | |||
805 | /** |
||
806 | * Fetch WordPress.org Jetpack plugin info. |
||
807 | */ |
||
808 | public static function get_org_data() { |
||
811 | |||
812 | /** |
||
813 | * Helper function used to fetch remote data from WordPress.org, GitHub, and betadownload.jetpack.me |
||
814 | * |
||
815 | * @param string $url - Url being fetched. |
||
816 | * @param string $transient - Transient name (manifest|org_data|github_commits_). |
||
817 | * @param bool $bypass - Whether to bypass cached response. |
||
818 | */ |
||
819 | public static function get_remote_data( $url, $transient, $bypass = false ) { |
||
837 | |||
838 | /** |
||
839 | * Delete set transients when plugin is deactivated. |
||
840 | */ |
||
841 | public static function delete_all_transiants() { |
||
850 | |||
851 | /** |
||
852 | * Install & activate JP for the given branch/section. |
||
853 | * |
||
854 | * @param string $branch - Branch. |
||
855 | * @param string $section - Section. |
||
856 | */ |
||
857 | public static function install_and_activate( $branch, $section ) { |
||
858 | // Cleanup previous version of the beta plugin. |
||
859 | if ( file_exists( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'jetpack-pressable-beta' ) ) { |
||
860 | // Delete the Jetpack dev plugin. |
||
861 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
||
862 | $creds = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() ); |
||
863 | if ( ! WP_Filesystem( $creds ) ) { |
||
864 | // Any problems and we exit. |
||
865 | return new WP_error( 'Filesystem Problem' ); |
||
866 | } |
||
867 | global $wp_filesystem; |
||
868 | if ( ! $wp_filesystem ) { |
||
869 | return new WP_error( '$wp_filesystem is not global' ); |
||
870 | } |
||
871 | |||
872 | $working_dir = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'jetpack-pressable-beta'; |
||
873 | // Delete the folder `JETPACK_BETA_PLUGIN_FOLDER`. |
||
874 | if ( $wp_filesystem->is_dir( $working_dir ) ) { |
||
875 | $wp_filesystem->delete( $working_dir, true ); |
||
876 | } |
||
877 | // Deactivate the plugin. |
||
878 | self::replace_active_plugin( 'jetpack-pressable-beta/jetpack.php' ); |
||
879 | } |
||
880 | |||
881 | self::update_autoload_dev_constant( $section ); |
||
882 | |||
883 | View Code Duplication | if ( 'stable' === $section && |
|
884 | file_exists( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . JETPACK_PLUGIN_FILE ) ) { |
||
885 | self::replace_active_plugin( JETPACK_DEV_PLUGIN_FILE, JETPACK_PLUGIN_FILE, true ); |
||
886 | self::update_option( $branch, $section ); |
||
887 | return; |
||
888 | } |
||
889 | |||
890 | View Code Duplication | if ( self::get_branch_and_section_dev() === array( $branch, $section ) |
|
891 | && file_exists( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . JETPACK_DEV_PLUGIN_FILE ) ) { |
||
892 | self::replace_active_plugin( JETPACK_PLUGIN_FILE, JETPACK_DEV_PLUGIN_FILE, true ); |
||
893 | self::update_option( $branch, $section ); |
||
894 | return; |
||
895 | } |
||
896 | |||
897 | self::proceed_to_install_and_activate( |
||
898 | self::get_install_url( $branch, $section ), |
||
899 | self::get_plugin_slug( $section ), |
||
900 | $section |
||
901 | ); |
||
902 | self::update_option( $branch, $section ); |
||
903 | } |
||
904 | |||
905 | /** |
||
906 | * Update to the latest version. |
||
907 | * |
||
908 | * @param string $branch - Branch. |
||
909 | * @param string $section - Section. |
||
910 | */ |
||
911 | public static function update_plugin( $branch, $section ) { |
||
922 | |||
923 | /** |
||
924 | * Helper function to update installed version option. |
||
925 | * |
||
926 | * @param string $branch - Branch. |
||
927 | * @param string $section - Section. |
||
928 | */ |
||
929 | public static function update_option( $branch, $section ) { |
||
935 | |||
936 | /** |
||
937 | * Return manifest info for specififed branch/section. |
||
938 | * |
||
939 | * @param string $branch - Branch. |
||
940 | * @param string $section - Section. |
||
941 | */ |
||
942 | public static function get_manifest_data( $branch, $section ) { |
||
966 | |||
967 | /** |
||
968 | * Install specified plugin version. |
||
969 | * |
||
970 | * @param string $url - Url for plugin version. |
||
971 | * @param string $plugin_folder - Path JP or JP Dev plugin folder. |
||
972 | * @param string $section - Section. |
||
973 | */ |
||
974 | public static function proceed_to_install_and_activate( $url, $plugin_folder, $section ) { |
||
983 | |||
984 | /** |
||
985 | * Download plugin files. |
||
986 | * |
||
987 | * @param string $url - Url for plugin version. |
||
988 | * @param string $plugin_folder - Path JP or JP Dev plugin folder. |
||
989 | * @param string $section - Section. |
||
990 | */ |
||
991 | public static function proceed_to_install( $url, $plugin_folder, $section ) { |
||
1020 | |||
1021 | /** |
||
1022 | * Check if plugin is network activated. |
||
1023 | */ |
||
1024 | public static function is_network_active() { |
||
1039 | |||
1040 | /** |
||
1041 | * Swap plugin files. |
||
1042 | * |
||
1043 | * @param string $current_plugin - Current plugin path. |
||
1044 | * @param string $replace_with_plugin - Plugin path to replace with. |
||
1045 | * @param bool $force_activate - Whether to force activate plguin. |
||
1046 | */ |
||
1047 | public static function replace_active_plugin( $current_plugin, $replace_with_plugin = null, $force_activate = false ) { |
||
1078 | |||
1079 | /** |
||
1080 | * Check if `stable` should be updated. |
||
1081 | * |
||
1082 | * @return bool |
||
1083 | */ |
||
1084 | public static function should_update_stable_version() { |
||
1110 | |||
1111 | /** |
||
1112 | * Here we are checking if the DEV branch that we are currenly on is not something that is available in the manifest |
||
1113 | * Meaning that the DEV branch was merged into master and so we need to update it. |
||
1114 | * |
||
1115 | * @return bool |
||
1116 | */ |
||
1117 | public static function should_update_dev_to_master() { |
||
1126 | |||
1127 | /** |
||
1128 | * Get WP Option: jp_beta_autoupdate |
||
1129 | */ |
||
1130 | public static function is_set_to_autoupdate() { |
||
1133 | |||
1134 | /** |
||
1135 | * Get WP Option: jp_beta_email_notifications |
||
1136 | */ |
||
1137 | public static function is_set_to_email_notifications() { |
||
1140 | |||
1141 | /** |
||
1142 | * Clear scheduled WP-Cron jobs on plugin deactivation. |
||
1143 | */ |
||
1144 | public static function clear_autoupdate_cron() { |
||
1154 | |||
1155 | /** |
||
1156 | * Schedule plugin update jobs. |
||
1157 | */ |
||
1158 | public static function schedule_hourly_autoupdate() { |
||
1162 | |||
1163 | /** |
||
1164 | * Determine if plugin update jobs should be scheduled. |
||
1165 | */ |
||
1166 | public static function maybe_schedule_autoupdate() { |
||
1179 | |||
1180 | /** |
||
1181 | * Get "What changed" info for display. |
||
1182 | * |
||
1183 | * @return string|false |
||
1184 | */ |
||
1185 | public static function what_changed() { |
||
1221 | |||
1222 | /** |
||
1223 | * Get version commit if available. |
||
1224 | * |
||
1225 | * @return string|false |
||
1226 | */ |
||
1227 | public static function get_version_commit() { |
||
1234 | |||
1235 | /** |
||
1236 | * Fetch commit data from GitHub. |
||
1237 | * |
||
1238 | * @param string $commit - The commit to fetch. |
||
1239 | */ |
||
1240 | public static function get_commit_data_from_github( $commit ) { |
||
1243 | |||
1244 | /** |
||
1245 | * The jetpack_beta_autoupdate_hourly_cron job. |
||
1246 | */ |
||
1247 | public static function run_autoupdate() { |
||
1305 | |||
1306 | /** |
||
1307 | * Builds and sends an email about succesfull plugin autoupdate. |
||
1308 | * |
||
1309 | * @param Array $plugins - List of plugins that were updated. |
||
1310 | * @param String $log - Upgrade message from core's plugin upgrader. |
||
1311 | */ |
||
1312 | private static function send_autoupdate_email( $plugins, $log ) { |
||
1389 | |||
1390 | /** |
||
1391 | * This checks intends to fix errors in our build server when Jetpack. |
||
1392 | * |
||
1393 | * @param string $source - Source path. |
||
1394 | * @param string $remote_source - Remote path. |
||
1395 | * |
||
1396 | * @return WP_Error |
||
1397 | */ |
||
1398 | public static function check_for_main_files( $source, $remote_source ) { |
||
1418 | |||
1419 | /** |
||
1420 | * Checks if a dir is empty. |
||
1421 | * |
||
1422 | * @param [type] $dir The absolute directory path to check. |
||
1423 | * @return boolean |
||
1424 | */ |
||
1425 | public static function is_dir_empty( $dir ) { |
||
1428 | |||
1429 | /** |
||
1430 | * Callback function to include Jetpack beta options into Jetpack sync whitelist. |
||
1431 | * |
||
1432 | * @param Array $whitelist List of whitelisted options to sync. |
||
1433 | */ |
||
1434 | public function add_to_options_whitelist( $whitelist ) { |
||
1441 | |||
1442 | /** |
||
1443 | * Custom error handler to intercept errors and log them using Jetpack's own logger. |
||
1444 | * |
||
1445 | * @param int $errno - Error code. |
||
1446 | * @param string $errstr - Error message. |
||
1447 | * @param string $errfile - File name where the error happened. |
||
1448 | * @param int $errline - Line in the code. |
||
1449 | * |
||
1450 | * @return bool Whether to make the default handler handle the error as well. |
||
1451 | */ |
||
1452 | public static function custom_error_handler( $errno, $errstr, $errfile, $errline ) { |
||
1477 | |||
1478 | /** |
||
1479 | * Clears the autoloader transient. |
||
1480 | */ |
||
1481 | public static function clear_autoloader_plugin_cache() { |
||
1484 | |||
1485 | /** |
||
1486 | * Sets the 'jetpack_autoload_dev' option when a Jetpack version is activated: |
||
1487 | * - Sets the option to true when a development version of Jetpack is activated. |
||
1488 | * - Sets the option to false when the stable or release candidate version is activated. |
||
1489 | * |
||
1490 | * This option is used to set the JETPACK_AUTOLOAD_DEV constant in jetpack-beta.php. The constant |
||
1491 | * is used by the Jetpack autoloader to determine whether stable or development versions of |
||
1492 | * packages should be preferred. |
||
1493 | * |
||
1494 | * @param string $section The section value for the activating Jetpack version. |
||
1495 | */ |
||
1496 | public static function update_autoload_dev_constant( $section ) { |
||
1504 | } |
||
1505 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.