Completed
Push — renovate/copy-webpack-plugin-6... ( 7623fa...eb5c64 )
by
unknown
70:27 queued 62:03
created

Jetpack_Calypsoify::jetpack_toggle_autoupdate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 31
rs 9.424
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Jetpack_Calypsoify::installed_plugins_icon() 0 5 1
1
<?php
2
/**
3
 * This is Calypso skin of the wp-admin interface that is conditionally triggered via the ?calypsoify=1 param.
4
 * Ported from an internal Automattic plugin.
5
 */
6
7
use Automattic\Jetpack\Redirect;
8
9
class Jetpack_Calypsoify {
10
11
	/**
12
	 * Singleton instance of `Jetpack_Calypsoify`.
13
	 *
14
	 * @var object
15
	 */
16
	public static $instance = false;
17
18
	/**
19
	 * Is Calypsoify enabled, based on any value of `calypsoify` user meta.
20
	 *
21
	 * @var bool
22
	 */
23
	public $is_calypsoify_enabled = false;
24
25
	private function __construct() {
26
		add_action( 'wp_loaded', array( $this, 'setup' ) );
27
	}
28
29
	public static function getInstance() {
30
		if ( ! self::$instance ) {
31
			self::$instance = new self();
32
		}
33
34
		return self::$instance;
35
	}
36
37
	public function setup() {
38
		$this->is_calypsoify_enabled = 1 == (int) get_user_meta( get_current_user_id(), 'calypsoify', true );
39
		if ( isset( $_GET['calypsoify'] ) ) {
40
			$this->is_calypsoify_enabled = 1 === (int) $_GET['calypsoify'];
41
		}
42
43
		add_action( 'admin_init', array( $this, 'check_param' ), 4 );
44
45
		if ( $this->is_calypsoify_enabled ) {
46
			add_action( 'admin_init', array( $this, 'setup_admin' ), 6 );
47
			add_action( 'admin_menu', array( $this, 'remove_core_menus' ), 100 );
48
			add_action( 'admin_menu', array( $this, 'add_custom_menus' ), 101 );
49
		}
50
	}
51
52
	public function setup_admin() {
53
		// Masterbar is currently required for this to work properly. Mock the instance of it
54
		if ( ! Jetpack::is_module_active( 'masterbar' ) ) {
55
			$this->mock_masterbar_activation();
56
		}
57
58
		if ( $this->is_page_gutenberg() ) {
59
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_for_gutenberg' ), 100 );
60
			return;
61
		}
62
63
		add_action( 'admin_init', array( $this, 'check_page' ) );
64
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 100 );
65
		add_action( 'in_admin_header', array( $this, 'insert_sidebar_html' ) );
66
		add_action( 'wp_before_admin_bar_render', array( $this, 'modify_masterbar' ), 100000 );
67
68
		add_filter( 'get_user_option_admin_color', array( $this, 'admin_color_override' ) );
69
70
		add_action( 'current_screen', array( $this, 'attach_views_filter' ) );
71
	}
72
73
	public function admin_color_override( $color ) {
74
		return 'fresh';
75
	}
76
77
	public function mock_masterbar_activation() {
78
		include_once JETPACK__PLUGIN_DIR . 'modules/masterbar/masterbar.php';
79
		new A8C_WPCOM_Masterbar;
80
	}
81
82
	public function remove_core_menus() {
83
		remove_menu_page( 'edit.php?post_type=feedback' );
84
		remove_menu_page( 'index.php' );
85
		remove_menu_page( 'jetpack' );
86
		remove_menu_page( 'edit.php' );
87
		remove_menu_page( 'upload.php' );
88
		remove_menu_page( 'edit.php?post_type=page' );
89
		remove_menu_page( 'edit-comments.php' );
90
		remove_menu_page( 'themes.php' );
91
		remove_menu_page( 'plugins.php' );
92
		remove_menu_page( 'users.php' );
93
		remove_menu_page( 'tools.php' );
94
		remove_menu_page( 'link-manager.php' );
95
96
		// Core settings pages
97
		remove_submenu_page( 'options-general.php', 'options-general.php' );
98
		remove_submenu_page( 'options-general.php', 'options-writing.php' );
99
		remove_submenu_page( 'options-general.php', 'options-reading.php' );
100
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
101
		remove_submenu_page( 'options-general.php', 'options-media.php' );
102
		remove_submenu_page( 'options-general.php', 'options-permalink.php' );
103
		remove_submenu_page( 'options-general.php', 'privacy.php' );
104
		remove_submenu_page( 'options-general.php', 'sharing' );
105
	}
106
107
	public function add_custom_menus() {
108
		global $menu, $submenu;
109
110
		if ( isset( $_GET['post_type'] ) && 'feedback' === $_GET['post_type'] ) {
111
			// there is currently no gridicon for feedback, so using dashicon.
112
			add_menu_page( __( 'Feedback', 'jetpack' ), __( 'Feedback', 'jetpack' ), 'edit_pages', 'edit.php?post_type=feedback', '', 'dashicons-feedback', 1 );
113
			remove_menu_page( 'options-general.php' );
114
			remove_submenu_page( 'edit.php?post_type=feedback', 'feedback-export' );
115
		} else {
116
			add_menu_page( __( 'Manage Plugins', 'jetpack' ), __( 'Manage Plugins', 'jetpack' ), 'activate_plugins', 'plugins.php', '', $this->installed_plugins_icon(), 1 );
117
			// Count the settings page submenus, if it's zero then don't show this.
118
			if ( empty( $submenu['options-general.php'] ) ) {
119
				remove_menu_page( 'options-general.php' );
120
			} else {
121
				// Rename and make sure the plugin settings menu is always last.
122
				// Sneaky plugins seem to override this otherwise.
123
				// Settings is always key 80.
124
				$menu[80][0]                            = __( 'Plugin Settings', 'jetpack' );
125
				$menu[ max( array_keys( $menu ) ) + 1 ] = $menu[80];
126
				unset( $menu[80] );
127
			}
128
		}
129
	}
130
131
	public function enqueue() {
132
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style.min.css', false, JETPACK__VERSION );
133
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
134
        wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );
135
136
		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods.js', false, JETPACK__VERSION );
137
		wp_localize_script( 'calypsoify_wpadminmods_js', 'CalypsoifyOpts', array(
138
			'nonces' => array(
139
				'autoupdate_plugins' => wp_create_nonce( 'jetpack_toggle_autoupdate-plugins' ),
140
				'autoupdate_plugins_translations' => wp_create_nonce( 'jetpack_toggle_autoupdate-plugins_translations' ),
141
			)
142
		) );
143
	}
144
145
	public function enqueue_for_gutenberg() {
146
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style-gutenberg.min.css', false, JETPACK__VERSION );
147
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
148
        wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );
149
150
		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods-gutenberg.js', false, JETPACK__VERSION );
151
		wp_localize_script(
152
			'calypsoify_wpadminmods_js',
153
			'calypsoifyGutenberg',
154
			array(
155
				'closeUrl'   => $this->get_close_gutenberg_url(),
156
				'manageReusableBlocksUrl' => $this->get_calypso_origin() . '/types/wp_block' . $this->get_site_suffix(),
157
			)
158
		);
159
	}
160
161
	/**
162
	 * Inserts Sidebar HTML
163
	 *
164
	 * @return void
165
	 */
166
	public function insert_sidebar_html() {
167
		$heading       = ( isset( $_GET['post_type'] ) && 'feedback' === $_GET['post_type'] ) ? __( 'Feedback', 'jetpack' ) : __( 'Plugins', 'jetpack' );
168
		$home_url = Redirect::get_url( 'calypso-home' );
169
		?>
170
		<a href="<?php echo esc_url( $home_url ); ?>" id="calypso-sidebar-header">
171
			<svg class="gridicon gridicons-chevron-left" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g><path d="M14 20l-8-8 8-8 1.414 1.414L8.828 12l6.586 6.586"></path></g></svg>
172
173
			<ul>
174
				<li id="calypso-sitename"><?php bloginfo( 'name' ); ?></li>
175
				<li id="calypso-plugins"><?php echo esc_html( $heading ); ?></li>
176
			</ul>
177
		</a>
178
		<?php
179
	}
180
181
	public function modify_masterbar() {
182
		global $wp_admin_bar;
183
184
		// Add proper links to masterbar top sections.
185
		$my_sites_node       = (object) $wp_admin_bar->get_node( 'blog' );
186
		$my_sites_node->href = Redirect::get_url( 'calypso-home' );
187
		$wp_admin_bar->add_node( $my_sites_node );
188
189
		$reader_node       = (object) $wp_admin_bar->get_node( 'newdash' );
190
		$reader_node->href = Redirect::get_url( 'calypso-read' );
191
		$wp_admin_bar->add_node( $reader_node );
192
193
		$me_node       = (object) $wp_admin_bar->get_node( 'my-account' );
194
		$me_node->href = Redirect::get_url( 'calypso-me' );
195
		$wp_admin_bar->add_node( $me_node );
196
	}
197
198
	private function installed_plugins_icon() {
199
		$svg = '<svg class="gridicon gridicons-plugins" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 24"><g><path d="M16 8V3c0-.552-.448-1-1-1s-1 .448-1 1v5h-4V3c0-.552-.448-1-1-1s-1 .448-1 1v5H5v4c0 2.79 1.637 5.193 4 6.317V22h6v-3.683c2.363-1.124 4-3.527 4-6.317V8h-3z" fill="black"></path></g></svg>';
200
201
		return 'data:image/svg+xml;base64,' . base64_encode( $svg );
202
	}
203
204
	/**
205
	 * Returns the Calypso domain that originated the current request.
206
	 *
207
	 * @return string
208
	 */
209
	private function get_calypso_origin() {
210
		$origin    = ! empty( $_GET['origin'] ) ? $_GET['origin'] : 'https://wordpress.com';
211
		$allowed = array(
212
			'http://calypso.localhost:3000',
213
			'http://127.0.0.1:41050', // Desktop App
214
			'https://wpcalypso.wordpress.com',
215
			'https://horizon.wordpress.com',
216
			'https://wordpress.com',
217
		);
218
		return in_array( $origin, $allowed, true ) ? $origin : 'https://wordpress.com';
219
220
		function get_site_suffix() {
0 ignored issues
show
Unused Code introduced by
function get_site_suffix... } return ''; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
221
			if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) {
222
				$site_suffix = Jetpack::build_raw_urls( home_url() );
223
			} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
224
				$site_suffix = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
225
			}
226
227
			if ( $site_suffix ) {
228
				return "/${site_suffix}";
229
			}
230
			return '';
231
		}
232
	}
233
234
	/**
235
	 * Returns the site slug suffix to be used as part of the Calypso URLs. It already
236
	 * includes the slash separator at the beginning.
237
	 *
238
	 * @example "https://wordpress.com/block-editor" . $this->get_site_suffix()
239
	 *
240
	 * @return string
241
	 */
242
	private function get_site_suffix() {
243
		if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) {
244
			$site_suffix = Jetpack::build_raw_urls( home_url() );
245
		} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
246
			$site_suffix = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
247
		}
248
249
		if ( $site_suffix ) {
250
			return "/${site_suffix}";
0 ignored issues
show
Bug introduced by
The variable $site_suffix 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...
251
		}
252
		return '';
253
	}
254
255
	/**
256
	 * Returns the Calypso URL that displays either the current post type list (if no args
257
	 * are supplied) or the classic editor for the current post (if a post ID is supplied).
258
	 *
259
	 * @param int|null $post_id
260
	 * @return string
261
	 */
262
	public function get_calypso_url( $post_id = null ) {
263
		$screen = get_current_screen();
264
		$post_type = $screen->post_type;
265
		if ( is_null( $post_id ) ) {
266
			// E.g. `posts`, `pages`, or `types/some_custom_post_type`
267
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
268
				? "/${post_type}s"
269
				: "/types/${post_type}";
270
			$post_suffix = '';
271
		} else {
272
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
273
				? "/${post_type}"
274
				: "/edit/${post_type}";
275
			$post_suffix = "/${post_id}";
276
		}
277
278
		return $this->get_calypso_origin() . $post_type_suffix . $this->get_site_suffix() . $post_suffix;
279
	}
280
281
	/**
282
	 * Returns the URL to be used on the block editor close button for going back to the
283
	 * Calypso post list.
284
	 *
285
	 * @return string
286
	 */
287
	public function get_close_gutenberg_url() {
288
		return $this->get_calypso_url();
289
	}
290
291
	/**
292
	 * Returns the URL for switching the user's editor to the Calypso (WordPress.com Classic) editor.
293
	 *
294
	 * @return string
295
	 */
296
	public function get_switch_to_classic_editor_url() {
297
		return add_query_arg(
298
			'set-editor',
299
			'classic',
300
			$this->is_calypsoify_enabled ? $this->get_calypso_url( get_the_ID() ) : false
301
		);
302
	}
303
304
	public function check_param() {
305
		if ( isset( $_GET['calypsoify'] ) ) {
306
			if ( 1 == (int) $_GET['calypsoify'] ) {
307
				update_user_meta( get_current_user_id(), 'calypsoify', 1 );
308
			} else {
309
				update_user_meta( get_current_user_id(), 'calypsoify', 0 );
310
			}
311
		}
312
	}
313
314
	public function check_page() {
315
		// If the user hits plain /wp-admin/ then disable Calypso styles.
316
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
317
318
		if ( false !== strpos( 'index.php', $page ) || false !== strpos( 'wp-admin', $page ) ) {
319
			update_user_meta( get_current_user_id(), 'calypsoify', 0 );
320
			wp_safe_redirect( admin_url() );
321
			die;
322
		}
323
	}
324
325
	/**
326
	 * Return whether a post type should display the Gutenberg/block editor.
327
	 *
328
	 * @since 6.7.0
329
	 */
330
	public function is_post_type_gutenberg( $post_type ) {
331
		return use_block_editor_for_post_type( $post_type );
332
	}
333
334
	public function is_page_gutenberg() {
335
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
336
337
		if ( false !== strpos( $page, 'post-new.php' ) && empty ( $_GET['post_type'] ) ) {
338
			return true;
339
		}
340
341
		if ( false !== strpos( $page, 'post-new.php' ) && isset( $_GET['post_type'] ) && $this->is_post_type_gutenberg( $_GET['post_type'] ) ) {
342
			return true;
343
		}
344
345 View Code Duplication
		if ( false !== strpos( $page, 'post.php' ) ) {
346
			$post = get_post( $_GET['post'] );
347
			if ( isset( $post ) && isset( $post->post_type ) && $this->is_post_type_gutenberg( $post->post_type ) ) {
348
				return true;
349
			}
350
		}
351
352 View Code Duplication
		if ( false !== strpos( $page, 'revision.php' ) ) {
353
			$post   = get_post( $_GET['revision'] );
354
			$parent = get_post( $post->post_parent );
355
			if ( isset( $parent ) && isset( $parent->post_type ) && $this->is_post_type_gutenberg( $parent->post_type ) ) {
356
				return true;
357
			}
358
		}
359
360
		return false;
361
	}
362
363
	/**
364
	 * Attach a WP_List_Table views filter to all screens.
365
	 */
366
	public function attach_views_filter( $current_screen ) {
367
		add_filter( "views_{$current_screen->id}", array( $this, 'filter_views' ) );
368
	}
369
370
	/**
371
	 * Remove the parentheses from list table view counts when Calypsofied.
372
	 *
373
	 * @param array $views Array of views. See: WP_List_Table::get_views().
374
	 * @return array Filtered views.
375
	 */
376
	public function filter_views( $views ) {
377
		foreach ( $views as $id => $view ) {
378
			$views[ $id ] = preg_replace( '/<span class="count">\((\d+)\)<\/span>/', '<span class="count">$1</span>', $view );
379
		}
380
381
		return $views;
382
	}
383
}
384
385
$Jetpack_Calypsoify = Jetpack_Calypsoify::getInstance();
386