Completed
Push — fix/videopress-missing-from-se... ( 60564b )
by
unknown
22:41 queued 12:31
created

SAL_Site::user_can_view_post()   C

Complexity

Conditions 17
Paths 113

Size

Total Lines 55
Code Lines 27

Duplication

Lines 29
Ratio 52.73 %

Importance

Changes 0
Metric Value
cc 17
eloc 27
nc 113
nop 1
dl 29
loc 55
rs 6.2267
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
require_once dirname( __FILE__ ) . '/class.json-api-date.php';
4
require_once dirname( __FILE__ ) . '/class.json-api-post-base.php';
5
6
/**
7
 * Base class for the Site Abstraction Layer (SAL)
8
 * Note that this is the site "as seen by user $user_id with token $token", which
9
 * is why we pass the token to the platform; these site instances are value objects
10
 * to be used in the context of a single request for a single user.
11
 * Also note that at present this class _assumes_ you've "switched to"
12
 * the site in question, and functions like `get_bloginfo( 'name' )` will
13
 * therefore return the correct value
14
 **/
15
abstract class SAL_Site {
16
	public $blog_id;
17
	public $platform;
18
19
	public function __construct( $blog_id, $platform ) {
20
		$this->blog_id = $blog_id;
21
		$this->platform = $platform;
22
	}
23
24
	public function get_id() {
25
		return $this->blog_id;
26
	}
27
28
	public function get_name() {
29
		return (string) htmlspecialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
30
	}
31
32
	public function get_description() {
33
		return (string) htmlspecialchars_decode( get_bloginfo( 'description' ), ENT_QUOTES );
34
	}
35
36
	public function get_url() {
37
		return (string) home_url();
38
	}
39
40
	public function get_post_count() {
41
		return (int) wp_count_posts( 'post' )->publish;
42
	}
43
44
	abstract public function has_videopress();
45
46
	abstract public function upgraded_filetypes_enabled();
47
48
	abstract public function is_mapped_domain();
49
50
	abstract public function is_redirect();
51
52
	abstract public function featured_images_enabled();
53
54
	abstract public function has_wordads();
55
56
	abstract public function get_frame_nonce();
57
58
	abstract public function allowed_file_types();
59
60
	abstract public function get_post_formats();
61
62
	abstract public function is_private();
63
64
	abstract public function is_following();
65
66
	abstract public function get_subscribers_count();
67
68
	abstract public function get_locale();
69
70
	abstract public function is_jetpack();
71
72
	abstract public function get_jetpack_modules();
73
74
	abstract public function is_vip();
75
76
	abstract public function is_multisite();
77
78
	abstract public function is_single_user_site();
79
80
	abstract public function get_plan();
81
82
	abstract public function get_ak_vp_bundle_enabled();
83
84
	abstract public function get_jetpack_seo_front_page_description();
85
86
	abstract public function get_jetpack_seo_title_formats();
87
88
	abstract public function get_verification_services_codes();
89
90
	abstract public function before_render();
91
92
	abstract public function after_render( &$response );
93
94
	// TODO - factor this out? Seems an odd thing to have on a site
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
95
	abstract public function after_render_options( &$options );
96
97
	// wrap a WP_Post object with SAL methods
98
	abstract public function wrap_post( $post, $context );
99
100
101
	public function get_post_by_id( $post_id, $context ) {
102
		$post = get_post( $post_id, OBJECT, $context );
103
104
		if ( ! $post ) {
105
			return new WP_Error( 'unknown_post', 'Unknown post', 404 );
106
		}
107
108
		$wrapped_post = $this->wrap_post( $post, $context );
109
110
		// validate access
111
		return $this->validate_access( $wrapped_post );
112
	}
113
114
	/**
115
	 * Validate current user can access the post
116
	 * 
117
	 * @return WP_Error or post
118
	 */
119
	private function validate_access( $post ) {
120
		$context = $post->context;
121
122 View Code Duplication
		if ( ! $this->is_post_type_allowed( $post->post_type ) 
123
			&& 
124
			( ! function_exists( 'is_post_freshly_pressed' ) || ! is_post_freshly_pressed( $post->ID ) ) ) {
125
			return new WP_Error( 'unknown_post', 'Unknown post', 404 );
126
		}
127
128
		switch ( $context ) {
129
		case 'edit' :
130
			if ( ! current_user_can( 'edit_post', $post ) ) {
131
				return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
132
			}
133
			break;
134
		case 'display' :
135
			$can_view = $this->user_can_view_post( $post );
136
			if ( is_wp_error( $can_view ) ) {
137
				return $can_view;
138
			}
139
			break;
140
		default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
141
			return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
142
		}
143
144
		return $post;
145
	}
146
147
	// copied from class.json-api-endpoints.php
148
	private function is_post_type_allowed( $post_type ) {
149
		// if the post type is empty, that's fine, WordPress will default to post
150
		if ( empty( $post_type ) )
151
			return true;
152
153
		// allow special 'any' type
154
		if ( 'any' == $post_type )
155
			return true;
156
157
		// check for allowed types
158
		if ( in_array( $post_type, $this->_get_whitelisted_post_types() ) )
159
			return true;
160
161
		return false;
162
	}
163
164
	// copied from class.json-api-endpoints.php
165
	/**
166
	 * Gets the whitelisted post types that JP should allow access to.
167
	 *
168
	 * @return array Whitelisted post types.
169
	 */
170 View Code Duplication
	private function _get_whitelisted_post_types() {
171
		$allowed_types = array( 'post', 'page', 'revision' );
172
173
		/**
174
		 * Filter the post types Jetpack has access to, and can synchronize with WordPress.com.
175
		 *
176
		 * @module json-api
177
		 *
178
		 * @since 2.2.3
179
		 *
180
		 * @param array $allowed_types Array of whitelisted post types. Default to `array( 'post', 'page', 'revision' )`.
181
		 */
182
		$allowed_types = apply_filters( 'rest_api_allowed_post_types', $allowed_types );
183
184
		return array_unique( $allowed_types );
185
	}
186
187
	// copied and modified a little from class.json-api-endpoints.php
188
	private function user_can_view_post( $post ) {
189
		if ( !$post || is_wp_error( $post ) ) {
190
			return false;
191
		}
192
193 View Code Duplication
		if ( 'inherit' === $post->post_status ) {
194
			$parent_post = get_post( $post->post_parent );
195
			$post_status_obj = get_post_status_object( $parent_post->post_status );
196
		} else {
197
			$post_status_obj = get_post_status_object( $post->post_status );
198
		}
199
200
		$authorized = (
201
			$post_status_obj->public ||
202
			( is_user_logged_in() && 
203
				(
204
					( $post_status_obj->protected    && current_user_can( 'edit_post', $post->ID ) ) ||
205
					( $post_status_obj->private      && current_user_can( 'read_post', $post->ID ) ) ||
206
					( 'trash' === $post->post_status && current_user_can( 'edit_post', $post->ID ) ) ||
207
					'auto-draft' === $post->post_status
208
				) 
209
			) 
210
		);
211
212
		if ( ! $authorized ) {
213
			return new WP_Error( 'unauthorized', 'User cannot view post', 403 );
214
		}
215
216 View Code Duplication
		if (
217
			-1 == get_option( 'blog_public' ) &&
218
			/**
219
			 * Filter access to a specific post.
220
			 *
221
			 * @module json-api
222
			 *
223
			 * @since 3.4.0
224
			 *
225
			 * @param bool current_user_can( 'read_post', $post->ID ) Can the current user access the post.
226
			 * @param WP_Post $post Post data.
227
			 */
228
			! apply_filters(
229
				'wpcom_json_api_user_can_view_post',
230
				current_user_can( 'read_post', $post->ID ),
231
				$post
232
			)
233
		) {
234
			return new WP_Error( 'unauthorized', 'User cannot view post', array( 'status_code' => 403, 'error' => 'private_blog' ) );
235
		}
236
237 View Code Duplication
		if ( strlen( $post->post_password ) && !current_user_can( 'edit_post', $post->ID ) ) {
238
			return new WP_Error( 'unauthorized', 'User cannot view password protected post', array( 'status_code' => 403, 'error' => 'password_protected' ) );
239
		}
240
241
		return true;
242
	}
243
244
	/**
245
	 * Get post ID by name
246
	 *
247
	 * Attempts to match name on post title and page path
248
	 *
249
	 * @param string $name
250
	 *
251
	 * @return int|object Post ID on success, WP_Error object on failure
252
	 */
253
	public function get_post_id_by_name( $name ) {
254
		$name = sanitize_title( $name );
255
256
		if ( ! $name ) {
257
			return new WP_Error( 'invalid_post', 'Invalid post', 400 );
258
		}
259
260
		$posts = get_posts( array(
261
			'name' => $name,
262
			'numberposts' => 1,
263
			'post_type' => $this->_get_whitelisted_post_types(),
264
		) );
265
266
		if ( ! $posts || ! isset( $posts[0]->ID ) || ! $posts[0]->ID ) {
267
			$page = get_page_by_path( $name );
268
269
			if ( ! $page ) {
270
				return new WP_Error( 'unknown_post', 'Unknown post', 404 );
271
			}
272
273
			return $page->ID;
274
		}
275
276
		return (int) $posts[0]->ID;
277
	}
278
279
	/**
280
	 * Get post by name
281
	 *
282
	 * Attempts to match name on post title and page path
283
	 *
284
	 * @param string $name
285
	 * @param string $context (display or edit)
286
	 *
287
	 * @return object Post object on success, WP_Error object on failure
288
	 **/
289
	public function get_post_by_name( $name, $context ) {
290
		$post_id = $this->get_post_id_by_name( $name );
291
		if ( is_wp_error( $post_id ) ) {
292
			return $post_id;
293
		}
294
295
		return $this->get_post_by_id( $post_id, $context );
296
	}
297
298
	function user_can_manage() {
299
		current_user_can( 'manage_options' );
300
	}
301
302
	function get_xmlrpc_url() {
303
		$xmlrpc_scheme = apply_filters( 'wpcom_json_api_xmlrpc_scheme', parse_url( get_option( 'home' ), PHP_URL_SCHEME ) );
304
		return site_url( 'xmlrpc.php', $xmlrpc_scheme );
305
	}
306
307
	function get_registered_date() {
308
		if ( function_exists( 'get_blog_details' ) ) {
309
			$blog_details = get_blog_details();
310
			if ( ! empty( $blog_details->registered ) ) {
311
				return WPCOM_JSON_API_Date::format_date( $blog_details->registered );
312
			}
313
		}
314
315
		return '0000-00-00T00:00:00+00:00';
316
	}
317
318
	function get_capabilities() {
319
		return array(
320
			'edit_pages'          => current_user_can( 'edit_pages' ),
321
			'edit_posts'          => current_user_can( 'edit_posts' ),
322
			'edit_others_posts'   => current_user_can( 'edit_others_posts' ),
323
			'edit_others_pages'   => current_user_can( 'edit_others_pages' ),
324
			'delete_posts'        => current_user_can( 'delete_posts' ),
325
			'delete_others_posts' => current_user_can( 'delete_others_posts' ),
326
			'edit_theme_options'  => current_user_can( 'edit_theme_options' ),
327
			'edit_users'          => current_user_can( 'edit_users' ),
328
			'list_users'          => current_user_can( 'list_users' ),
329
			'manage_categories'   => current_user_can( 'manage_categories' ),
330
			'manage_options'      => current_user_can( 'manage_options' ),
331
			'promote_users'       => current_user_can( 'promote_users' ),
332
			'publish_posts'       => current_user_can( 'publish_posts' ),
333
			'upload_files'        => current_user_can( 'upload_files' ),
334
			'view_stats'          => stats_is_blog_user( $this->blog_id )
335
		);
336
	}
337
338
	function is_visible() {
339
		if ( is_user_logged_in() ) {
340
			$current_user = wp_get_current_user();
341
			$visible      = (array) get_user_meta( $current_user->ID, 'blog_visibility', true );
342
343
			$is_visible = true;
344
			if ( isset( $visible[ $this->blog_id ] ) ) {
345
				$is_visible = (bool) $visible[ $this->blog_id ];
346
			}
347
348
			// null and true are visible
349
			return $is_visible;
350
		}
351
352
		return null;
353
	}
354
355
	function get_logo() {
356
357
		// Set an empty response array.
358
		$logo_setting = array(
359
			'id'    => (int) 0,
360
			'sizes' => array(),
361
			'url'   => '',
362
		);
363
364
		// Get current site logo values.
365
		$logo = get_option( 'site_logo' );
366
367
		// Update the response array if there's a site logo currenty active.
368
		if ( $logo && 0 != $logo['id'] ) {
369
			$logo_setting['id']  = $logo['id'];
370
			$logo_setting['url'] = $logo['url'];
371
372
			foreach ( $logo['sizes'] as $size => $properties ) {
373
				$logo_setting['sizes'][ $size ] = $properties;
374
			}
375
		}
376
377
		return $logo_setting;
378
	}
379
380
	function get_timezone() {
381
		return (string) get_option( 'timezone_string' );
382
	}
383
384
	function get_gmt_offset() {
385
		return (float) get_option( 'gmt_offset' );
386
	}
387
388
	function get_login_url() {
389
		return wp_login_url();
390
	}
391
392
	function get_admin_url() {
393
		return get_admin_url();
394
	}
395
396
	function get_unmapped_url() {
397
		return get_site_url( get_current_blog_id() );
398
	}
399
400
	function get_theme_slug() {
401
		return get_option( 'stylesheet' );
402
	}
403
404
	function get_header_image() {
405
		return get_theme_mod( 'header_image_data' );
406
	}
407
408
	function get_background_color() {
409
		return get_theme_mod( 'background_color' );
410
	}
411
412
	function get_image_default_link_type() {
413
		return get_option( 'image_default_link_type' );
414
	}
415
416
	function get_image_thumbnail_width() {
417
		return (int) get_option( 'thumbnail_size_w' );	
418
	}
419
420
	function get_image_thumbnail_height() {
421
		return (int) get_option( 'thumbnail_size_h' );
422
	}	
423
424
	function get_image_thumbnail_crop() {
425
		return get_option( 'thumbnail_crop' );
426
	}
427
428
	function get_image_medium_width() {
429
		return (int) get_option( 'medium_size_w' );
430
	}
431
432
	function get_image_medium_height() {
433
		return (int) get_option( 'medium_size_h' );	
434
	}
435
436
	function get_image_large_width() {
437
		return (int) get_option( 'large_size_w' );
438
	}
439
440
	function get_image_large_height() {
441
		return (int) get_option( 'large_size_h' );
442
	}
443
444
	function get_permalink_structure() {
445
		return get_option( 'permalink_structure' );
446
	}
447
448
	function get_default_post_format() {
449
		return get_option( 'default_post_format' );	
450
	}
451
	
452
	function get_default_category() {
453
		return (int) get_option( 'default_category' );
454
	}
455
456
	function get_show_on_front() {
457
		return get_option( 'show_on_front' );
458
	}
459
460
	function is_custom_front_page() {
461
		return ( 'page' === $this->get_show_on_front() );
462
	}
463
464
	function get_default_likes_enabled() {
465
		return (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
466
	}
467
468
	function get_default_sharing_status() {
469
		$default_sharing_status = false;
470
		if ( class_exists( 'Sharing_Service' ) ) {
471
			$ss                     = new Sharing_Service();
472
			$blog_services          = $ss->get_blog_services();
473
			$default_sharing_status = ! empty( $blog_services['visible'] );
474
		}
475
		return (bool) $default_sharing_status;
476
	}
477
478
	function get_default_comment_status() {
479
		return 'closed' !== get_option( 'default_comment_status' );
480
	}
481
482
	function default_ping_status() {
483
		return 'closed' !== get_option( 'default_ping_status' );	
484
	}
485
486
	function is_publicize_permanently_disabled() {
487
		$publicize_permanently_disabled = false;
488
		if ( function_exists( 'is_publicize_permanently_disabled' ) ) {
489
			$publicize_permanently_disabled = is_publicize_permanently_disabled( $this->blog_id );
490
		}
491
		return $publicize_permanently_disabled;	
492
	}
493
	
494
	function get_page_on_front() {
495
		return (int) get_option( 'page_on_front' );
496
	}
497
498
	function get_page_for_posts() {
499
		return (int) get_option( 'page_for_posts' );
500
	}
501
502
	function is_headstart() {
503
		return get_option( 'headstart' );
504
	}
505
506
	function get_wordpress_version() {
507
		global $wp_version;
508
		return $wp_version;
509
	}
510
}
511