Completed
Push — add/jetpack-assistant-ui ( a6f776...33ce41 )
by Jeremy
202:03 queued 191:10
created

Jetpack_Beta_Admin::update_action_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * Handles the Jetpack Admin functions.
4
 *
5
 * @package Jetpack Beta
6
 */
7
8
/** Class Jetpack_Beta_Admin */
9
class Jetpack_Beta_Admin {
10
11
	/** Initialize admin hooks. */
12
	public static function init() {
13
		add_action( 'admin_menu', array( __CLASS__, 'add_actions' ), 998 );
14
		add_action( 'network_admin_menu', array( __CLASS__, 'add_actions' ), 998 );
15
		add_action( 'admin_notices', array( __CLASS__, 'render_banner' ) );
16
	}
17
18
	/** Attach hooks common to all Jetpack admin pages. */
19
	public static function add_actions() {
20
		$hook = self::get_page_hook();
21
		add_action( "load-$hook", array( __CLASS__, 'admin_page_load' ) );
22
		add_action( "admin_print_styles-$hook", array( __CLASS__, 'admin_styles' ) );
23
		add_action( "admin_print_scripts-$hook", array( __CLASS__, 'admin_scripts' ) );
24
		add_filter( 'plugin_action_links_' . JPBETA__PLUGIN_FOLDER . '/jetpack-beta.php', array( __CLASS__, 'admin_plugin_settings_link' ) );
25
	}
26
27
	/** Get page hook */
28
	public static function get_page_hook() {
29
		if ( Jetpack_Beta::is_network_active() && ! is_network_admin() ) {
30
			return;
31
		}
32
		if ( class_exists( 'Jetpack' ) ) {
33
			return add_submenu_page(
34
				'jetpack',
35
				'Jetpack Beta',
36
				'Jetpack Beta',
37
				'update_plugins',
38
				'jetpack-beta',
39
				array( __CLASS__, 'render' )
40
			);
41
		}
42
43
		return add_menu_page(
44
			'Jetpack Beta',
45
			'Jetpack Beta',
46
			'update_plugins',
47
			'jetpack-beta',
48
			array( __CLASS__, 'render' )
49
		);
50
	}
51
52
	/** Always grab and render the latest version. */
53
	public static function render() {
54
		Jetpack_Beta::get_beta_manifest( true );
55
		require_once JPBETA__PLUGIN_DIR . 'admin/main.php';
56
	}
57
58
	/** Return the beta plugin's settings link. */
59
	public static function settings_link() {
60
		return admin_url( 'admin.php?page=jetpack-beta' );
61
	}
62
63
	/**
64
	 * Create the beta plugin's settings link.
65
	 *
66
	 * @param array $links the link being clicked.
67
	 */
68
	public static function admin_plugin_settings_link( $links ) {
69
		$settings_link = '<a href="' . esc_url( self::settings_link() ) . '">' . __( 'Settings', 'jetpack-beta' ) . '</a>';
70
		array_unshift( $links, $settings_link );
71
		return $links;
72
	}
73
74
	/** Handles Beta plugin admin page. */
75
	public static function admin_page_load() {
76
		if ( ! isset( $_GET['_nonce'] ) ) {
77
			return;
78
		}
79
		// Install and activate Jetpack Version.
80 View Code Duplication
		if ( wp_verify_nonce( $_GET['_nonce'], 'activate_branch' ) && isset( $_GET['activate-branch'] ) && isset( $_GET['section'] ) ) {
81
			$branch  = esc_html( $_GET['activate-branch'] );
82
			$section = esc_html( $_GET['section'] );
83
84
			Jetpack_Beta::install_and_activate( $branch, $section );
85
		}
86
87
		// Update to the latest version.
88 View Code Duplication
		if ( wp_verify_nonce( $_GET['_nonce'], 'update_branch' ) && isset( $_GET['update-branch'] ) && isset( $_GET['section'] ) ) {
89
			$branch  = esc_html( $_GET['update-branch'] );
90
			$section = esc_html( $_GET['section'] );
91
92
			Jetpack_Beta::update_plugin( $branch, $section );
93
		}
94
95
		// Toggle autoupdates.
96
		if ( self::is_toggle_action( 'autoupdates' ) ) {
97
			$autoupdate = (bool) Jetpack_Beta::is_set_to_autoupdate();
98
			update_option( 'jp_beta_autoupdate', (int) ! $autoupdate );
99
100
			if ( Jetpack_Beta::is_set_to_autoupdate() ) {
101
				Jetpack_Beta::maybe_schedule_autoupdate();
102
			}
103
		}
104
105
		// Toggle email notifications.
106
		if ( self::is_toggle_action( 'email_notifications' ) ) {
107
			$enable_email_notifications = (bool) Jetpack_Beta::is_set_to_email_notifications();
108
			update_option( 'jp_beta_email_notifications', (int) ! $enable_email_notifications );
109
		}
110
		wp_safe_redirect( Jetpack_Beta::admin_url() );
111
112
		exit();
113
	}
114
115
	/**
116
	 * Checks if autoupdates and email notifications are toggled.
117
	 *
118
	 * @param string $option - Which option is being toggled.
119
	 */
120
	public static function is_toggle_action( $option ) {
121
		return (
122
			isset( $_GET['_nonce'] ) &&
123
			wp_verify_nonce( $_GET['_nonce'], 'enable_' . $option ) &&
124
			isset( $_GET['_action'] ) &&
125
			'toggle_enable_' . $option === $_GET['_action']
126
		);
127
	}
128
129
	/** Render beta plugin banner */
130
	public static function render_banner() {
131
		global $current_screen;
132
133
		if ( 'plugins' !== $current_screen->base ) {
134
			return;
135
		}
136
137
		if ( Jetpack_Beta::get_option() ) {
138
			return;
139
		}
140
141
		self::start_notice();
142
	}
143
144
	/** Enqueue admin styling from admin.css */
145
	public static function admin_styles() {
146
		wp_enqueue_style( 'jetpack-beta-admin', plugins_url( 'admin/admin.css', JPBETA__PLUGIN_FILE ), array(), JPBETA_VERSION );
147
	}
148
149
	/** Enqueue scripts from admin.js */
150
	public static function admin_scripts() {
151
		wp_enqueue_script( 'jetpack-admin-js', plugins_url( 'admin/admin.js', JPBETA__PLUGIN_FILE ), array(), JPBETA_VERSION, true );
152
		wp_localize_script(
153
			'jetpack-admin-js',
154
			'JetpackBeta',
155
			array(
156
				'activate'   => __( 'Activate', 'jetpack-beta' ),
157
				'activating' => __( 'Activating...', 'jetpack-beta' ),
158
				'updating'   => __( 'Updating...', 'jetpack-beta' ),
159
				'leaving'    => __( 'Don\'t go Plugin is still installing!', 'jetpack-beta' ),
160
			)
161
		);
162
	}
163
164
	/** Determine what we're going to test (pr, master, rc) */
165
	public static function to_test_content() {
166
		list( $branch, $section ) = Jetpack_Beta::get_branch_and_section();
167
		switch ( $section ) {
168
			case 'pr':
169
				return self::to_test_pr_content( $branch );
170
			case 'master':
171
			case 'rc':
172
				return self::to_test_file_content();
173
		}
174
		return null;
175
	}
176
177
	/** Return testing instructions for release candidate branch */
178
	public static function to_test_file_content() {
179
		$test_file = WP_PLUGIN_DIR . '/' . Jetpack_Beta::get_plugin_slug() . '/to-test.md';
180
		if ( ! file_exists( $test_file ) ) {
181
			return;
182
		}
183
		WP_Filesystem();
184
		global $wp_filesystem;
185
		$content = $wp_filesystem->get_contents( $test_file );
186
		return self::render_markdown( $content );
187
	}
188
189
	/**
190
	 * Get PR information for what we want to test
191
	 *
192
	 * @param string $branch_key The branch we're switching to.
193
	 * */
194
	public static function to_test_pr_content( $branch_key ) {
195
		$manifest = Jetpack_Beta::get_beta_manifest();
196
		$pr       = isset( $manifest->pr->{$branch_key}->pr ) ? $manifest->pr->{$branch_key}->pr : null;
197
198
		if ( ! $pr ) {
199
			return null;
200
		}
201
		$github_info = Jetpack_Beta::get_remote_data( JETPACK_GITHUB_API_URL . 'pulls/' . $pr, 'github_' . $pr );
202
203
		return self::render_markdown( $github_info->body );
204
	}
205
206
	/**
207
	 * Rendering markdown for testing instructions
208
	 *
209
	 * @param string $content - Content from testing instructions for the branch we're testing.
210
	 */
211
	public static function render_markdown( $content ) {
212
213
		add_filter( 'jetpack_beta_test_content', 'wptexturize' );
214
		add_filter( 'jetpack_beta_test_content', 'convert_smilies' );
215
		add_filter( 'jetpack_beta_test_content', 'convert_chars' );
216
		add_filter( 'jetpack_beta_test_content', 'wpautop' );
217
		add_filter( 'jetpack_beta_test_content', 'shortcode_unautop' );
218
		add_filter( 'jetpack_beta_test_content', 'prepend_attachment' );
219
220
		if ( ! function_exists( 'jetpack_require_lib' ) ) {
221
			return apply_filters( 'jetpack_beta_test_content', $content );
222
		}
223
224
		jetpack_require_lib( 'markdown' );
225
		if ( ! class_exists( 'WPCom_Markdown' ) ) {
226
			require_once WP_PLUGIN_DIR . '/' . Jetpack_Beta::get_plugin_slug() . '/modules/markdown/easy-markdown.php';
227
		}
228
		$rendered_html = WPCom_Markdown::get_instance()->transform(
229
			$content,
230
			array(
231
				'id'      => false,
232
				'unslash' => false,
233
			)
234
		);
235
236
		// Lets convert #hash numbers into links to issues.
237
		$rendered_html = preg_replace( '/\#([0-9]+)/', '<a href="https://github.com/Automattic/jetpack/issues/$1">#$1</a>', $rendered_html );
238
239
		$rendered_html = apply_filters( 'jetpack_beta_test_content', $rendered_html );
240
241
		return $rendered_html;
242
	}
243
244
	/** Display the body of the start notice on the Jetpack Beta admin page */
245
	public static function start_notice() {
246
		global $current_screen;
247
248
		$is_notice = ( 'plugins' === $current_screen->base ? true : false );
249
		?>
250
		<style type="text/css">
251
			#jetpack-beta-tester__start {
252
				background: #FFF;
253
				padding: 20px;
254
				margin-top:20px;
255
				box-shadow: 0 0 0 1px rgba(200, 215, 225, 0.5), 0 1px 2px #e9eff3;
256
				position: relative;
257
			}
258
			#jetpack-beta-tester__start.updated {
259
				border-left: 3px solid #8CC258;
260
			}
261
			#jetpack-beta-tester__start h1 {
262
				font-weight: 400;
263
				margin: 0;
264
				font-size: 20px;
265
			}
266
			#jetpack-beta-tester__start p {
267
				margin-bottom:1em;
268
			}
269
		</style>
270
		<div id="jetpack-beta-tester__start" class="dops-card <?php echo ( $is_notice ? 'updated' : '' ); ?> ">
271
			<h1><?php esc_html_e( 'Welcome to Jetpack Beta Tester', 'jetpack-beta' ); ?></h1>
272
			<p><?php esc_html_e( 'Thank you for helping to test Jetpack!  We appreciate your time and effort.', 'jetpack-beta' ); ?></p>
273
			<p>
274
			<?php
275
			echo wp_kses_post(
276
				__(
277
					'When you select a Jetpack branch to test, Jetpack Beta Tester will install and activate it on your behalf and keep it up to date.
278
					When you are finished testing, you can switch back to the current version of Jetpack by selecting <em>Latest Stable</em>.',
279
					'jetpack-beta'
280
				)
281
			);
282
			?>
283
				</p>
284
			<p>
285
			<?php
286
			echo wp_kses_post(
287
				printf(
288
					// Translators: link to Jetack master testing doc in Github.
289
					__( 'Not sure where to start?  If you select <em>Bleeding Edge</em>, you\'ll get <a href="%1$s">all the cool new features</a> we\'re planning to ship in our next release.', 'jetpack-beta' ),
290
					esc_url( 'https://github.com/Automattic/jetpack/blob/master/to-test.md' )
291
				)
292
			);
293
			?>
294
			</p>
295
			<?php if ( $is_notice ) { ?>
296
			<a href="<?php echo esc_url( Jetpack_Beta::admin_url() ); ?>"><?php esc_html_e( 'Let\'s get testing!', 'jetpack-beta' ); ?></a>
297
			<?php } ?>
298
299
		</div>
300
		<?php
301
	}
302
303
	/**
304
	 * Handles branch selection on beta plugin's admin page
305
	 *
306
	 * @param string $header Title of the branch.
307
	 * @param string $branch_key Specifies which branch.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $branch not be object|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
308
	 * @param object $branch Contains branch data (title, update date, download link, commit, etc).
309
	 * @param string $section The kind of branch we're switching to (stable, rc, master, pr).
0 ignored issues
show
Documentation introduced by
Should the type for parameter $section not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
310
	 * @param bool   $is_last Last branch in the list.
311
	 */
312
	public static function show_branch( $header, $branch_key, $branch = null, $section = null, $is_last = false ) {
313
		if ( ! is_object( $branch ) ) {
314
			$manifest = Jetpack_Beta::get_beta_manifest();
315
			if ( empty( $manifest->{$section} ) ) {
316
				return;
317
			}
318
			$branch = $manifest->{$section};
319
		}
320
321
		$is_compact = $is_last ? '' : 'is-compact';
322
		$more_info  = '';
323
		$pr         = '';
324
		if ( isset( $branch->pr ) && is_int( $branch->pr ) ) {
325
			$pr = sprintf( 'data-pr="%s"', esc_attr( $branch->pr ) );
326
			// translators: Translates the `More info` link.
327
			$more_info = sprintf( __( '<a target="_blank" rel="external noopener noreferrer" href="%1$s">more info #%2$s</a> - ', 'jetpack-beta' ), Jetpack_Beta::get_url( $branch_key, $section ), $branch->pr );
328
		}
329
330
		$update_time = ( isset( $branch->update_date )
331
			// translators: %s is how long ago the branch was updated.
332
			? sprintf( __( 'last updated %s ago', 'jetpack-beta' ), human_time_diff( strtotime( $branch->update_date ) ) )
333
			: ''
334
		);
335
336
		$branch_class                             = 'branch-card';
337
		list( $current_branch, $current_section ) = Jetpack_Beta::get_branch_and_section();
338 View Code Duplication
		if ( $current_branch === $branch_key && $current_section === $section ) {
339
			$action       = __( 'Active', 'jetpack-beta' );
340
			$branch_class = 'branch-card-active';
341
		} else {
342
			$action = self::activate_button( $branch_key, $section );
343
		}
344
		$header = str_replace( '-', ' ', $header );
345
		$header = str_replace( '_', ' / ', $header );
346
		?>
347
		<div <?php echo esc_attr( $pr ); ?> " class="dops-foldable-card <?php echo esc_attr( $branch_class ); ?> has-expanded-summary dops-card <?php echo esc_attr( $is_compact ); ?>">
348
			<div class="dops-foldable-card__header has-border" >
349
				<span class="dops-foldable-card__main">
350
					<div class="dops-foldable-card__header-text">
351
						<div class="dops-foldable-card__header-text branch-card-header"><?php echo esc_html( $header ); ?></div>
352
						<div class="dops-foldable-card__subheader">
353
						<?php
354
							echo wp_kses_post( $more_info );
355
							echo wp_kses_post( $update_time );
356
						?>
357
						</div>
358
					</div>
359
				</span>
360
				<span class="dops-foldable-card__secondary">
361
					<span class="dops-foldable-card__summary">
362
						<?php echo wp_kses_post( $action ); ?>
363
					</span>
364
				</span>
365
			</div>
366
		</div>
367
		<?php
368
	}
369
370
	/**
371
	 * Handles list of available Jetpack tags to select specific Jetpack version number.
372
	 *
373
	 * @param string $header Title of tag.
374
	 * @param string $tag Jetpack tag (for selecting a specific version of Jetpack).
375
	 * @param string $url Download link for Jetpack version.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $url not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
376
	 * @param string $section The kind of version we're switching to (in this case 'tags').
0 ignored issues
show
Documentation introduced by
Should the type for parameter $section not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
377
	 * @param bool   $is_last last version in the list.
378
	 */
379
	public static function show_tag( $header, $tag, $url = null, $section = null, $is_last = false ) {
380
		$is_compact = $is_last ? '' : 'is-compact';
381
		if ( isset( $url ) ) {
382
			$data_tag = sprintf( 'data-tag="%s"', $tag );
383
		}
384
385
		$class_name                               = 'tag-card';
386
		list( $current_branch, $current_section ) = Jetpack_Beta::get_branch_and_section();
387 View Code Duplication
		if ( $current_branch === $tag && $current_section === $section ) {
388
			$action     = __( 'Active', 'jetpack-beta' );
389
			$class_name = 'tag-card-active';
390
		} else {
391
			$action = self::activate_button( $tag, $section );
392
		}
393
394
		$header = str_replace( '-', ' ', $header );
395
		$header = str_replace( '_', ' / ', $header );
396
		?>
397
		<div <?php echo wp_kses_post( $data_tag ); ?> " class="dops-foldable-card <?php echo esc_attr( $class_name ); ?> has-expanded-summary dops-card <?php echo esc_attr( $is_compact ); ?>">
0 ignored issues
show
Bug introduced by
The variable $data_tag does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
398
			<div class="dops-foldable-card__header has-border">
399
				<span class="dops-foldable-card__main">
400
					<div class="dops-foldable-card__header-text">
401
						<div class="dops-foldable-card__header-text tag-card-header">Jetpack <?php echo esc_html( $header ); ?></div>
402
						<div class="dops-foldable-card__subheader">
403
						<?php
404
						sprintf(
405
							// Translators: Which release is being selected.
406
							__( 'Public release (%1$s) <a href="https://plugins.trac.wordpress.org/browser/jetpack/tags/%2$s" target="_blank" rel="">available on WordPress.org</a>', 'jetpack-beta' ),
407
							esc_html( $tag ),
408
							esc_attr( $tag )
409
						);
410
						?>
411
						</div>
412
					</div>
413
				</span>
414
				<span class="dops-foldable-card__secondary">
415
					<span class="dops-foldable-card__summary">
416
						<?php echo wp_kses_post( $action ); ?>
417
					</span>
418
				</span>
419
			</div>
420
		</div>
421
		<?php
422
	}
423
424
	/**
425
	 * Handles the activation buttons.
426
	 *
427
	 * @param object $branch specifies which branch.
428
	 * @param string $section The kind of branch we're switching to (stable, rc, master, pr).
429
	 */
430
	public static function activate_button( $branch, $section ) {
431
		if ( is_object( $section ) && 'master' === $branch ) {
432
			$section = 'master';
433
		}
434
435
		if ( is_object( $section ) && 'rc' === $branch ) {
436
			$section = 'rc';
437
		}
438
		$query = array(
439
			'page'            => 'jetpack-beta',
440
			'activate-branch' => $branch,
441
			'section'         => $section,
442
			'_nonce'          => wp_create_nonce( 'activate_branch' ),
443
		);
444
		$url   = Jetpack_Beta::admin_url( '?' . build_query( $query ) );
445
446
		return sprintf(
447
			'<a href="%1$s" class="is-primary jp-form-button activate-branch dops-button is-compact jptracks" data-jptracks-name="%2$s" data-jptracks-prop="%3$s">%4$s</a>',
448
			esc_url( $url ),
449
			'jetpack_beta_activate_branch',
450
			esc_attr( $branch ),
451
			esc_html__( 'Activate', 'jetpack-beta' )
452
		);
453
	}
454
455
	/**
456
	 * Display the branch header
457
	 *
458
	 * @param string $title - The title of the branch.
459
	 */
460
	public static function header( $title ) {
461
		echo '<header><h2 class="jp-jetpack-connect__container-subtitle">' . esc_html( $title ) . '</h2></header>';
462
	}
463
464
	/**
465
	 * Display the branch list
466
	 *
467
	 * @param string $section - The kind of branch we're switching to (stable, rc, master, pr).
468
	 * @param string $title - The title of the branch.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $title not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
469
	 */
470 View Code Duplication
	public static function show_branches( $section, $title = null ) {
471
		if ( $title ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $title of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
472
			$title .= ': ';
473
		}
474
		echo '<div id="section-' . esc_attr( $section ) . '">';
475
476
		$manifest = Jetpack_Beta::get_beta_manifest();
477
		$count    = 0;
478
		if ( empty( $manifest->{$section} ) ) {
479
			return;
480
		}
481
		$branches  = (array) $manifest->{$section};
482
		$count_all = count( $branches );
483
484
		foreach ( $branches as $branch_name => $branch ) {
485
			$count ++;
486
			$is_last = $count_all === $count ? true : false;
487
			self::show_branch( $title . $branch_name, $branch_name, $branch, $section, $is_last );
488
		}
489
		echo '</div>';
490
	}
491
492
	/**
493
	 * Show list of available Jetpack tags to select specific Jetpack version number.
494
	 *
495
	 * @param string $section - The kind of version we're switching to (in this case 'tags').
496
	 * @param string $title - The name of the Jetpack tag.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $title not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
497
	 */
498 View Code Duplication
	public static function show_tags( $section, $title = null ) {
499
		if ( $title ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $title of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
500
			$title .= ': ';
501
		}
502
		echo '<div id="section-' . esc_attr( $section ) . '">';
503
504
		$manifest = Jetpack_Beta::get_org_data();
505
		$count    = 0;
506
		if ( empty( $manifest->versions ) ) {
507
			return;
508
		}
509
		$tags      = array_reverse( (array) $manifest->versions );
510
		$count_all = count( $tags );
511
512
		foreach ( $tags as $tag => $url ) {
513
			$count ++;
514
			$is_last = $count_all === $count ? true : false;
515
			self::show_tag( $title . $tag, $tag, $url, $section, $is_last );
516
		}
517
		echo '</div>';
518
	}
519
520
	/** Display the stable branch */
521
	public static function show_stable_branch() {
522
		$org_data = Jetpack_Beta::get_org_data();
523
524
		self::show_branch(
525
			__( 'Latest Stable', 'jetpack-beta' ),
526
			'stable',
527
			(object) array(
528
				'branch'      => 'stable',
529
				'update_date' => $org_data->last_updated,
530
			),
531
			'stable'
532
		);
533
	}
534
535
	/** Show search bar for PRs */
536 View Code Duplication
	public static function show_search_prs() {
537
		$manifest = Jetpack_Beta::get_beta_manifest();
538
		if ( empty( $manifest->pr ) ) {
539
			return;
540
		}
541
		?>
542
		<div class="dops-navigation">
543
			<div class="dops-section-nav has-pinned-items">
544
				<div class="dops-section-nav__panel">
545
					<div class="is-pinned is-open dops-search" role="search">
546
						<div aria-controls="search-component" aria-label="<?php esc_attr_e( 'Open Search', 'jetpack-beta' ); ?>" tabindex="-1">
547
							<svg class="gridicon gridicons-search dops-search-open__icon" height="24"
548
								viewbox="0 0 24 24" width="24">
549
								<g>
550
									<path d="M21 19l-5.154-5.154C16.574 12.742 17 11.42 17 10c0-3.866-3.134-7-7-7s-7 3.134-7 7 3.134 7 7 7c1.42 0 2.742-.426 3.846-1.154L19 21l2-2zM5 10c0-2.757 2.243-5 5-5s5 2.243 5 5-2.243 5-5 5-5-2.243-5-5z"></path>
551
								</g>
552
							</svg>
553
						</div>
554
						<input aria-hidden="false" class="dops-search__input" id="search-component-prs"
555
							placeholder="<?php esc_attr_e( 'Search for a Jetpack Feature Branch', 'jetpack-beta' ); ?>" role="search" type="search" value="">
556
						<span aria-controls="search-component" id="search-component-prs-close" aria-label="<?php esc_attr_e( 'Close Search', 'jetpack-beta' ); ?>"
557
							tabindex="0">
558
							<svg class="gridicon gridicons-cross dops-search-close__icon" height="24"
559
								viewbox="0 0 24 24" width="24">
560
								<g>
561
									<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z"></path>
562
								</g>
563
							</svg>
564
						</span>
565
					</div>
566
				</div>
567
			</div>
568
		</div>
569
		<?php
570
	}
571
572
	/** Show search bar for tags */
573 View Code Duplication
	public static function show_search_org_tags() {
574
		$org_data = Jetpack_Beta::get_org_data();
575
		if ( empty( $org_data->versions ) ) {
576
			return;
577
		}
578
		?>
579
		<div class="dops-navigation">
580
			<div class="dops-section-nav has-pinned-items">
581
				<div class="dops-section-nav__panel">
582
					<div class="is-pinned is-open dops-search" role="search">
583
						<div aria-controls="search-component" aria-label="<?php esc_attr_e( 'Open Search', 'jetpack-beta' ); ?>" tabindex="-1">
584
							<svg class="gridicon gridicons-search dops-search-open__icon" height="24"
585
								viewbox="0 0 24 24" width="24">
586
								<g>
587
									<path d="M21 19l-5.154-5.154C16.574 12.742 17 11.42 17 10c0-3.866-3.134-7-7-7s-7 3.134-7 7 3.134 7 7 7c1.42 0 2.742-.426 3.846-1.154L19 21l2-2zM5 10c0-2.757 2.243-5 5-5s5 2.243 5 5-2.243 5-5 5-5-2.243-5-5z"></path>
588
								</g>
589
							</svg>
590
						</div>
591
						<input aria-hidden="false" class="dops-search__input" id="search-component-tags"
592
							placeholder="<?php esc_attr_e( 'Search for a Jetpack tag', 'jetpack-beta' ); ?>" role="search" type="search" value="">
593
						<span aria-controls="search-component" id="search-component-tags-close" aria-label="<?php esc_attr_e( 'Close Search', 'jetpack-beta' ); ?>"
594
							tabindex="0">
595
							<svg class="gridicon gridicons-cross dops-search-close__icon" height="24"
596
								viewbox="0 0 24 24" width="24">
597
								<g>
598
									<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z"></path>
599
								</g>
600
							</svg>
601
						</span>
602
					</div>
603
				</div>
604
			</div>
605
		</div>
606
		<?php
607
	}
608
609
	/** Display autoupdate toggle */
610
	public static function show_toggle_autoupdates() {
611
		$autoupdate = (bool) Jetpack_Beta::is_set_to_autoupdate();
612
		self::show_toggle( __( 'Autoupdates', 'jetpack-beta' ), 'autoupdates', $autoupdate );
613
	}
614
615
	/** Display email notification toggle */
616
	public static function show_toggle_emails() {
617
		if ( ! Jetpack_Beta::is_set_to_autoupdate() || defined( 'JETPACK_BETA_SKIP_EMAIL' ) ) {
618
			return;
619
		}
620
		$email_notification = (bool) Jetpack_Beta::is_set_to_email_notifications();
621
		self::show_toggle( __( 'Email Notifications', 'jetpack-beta' ), 'email_notifications', $email_notification );
622
	}
623
624
	/**
625
	 * Display autoupdate and email notification toggles
626
	 *
627
	 * @param string $name name of toggle.
628
	 * @param string $option Which toggle (autoupdates, email_notification).
629
	 * @param bool   $value If toggle is active or not.
630
	 */
631
	public static function show_toggle( $name, $option, $value ) {
632
		$query = array(
633
			'page'    => 'jetpack-beta',
634
			'_action' => 'toggle_enable_' . $option,
635
			'_nonce'  => wp_create_nonce( 'enable_' . $option ),
636
		);
637
638
		?>
639
		<a
640
			href="<?php echo esc_url( Jetpack_Beta::admin_url( '?' . build_query( $query ) ) ); ?>"
641
			class="form-toggle__label <?php echo ( $value ? 'is-active' : '' ); ?>"
642
			data-jptracks-name="jetpack_beta_toggle_<?php echo esc_attr( $option ); ?>"
643
			data-jptracks-prop="<?php echo absint( ! $value ); ?>"
644
		>
645
			<span class="form-toggle-explanation" ><?php echo esc_html( $name ); ?></span>
646
			<span class="form-toggle__switch" tabindex="0" ></span>
647
			<span class="form-toggle__label-content" ></span>
648
		</a>
649
		<?php
650
	}
651
652
	/** Check if Jetpack versions are up to date */
653
	public static function show_needed_updates() {
654
		// Jetpack Stable not up to date?
655
		$should_update_stable_version = Jetpack_Beta::should_update_stable_version();
656
		$should_update_dev_version    = Jetpack_Beta::should_update_dev_version();
657
		$should_update_dev_to_master  = Jetpack_Beta::should_update_dev_to_master();
658
659
		if ( ! $should_update_stable_version
660
			&& ! $should_update_dev_version
661
			&& ! $should_update_dev_to_master ) {
662
			return;
663
		}
664
		?>
665
		<div class="jetpack-beta__wrap jetpack-beta__update-needed">
666
			<h2><?php esc_html_e( 'Some updates are required', 'jetpack-beta' ); ?></h2>
667
		<?php
668
669 View Code Duplication
		if ( $should_update_stable_version ) {
670
			self::update_card(
671
				__( 'Latest Stable', 'jetpack-beta' ),
672
				__( 'Needs an update', 'jetpack-beta' ),
673
				self::update_action_url( 'stable', 'stable' )
674
			);
675
		}
676
		// Jetpack Dev Folder not up to date?
677
		if ( $should_update_dev_version ) {
678
			list( $dev_branch, $dev_section ) = Jetpack_Beta::get_branch_and_section_dev();
679
			self::update_card(
680
				Jetpack_Beta::get_jetpack_plugin_pretty_version( true ),
681
				__( 'Is not running the latest version', 'jetpack-beta' ),
682
				self::update_action_url( $dev_branch, $dev_section )
683
			);
684
		}
685
686 View Code Duplication
		if ( $should_update_dev_to_master ) {
687
			self::update_card(
688
				__( 'Feature Branch was merged', 'jetpack-beta' ),
689
				__( 'Go back to Jetpack\'s Bleeding Edge version.', 'jetpack-beta' ),
690
				self::update_action_url( 'master', 'master' )
691
			);
692
		}
693
		?>
694
		</div>
695
		<?php
696
	}
697
698
	/**
699
	 * Handles card that notifies when there's an update available on a branch.
700
	 *
701
	 * @param string $header - Title of the branch that's ready for update.
702
	 * @param string $sub_header - Detailed information about the update.
703
	 * @param string $url - URL where branch can be updated.
704
	 */
705
	public static function update_card( $header, $sub_header, $url ) {
706
		?>
707
		<div class="dops-foldable-card has-expanded-summary dops-card is-compact">
708
			<div class="dops-foldable-card__header has-border" >
709
				<span class="dops-foldable-card__main">
710
					<div class="dops-foldable-card__header-text">
711
						<div class="dops-foldable-card__header-text branch-card-header"><?php echo esc_html( $header ); ?></div>
712
						<div class="dops-foldable-card__subheader"><?php echo esc_html( $sub_header ); ?></div>
713
					</div>
714
				</span>
715
				<span class="dops-foldable-card__secondary">
716
					<span class="dops-foldable-card__summary">
717
						<a
718
							href="<?php echo esc_url( $url ); ?>"
719
							class="is-primary jp-form-button activate-branch dops-button is-compact"><?php esc_html_e( 'Update', 'jetpack-beta' ); ?></a>
720
					</span>
721
				</span>
722
			</div>
723
		</div>
724
		<?php
725
	}
726
727
	/**
728
	 * Handles update button for branches
729
	 *
730
	 * @param string $branch - Branch that's ready for update.
731
	 * @param string $section - What kind of branch we're updated (master, rc, pr).
732
	 */
733
	public static function update_action_url( $branch, $section ) {
734
		$query = array(
735
			'page'          => 'jetpack-beta',
736
			'update-branch' => $branch,
737
			'section'       => $section,
738
			'_nonce'        => wp_create_nonce( 'update_branch' ),
739
		);
740
741
		return Jetpack_Beta::admin_url( '?' . build_query( $query ) );
742
	}
743
}
744