Completed
Push — branch-3.8 ( 6dae2a...5ea8e7 )
by
unknown
57:29 queued 49:08
created

Jetpack_JITM::manage_pi_msg()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 41
Code Lines 28

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 41
rs 5.3846
cc 8
eloc 28
nc 8
nop 0
1
<?php
2
3
/**
4
 * Jetpack just in time messaging through out the admin
5
 *
6
 * @since 3.7.0
7
 */
8
class Jetpack_JITM {
9
10
	/**
11
	 * @var Jetpack_JITM
12
	 **/
13
	private static $instance = null;
14
15
	/**
16
	 * Get user dismissed messages.
17
	 *
18
	 * @var array
19
	 */
20
	private static $jetpack_hide_jitm = array();
21
22
	static function init() {
23
		if ( is_null( self::$instance ) ) {
24
			self::$instance = new Jetpack_JITM;
25
		}
26
27
		return self::$instance;
28
	}
29
30
	private function __construct() {
31
		if ( ! Jetpack::is_active() ) {
32
			return;
33
		}
34
		add_action( 'current_screen', array( $this, 'prepare_jitms' ) );
35
	}
36
37
	/**
38
	 * Prepare actions according to screen and post type.
39
	 *
40
	 * @since 3.8.2
41
	 *
42
	 * @param object $screen
43
	 */
44
	function prepare_jitms( $screen ) {
45
		global $pagenow;
46
		$post_type = $screen->post_type;
47
		self::$jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' );
0 ignored issues
show
Documentation Bug introduced by
It seems like \Jetpack_Options::get_option('hide_jitm') of type * is incompatible with the declared type array of property $jetpack_hide_jitm.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
		$showphoton = empty( self::$jetpack_hide_jitm['photon'] ) ? 'show' : self::$jetpack_hide_jitm['photon'];
49
		$showmanage = empty( self::$jetpack_hide_jitm['manage'] ) ? 'show' : self::$jetpack_hide_jitm['manage'];
50
		$show_manage_pi = empty( self::$jetpack_hide_jitm['manage-pi'] ) ? 'show' : self::$jetpack_hide_jitm['manage-pi'];
51
		$show_editor = empty( self::$jetpack_hide_jitm['editor'] ) ? 'show' : self::$jetpack_hide_jitm['editor'];
52
		if ( 'media-new.php' == $pagenow && ! Jetpack::is_module_active( 'photon' ) && 'hide' != $showphoton ) {
53
			add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
54
			add_action( 'post-plupload-upload-ui', array( $this, 'photon_msg' ) );
55
		}
56
		else if ( 'update-core.php' == $pagenow && 'hide' != $showmanage ) {
57
			add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
58
			add_action( 'admin_notices', array( $this, 'manage_msg' ) );
59
		}
60
		elseif ( 'plugins.php' === $pagenow && isset( $_GET['activate'] ) && 'true' === $_GET['activate'] && 'hide' != $show_manage_pi ) {
61
			add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
62
			add_action( 'pre_current_active_plugins', array( $this, 'manage_pi_msg' ) );
63
		}
64
		elseif ( 'post-new.php' === $pagenow && in_array( $post_type, array( 'post', 'page' ) ) && 'hide' != $show_editor ) {
65
			add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
66
			add_action( 'admin_notices', array( $this, 'editor_msg' ) );
67
		}
68
	}
69
70
	/*
71
	 * Present Manage just in time activation msg on update-core.php
72
	 *
73
	 */
74
	function manage_msg() {
75
		if ( current_user_can( 'jetpack_manage_modules' ) ) {
76
			$normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
77
			$manage_active = Jetpack::is_module_active( 'manage' );
0 ignored issues
show
Unused Code introduced by
$manage_active is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
			?>
79
			<div class="jp-jitm">
80
				<a href="#"  data-module="manage" class="dismiss"><span class="genericon genericon-close"></span></a>
81
				<div class="jp-emblem">
82
					<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0" y="0" viewBox="0 0 172.9 172.9" enable-background="new 0 0 172.9 172.9" xml:space="preserve">
83
						<path d="M86.4 0C38.7 0 0 38.7 0 86.4c0 47.7 38.7 86.4 86.4 86.4s86.4-38.7 86.4-86.4C172.9 38.7 134.2 0 86.4 0zM83.1 106.6l-27.1-6.9C49 98 45.7 90.1 49.3 84l33.8-58.5V106.6zM124.9 88.9l-33.8 58.5V66.3l27.1 6.9C125.1 74.9 128.4 82.8 124.9 88.9z"/>
84
					</svg>
85
				</div>
86
				<p class="msg">
87
					<?php _e( 'Reduce security risks with automated plugin updates.', 'jetpack' ); ?>
88
				</p>
89
				<p>
90
					<img class="j-spinner hide" src="<?php echo esc_url( includes_url( 'images/spinner-2x.gif' ) ); ?>" alt="Loading ..." /><a href="#" data-module="manage" class="activate button <?php if( Jetpack::is_module_active( 'manage' ) ) { echo 'hide'; } ?>"><?php esc_html_e( 'Activate Now', 'jetpack' ); ?></a><a href="<?php echo esc_url( 'https://wordpress.com/plugins/' . $normalized_site_url ); ?>" target="_blank" title="<?php esc_attr_e( 'Go to WordPress.com to try these features', 'jetpack' ); ?>" id="jetpack-wordpressdotcom" class="button button-jetpack <?php if( ! Jetpack::is_module_active( 'manage' ) ) { echo 'hide'; } ?>"><?php esc_html_e( 'Go to WordPress.com', 'jetpack' ); ?></a>
91
				</p>
92
			</div>
93
		<?php
94
			//jitm is being viewed, track it
95
			$jetpack = Jetpack::init();
96
			$jetpack->stat( 'jitm', 'manage-viewed-' . JETPACK__VERSION );
97
			$jetpack->do_stats( 'server_side' );
98
		}
99
	}
100
101
	/*
102
	 * Present Photon just in time activation msg
103
	 *
104
	 */
105
	function photon_msg() {
106
		if ( current_user_can( 'jetpack_manage_modules' ) ) { ?>
107
			<div class="jp-jitm">
108
				<a href="#"  data-module="photon" class="dismiss"><span class="genericon genericon-close"></span></a>
109
				<div class="jp-emblem">
110
					<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0" y="0" viewBox="0 0 172.9 172.9" enable-background="new 0 0 172.9 172.9" xml:space="preserve">
111
						<path d="M86.4 0C38.7 0 0 38.7 0 86.4c0 47.7 38.7 86.4 86.4 86.4s86.4-38.7 86.4-86.4C172.9 38.7 134.2 0 86.4 0zM83.1 106.6l-27.1-6.9C49 98 45.7 90.1 49.3 84l33.8-58.5V106.6zM124.9 88.9l-33.8 58.5V66.3l27.1 6.9C125.1 74.9 128.4 82.8 124.9 88.9z"/>
112
					</svg>
113
				</div>
114
				<p class="msg">
115
					<?php _e( 'Speed up your photos and save bandwidth costs by using a free content delivery network.', 'jetpack' ); ?>
116
				</p>
117
				<p>
118
					<img class="j-spinner hide" style="margin-top: 13px;" width="17" height="17" src="<?php echo esc_url( includes_url( 'images/spinner-2x.gif' ) ); ?>" alt="Loading ..." /><a href="#" data-module="photon" class="activate button button-jetpack"><?php esc_html_e( 'Activate Photon', 'jetpack' ); ?></a>
119
				</p>
120
			</div>
121
		<?php
122
			//jitm is being viewed, track it
123
			$jetpack = Jetpack::init();
124
			$jetpack->stat( 'jitm', 'photon-viewed-' . JETPACK__VERSION );
125
			$jetpack->do_stats( 'server_side' );
126
		}
127
	}
128
129
	/**
130
	 * Display message prompting user to enable auto-updates in WordPress.com.
131
	 *
132
	 * @since 3.8.2
133
	 */
134
	function manage_pi_msg() {
135
		if ( current_user_can( 'jetpack_manage_modules' ) ) {
136
			$normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
137
			$manage_active = Jetpack::is_module_active( 'manage' );
138
			$manage_pi_dismissed = isset( self::$jetpack_hide_jitm['manage-pi'] );
139
140
			if ( ! $manage_active || ! $manage_pi_dismissed ) :
141
			?>
142
			<div class="jp-jitm">
143
				<a href="#"  data-module="manage-pi" class="dismiss"><span class="genericon genericon-close"></span></a>
144
				<div class="jp-emblem">
145
					<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0" y="0" viewBox="0 0 172.9 172.9" enable-background="new 0 0 172.9 172.9" xml:space="preserve">
146
						<path d="M86.4 0C38.7 0 0 38.7 0 86.4c0 47.7 38.7 86.4 86.4 86.4s86.4-38.7 86.4-86.4C172.9 38.7 134.2 0 86.4 0zM83.1 106.6l-27.1-6.9C49 98 45.7 90.1 49.3 84l33.8-58.5V106.6zM124.9 88.9l-33.8 58.5V66.3l27.1 6.9C125.1 74.9 128.4 82.8 124.9 88.9z"/>
147
					</svg>
148
				</div>
149
				<?php if ( ! $manage_active ) : ?>
150
					<p class="msg">
151
						<?php _e( 'Save time with automated plugin updates.', 'jetpack' ); ?>
152
					</p>
153
					<p>
154
						<img class="j-spinner hide" src="<?php echo esc_url( includes_url( 'images/spinner-2x.gif' ) ); ?>" alt="<?php echo esc_attr__( 'Loading...', 'jetpack' ); ?>" /><a href="#" data-module="manage" data-module-success="<?php esc_attr_e( 'Success!', 'jetpack' ); ?>" class="activate button"><?php esc_html_e( 'Activate remote management', 'jetpack'	); ?></a>
155
					</p>
156
				<?php elseif ( $manage_active ) : ?>
157
					<p>
158
						<?php esc_html_e( 'Save time with auto updates on WordPress.com', 'jetpack' ); ?>
159
					</p>
160
				<?php endif; // manage inactive ?>
161
				<?php if ( ! $manage_pi_dismissed ) : ?>
162
					<p class="show-after-enable <?php echo $manage_active ? '' : 'hide' ; ?>">
163
						<a href="<?php echo esc_url( 'https://wordpress.com/plugins/' . $normalized_site_url ); ?>" target="_blank" title="<?php esc_attr_e( 'Go to WordPress.com to enable auto-updates for plugins', 'jetpack' ); ?>" data-module="manage-pi" class="button button-jetpack launch show-after-enable"><?php esc_html_e( 'Enable auto-updates on WordPress.com', 'jetpack' ); ?></a>
164
					</p>
165
				<?php endif; // manage-pi inactive ?>
166
			</div>
167
			<?php
168
			//jitm is being viewed, track it
169
			$jetpack = Jetpack::init();
170
			$jetpack->stat( 'jitm', 'manage-pi-viewed-' . JETPACK__VERSION );
171
			$jetpack->do_stats( 'server_side' );
172
			endif; // manage or manage-pi inactive
173
		}
174
	}
175
176
	/**
177
	 * Display message in editor prompting user to compose entry in WordPress.com.
178
	 *
179
	 * @since 3.8.2
180
	 */
181
	function editor_msg() {
182
		global $typenow;
183
		if ( current_user_can( 'jetpack_manage_modules' ) && current_user_can( 'manage_options' ) ) {
184
			$normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
185
			$editor_dismissed = isset( self::$jetpack_hide_jitm['editor'] );
186
			if ( ! $editor_dismissed ) :
187
			?>
188
			<div class="jp-jitm">
189
				<a href="#"  data-module="editor" class="dismiss"><span class="genericon genericon-close"></span></a>
190
				<div class="jp-emblem">
191
					<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0" y="0" viewBox="0 0 172.9 172.9" enable-background="new 0 0 172.9 172.9" xml:space="preserve">
192
						<path d="M86.4 0C38.7 0 0 38.7 0 86.4c0 47.7 38.7 86.4 86.4 86.4s86.4-38.7 86.4-86.4C172.9 38.7 134.2 0 86.4 0zM83.1 106.6l-27.1-6.9C49 98 45.7 90.1 49.3 84l33.8-58.5V106.6zM124.9 88.9l-33.8 58.5V66.3l27.1 6.9C125.1 74.9 128.4 82.8 124.9 88.9z"/>
193
					</svg>
194
				</div>
195
				<p class="msg">
196
					<?php esc_html_e( 'Try the brand new editor.', 'jetpack' ); ?>
197
				</p>
198
				<p>
199
					<a href="<?php echo esc_url( 'https://wordpress.com/' . $typenow . '/' . $normalized_site_url ); ?>" target="_blank" title="<?php esc_attr_e( 'Write on WordPress.com', 'jetpack' ); ?>" data-module="editor" class="button button-jetpack launch show-after-enable"><?php esc_html_e( 'Write on WordPress.com', 'jetpack' ); ?></a>
200
				</p>
201
			</div>
202
			<?php
203
			//jitm is being viewed, track it
204
			$jetpack = Jetpack::init();
205
			$jetpack->stat( 'jitm', 'editor-viewed-' . JETPACK__VERSION );
206
			$jetpack->do_stats( 'server_side' );
207
			endif; // manage or editor inactive
208
		}
209
	}
210
211
	/*
212
	* Function to enqueue jitm css and js
213
	*/
214
	function jitm_enqueue_files( $hook ) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
215
216
		$wp_styles = new WP_Styles();
217
		$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
218
		wp_enqueue_style( 'jetpack-jitm-css', plugins_url( "css/jetpack-admin-jitm{$min}.css", JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION . '-201243242' );
219
		$wp_styles->add_data( 'jetpack-jitm-css', 'rtl', true );
220
221
		//Build stats url for tracking manage button
222
		$jitm_stats_url = Jetpack::build_stats_url( array( 'x_jetpack-jitm' => 'wordpresstools' ) );
223
224
		// Enqueue javascript to handle jitm notice events
225
		wp_enqueue_script( 'jetpack-jitm-js', plugins_url( '_inc/jetpack-jitm.js', JETPACK__PLUGIN_FILE ),
226
			array( 'jquery' ), JETPACK__VERSION, true );
227
		wp_localize_script(
228
			'jetpack-jitm-js',
229
			'jitmL10n',
230
			array(
231
				'ajaxurl'     => admin_url( 'admin-ajax.php' ),
232
				'jitm_nonce'  => wp_create_nonce( 'jetpack-jitm-nonce' ),
233
				'photon_msgs' => array(
234
					'success' => __( 'Success! Photon is now actively optimizing and serving your images for free.', 'jetpack' ),
235
					'fail'    => __( 'We are sorry but unfortunately Photon did not activate.', 'jetpack' )
236
				),
237
				'manage_msgs' => array(
238
					'success' => __( 'Success! WordPress.com tools are now active.', 'jetpack' ),
239
					'fail'    => __( 'We are sorry but unfortunately Manage did not activate.', 'jetpack' )
240
				),
241
				'jitm_stats_url' => $jitm_stats_url
242
			)
243
		);
244
	}
245
}
246
/**
247
 * Filter to turn off all just in time messages
248
 *
249
 * @since 3.7.0
250
 *
251
 * @param bool true Whether to show just in time messages.
252
 */
253
if ( apply_filters( 'jetpack_just_in_time_msgs', false ) ) {
254
	Jetpack_JITM::init();
255
}