Completed
Push — add/changelog-61 ( 06c296...838e7a )
by
unknown
25:58 queued 15:49
created

SAL_Site::woocommerce_is_active()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
	public function get_quota() {
45
		return null;
46
	}
47
48
	abstract public function has_videopress();
49
50
	abstract public function upgraded_filetypes_enabled();
51
52
	abstract public function is_mapped_domain();
53
54
	abstract public function is_redirect();
55
56
	abstract public function is_headstart_fresh();
57
58
	abstract public function featured_images_enabled();
59
60
	abstract public function has_wordads();
61
62
	abstract public function get_frame_nonce();
63
64
	abstract public function allowed_file_types();
65
66
	abstract public function get_post_formats();
67
68
	abstract public function is_private();
69
70
	abstract public function is_following();
71
72
	abstract public function get_subscribers_count();
73
74
	abstract public function get_locale();
75
76
	abstract public function is_jetpack();
77
78
	abstract public function get_jetpack_modules();
79
80
	abstract public function is_vip();
81
82
	abstract public function is_multisite();
83
84
	abstract public function is_single_user_site();
85
86
	abstract public function get_plan();
87
88
	abstract public function get_ak_vp_bundle_enabled();
89
90
	abstract public function get_podcasting_archive();
91
92
	abstract public function get_jetpack_seo_front_page_description();
93
94
	abstract public function get_jetpack_seo_title_formats();
95
96
	abstract public function get_verification_services_codes();
97
98
	abstract public function before_render();
99
100
	abstract public function after_render( &$response );
101
102
	// 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...
103
	abstract public function after_render_options( &$options );
104
105
	// wrap a WP_Post object with SAL methods
106
	abstract public function wrap_post( $post, $context );
107
108
	abstract protected function is_a8c_publication( $post_id );
109
110
	public function is_automated_transfer() {
111
		return false;
112
	}
113
114
	public function is_wpcom_store() {
115
		return false;
116
	}
117
118
	public function woocommerce_is_active() {
119
		return false;
120
	}
121
122
	public function get_post_by_id( $post_id, $context ) {
123
		// Remove the skyword tracking shortcode for posts returned via the API.
124
		remove_shortcode( 'skyword-tracking' );
125
		add_shortcode( 'skyword-tracking', '__return_empty_string' );
126
127
		$post = get_post( $post_id, OBJECT, $context );
128
129
		if ( ! $post ) {
130
			return new WP_Error( 'unknown_post', 'Unknown post', 404 );
131
		}
132
133
		$wrapped_post = $this->wrap_post( $post, $context );
134
135
		// validate access
136
		return $this->validate_access( $wrapped_post );
137
	}
138
139
	/**
140
	 * Validate current user can access the post
141
	 *
142
	 * @return WP_Error or post
143
	 */
144
	private function validate_access( $post ) {
145
		$context = $post->context;
146
147
		if (
148
			! $this->is_post_type_allowed( $post->post_type )
149
			&& ! $this->is_a8c_publication( $post->ID )
150
		) {
151
			return new WP_Error( 'unknown_post', 'Unknown post', 404 );
152
		}
153
154
		switch ( $context ) {
155
		case 'edit' :
156
			if ( ! current_user_can( 'edit_post', $post ) ) {
157
				return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
158
			}
159
			break;
160
		case 'display' :
161
			$can_view = $this->user_can_view_post( $post );
162
			if ( is_wp_error( $can_view ) ) {
163
				return $can_view;
164
			}
165
			break;
166
		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...
167
			return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
168
		}
169
170
		return $post;
171
	}
172
173 View Code Duplication
	public function current_user_can_access_post_type( $post_type, $context ) {
174
		$post_type_object = $this->get_post_type_object( $post_type );
175
		if ( ! $post_type_object ) {
176
			return false;
177
		}
178
179
		switch( $context ) {
180
			case 'edit':
181
				return current_user_can( $post_type_object->cap->edit_posts );
182
			case 'display':
183
				return $post_type_object->public || current_user_can( $post_type_object->cap->read_private_posts );
184
			default:
185
				return false;
186
		}
187
	}
188
189
	protected function get_post_type_object( $post_type ) {
190
		return get_post_type_object( $post_type );
191
	}
192
193
	// copied from class.json-api-endpoints.php
194 View Code Duplication
	public function is_post_type_allowed( $post_type ) {
195
		// if the post type is empty, that's fine, WordPress will default to post
196
		if ( empty( $post_type ) ) {
197
			return true;
198
		}
199
200
		// allow special 'any' type
201
		if ( 'any' == $post_type ) {
202
			return true;
203
		}
204
205
		// check for allowed types
206
		if ( in_array( $post_type, $this->get_whitelisted_post_types() ) ) {
207
			return true;
208
		}
209
210
		if ( $post_type_object = get_post_type_object( $post_type ) ) {
211
			if ( ! empty( $post_type_object->show_in_rest ) ) {
212
				return $post_type_object->show_in_rest;
213
			}
214
			if ( ! empty( $post_type_object->publicly_queryable ) ) {
215
				return $post_type_object->publicly_queryable;
216
			}
217
		}
218
219
		return ! empty( $post_type_object->public );
220
	}
221
222
	// copied from class.json-api-endpoints.php
223
	/**
224
	 * Gets the whitelisted post types that JP should allow access to.
225
	 *
226
	 * @return array Whitelisted post types.
227
	 */
228 View Code Duplication
	public function get_whitelisted_post_types() {
229
		$allowed_types = array( 'post', 'page', 'revision' );
230
231
		/**
232
		 * Filter the post types Jetpack has access to, and can synchronize with WordPress.com.
233
		 *
234
		 * @module json-api
235
		 *
236
		 * @since 2.2.3
237
		 *
238
		 * @param array $allowed_types Array of whitelisted post types. Default to `array( 'post', 'page', 'revision' )`.
239
		 */
240
		$allowed_types = apply_filters( 'rest_api_allowed_post_types', $allowed_types );
241
242
		return array_unique( $allowed_types );
243
	}
244
245
	// copied and modified a little from class.json-api-endpoints.php
246
	private function user_can_view_post( $post ) {
247
		if ( !$post || is_wp_error( $post ) ) {
248
			return false;
249
		}
250
251 View Code Duplication
		if ( 'inherit' === $post->post_status ) {
252
			$parent_post = get_post( $post->post_parent );
253
			$post_status_obj = get_post_status_object( $parent_post->post_status );
254
		} else {
255
			$post_status_obj = get_post_status_object( $post->post_status );
256
		}
257
258
		$authorized = (
259
			$post_status_obj->public ||
260
			( is_user_logged_in() &&
261
				(
262
					( $post_status_obj->protected    && current_user_can( 'edit_post', $post->ID ) ) ||
263
					( $post_status_obj->private      && current_user_can( 'read_post', $post->ID ) ) ||
264
					( 'trash' === $post->post_status && current_user_can( 'edit_post', $post->ID ) ) ||
265
					'auto-draft' === $post->post_status
266
				)
267
			)
268
		);
269
270
		if ( ! $authorized ) {
271
			return new WP_Error( 'unauthorized', 'User cannot view post', 403 );
272
		}
273
274 View Code Duplication
		if (
275
			-1 == get_option( 'blog_public' ) &&
276
			/**
277
			 * Filter access to a specific post.
278
			 *
279
			 * @module json-api
280
			 *
281
			 * @since 3.4.0
282
			 *
283
			 * @param bool current_user_can( 'read_post', $post->ID ) Can the current user access the post.
284
			 * @param WP_Post $post Post data.
285
			 */
286
			! apply_filters(
287
				'wpcom_json_api_user_can_view_post',
288
				current_user_can( 'read_post', $post->ID ),
289
				$post
290
			)
291
		) {
292
			return new WP_Error( 'unauthorized', 'User cannot view post', array( 'status_code' => 403, 'error' => 'private_blog' ) );
293
		}
294
295 View Code Duplication
		if ( strlen( $post->post_password ) && !current_user_can( 'edit_post', $post->ID ) ) {
296
			return new WP_Error( 'unauthorized', 'User cannot view password protected post', array( 'status_code' => 403, 'error' => 'password_protected' ) );
297
		}
298
299
		return true;
300
	}
301
302
	/**
303
	 * Get post ID by name
304
	 *
305
	 * Attempts to match name on post title and page path
306
	 *
307
	 * @param string $name
308
	 *
309
	 * @return int|object Post ID on success, WP_Error object on failure
310
	 */
311
	public function get_post_id_by_name( $name ) {
312
		$name = sanitize_title( $name );
313
314
		if ( ! $name ) {
315
			return new WP_Error( 'invalid_post', 'Invalid post', 400 );
316
		}
317
318
		$posts = get_posts( array(
319
			'name' => $name,
320
			'numberposts' => 1,
321
			'post_type' => $this->get_whitelisted_post_types(),
322
		) );
323
324
		if ( ! $posts || ! isset( $posts[0]->ID ) || ! $posts[0]->ID ) {
325
			$page = get_page_by_path( $name );
326
327
			if ( ! $page ) {
328
				return new WP_Error( 'unknown_post', 'Unknown post', 404 );
329
			}
330
331
			return $page->ID;
332
		}
333
334
		return (int) $posts[0]->ID;
335
	}
336
337
	/**
338
	 * Get post by name
339
	 *
340
	 * Attempts to match name on post title and page path
341
	 *
342
	 * @param string $name
343
	 * @param string $context (display or edit)
344
	 *
345
	 * @return object Post object on success, WP_Error object on failure
346
	 **/
347
	public function get_post_by_name( $name, $context ) {
348
		$post_id = $this->get_post_id_by_name( $name );
349
		if ( is_wp_error( $post_id ) ) {
350
			return $post_id;
351
		}
352
353
		return $this->get_post_by_id( $post_id, $context );
354
	}
355
356
	function user_can_manage() {
357
		current_user_can( 'manage_options' );
358
	}
359
360
	function get_xmlrpc_url() {
361
		$xmlrpc_scheme = apply_filters( 'wpcom_json_api_xmlrpc_scheme', parse_url( get_option( 'home' ), PHP_URL_SCHEME ) );
362
		return site_url( 'xmlrpc.php', $xmlrpc_scheme );
363
	}
364
365
	function get_registered_date() {
366
		if ( function_exists( 'get_blog_details' ) ) {
367
			$blog_details = get_blog_details();
368
			if ( ! empty( $blog_details->registered ) ) {
369
				return WPCOM_JSON_API_Date::format_date( $blog_details->registered );
370
			}
371
		}
372
373
		return '0000-00-00T00:00:00+00:00';
374
	}
375
376
	function get_capabilities() {
377
		return array(
378
			'edit_pages'          => current_user_can( 'edit_pages' ),
379
			'edit_posts'          => current_user_can( 'edit_posts' ),
380
			'edit_others_posts'   => current_user_can( 'edit_others_posts' ),
381
			'edit_others_pages'   => current_user_can( 'edit_others_pages' ),
382
			'delete_posts'        => current_user_can( 'delete_posts' ),
383
			'delete_others_posts' => current_user_can( 'delete_others_posts' ),
384
			'edit_theme_options'  => current_user_can( 'edit_theme_options' ),
385
			'edit_users'          => current_user_can( 'edit_users' ),
386
			'list_users'          => current_user_can( 'list_users' ),
387
			'manage_categories'   => current_user_can( 'manage_categories' ),
388
			'manage_options'      => current_user_can( 'manage_options' ),
389
			'moderate_comments'   => current_user_can( 'moderate_comments' ),
390
			'activate_wordads'    => wpcom_get_blog_owner() === (int) get_current_user_id(),
391
			'promote_users'       => current_user_can( 'promote_users' ),
392
			'publish_posts'       => current_user_can( 'publish_posts' ),
393
			'upload_files'        => current_user_can( 'upload_files' ),
394
			'delete_users'        => current_user_can( 'delete_users' ),
395
			'remove_users'        => current_user_can( 'remove_users' ),
396
			'view_stats'          => stats_is_blog_user( $this->blog_id )
397
		);
398
	}
399
400
	function is_visible() {
401
		if ( is_user_logged_in() ) {
402
			$current_user = wp_get_current_user();
403
			$visible      = (array) get_user_meta( $current_user->ID, 'blog_visibility', true );
404
405
			$is_visible = true;
406
			if ( isset( $visible[ $this->blog_id ] ) ) {
407
				$is_visible = (bool) $visible[ $this->blog_id ];
408
			}
409
410
			// null and true are visible
411
			return $is_visible;
412
		}
413
414
		return null;
415
	}
416
417
	function get_logo() {
418
419
		// Set an empty response array.
420
		$logo_setting = array(
421
			'id'    => (int) 0,
422
			'sizes' => array(),
423
			'url'   => '',
424
		);
425
426
		// Get current site logo values.
427
		$logo = get_option( 'site_logo' );
428
429
		// Update the response array if there's a site logo currenty active.
430
		if ( $logo && 0 != $logo['id'] ) {
431
			$logo_setting['id']  = $logo['id'];
432
			$logo_setting['url'] = $logo['url'];
433
434
			foreach ( $logo['sizes'] as $size => $properties ) {
435
				$logo_setting['sizes'][ $size ] = $properties;
436
			}
437
		}
438
439
		return $logo_setting;
440
	}
441
442
	function get_timezone() {
443
		return (string) get_option( 'timezone_string' );
444
	}
445
446
	function get_gmt_offset() {
447
		return (float) get_option( 'gmt_offset' );
448
	}
449
450
	function get_login_url() {
451
		return wp_login_url();
452
	}
453
454
	function get_admin_url() {
455
		return get_admin_url();
456
	}
457
458
	function get_unmapped_url() {
459
		return get_site_url( get_current_blog_id() );
460
	}
461
462
	function get_theme_slug() {
463
		return get_option( 'stylesheet' );
464
	}
465
466
	function get_header_image() {
467
		return get_theme_mod( 'header_image_data' );
468
	}
469
470
	function get_background_color() {
471
		return get_theme_mod( 'background_color' );
472
	}
473
474
	function get_image_default_link_type() {
475
		return get_option( 'image_default_link_type' );
476
	}
477
478
	function get_image_thumbnail_width() {
479
		return (int) get_option( 'thumbnail_size_w' );
480
	}
481
482
	function get_image_thumbnail_height() {
483
		return (int) get_option( 'thumbnail_size_h' );
484
	}
485
486
	function get_image_thumbnail_crop() {
487
		return get_option( 'thumbnail_crop' );
488
	}
489
490
	function get_image_medium_width() {
491
		return (int) get_option( 'medium_size_w' );
492
	}
493
494
	function get_image_medium_height() {
495
		return (int) get_option( 'medium_size_h' );
496
	}
497
498
	function get_image_large_width() {
499
		return (int) get_option( 'large_size_w' );
500
	}
501
502
	function get_image_large_height() {
503
		return (int) get_option( 'large_size_h' );
504
	}
505
506
	function get_permalink_structure() {
507
		return get_option( 'permalink_structure' );
508
	}
509
510
	function get_default_post_format() {
511
		return get_option( 'default_post_format' );
512
	}
513
514
	function get_default_category() {
515
		return (int) get_option( 'default_category' );
516
	}
517
518
	function get_show_on_front() {
519
		return get_option( 'show_on_front' );
520
	}
521
522
	function is_custom_front_page() {
523
		return ( 'page' === $this->get_show_on_front() );
524
	}
525
526
	function get_default_likes_enabled() {
527
		return (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
528
	}
529
530
	function get_default_sharing_status() {
531
		$default_sharing_status = false;
532
		if ( class_exists( 'Sharing_Service' ) ) {
533
			$ss                     = new Sharing_Service();
534
			$blog_services          = $ss->get_blog_services();
535
			$default_sharing_status = ! empty( $blog_services['visible'] );
536
		}
537
		return (bool) $default_sharing_status;
538
	}
539
540
	function get_default_comment_status() {
541
		return 'closed' !== get_option( 'default_comment_status' );
542
	}
543
544
	function default_ping_status() {
545
		return 'closed' !== get_option( 'default_ping_status' );
546
	}
547
548
	function is_publicize_permanently_disabled() {
549
		$publicize_permanently_disabled = false;
550
		if ( function_exists( 'is_publicize_permanently_disabled' ) ) {
551
			$publicize_permanently_disabled = is_publicize_permanently_disabled( $this->blog_id );
552
		}
553
		return $publicize_permanently_disabled;
554
	}
555
556
	function get_page_on_front() {
557
		return (int) get_option( 'page_on_front' );
558
	}
559
560
	function get_page_for_posts() {
561
		return (int) get_option( 'page_for_posts' );
562
	}
563
564
	function is_headstart() {
565
		return get_option( 'headstart' );
566
	}
567
568
	function get_wordpress_version() {
569
		global $wp_version;
570
		return $wp_version;
571
	}
572
573
	function is_domain_only() {
574
		$options = get_option( 'options' );
575
		return ! empty ( $options['is_domain_only'] ) ? (bool) $options['is_domain_only'] : false;
576
	}
577
578
	function get_blog_public() {
579
		return (int) get_option( 'blog_public' );
580
	}
581
582
	function has_pending_automated_transfer() {
583
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
584
			require_once( WP_CONTENT_DIR . '/lib/automated-transfer/utils.php' );
585
			return A8C\Automated_Transfer\Utils\has_site_pending_automated_transfer( $this->blog_id );
586
		}
587
588
		return false;
589
	}
590
591
	function signup_is_store() {
592
		return $this->get_design_type() === 'store';
593
	}
594
595
	function get_roles() {
596
		return new WP_Roles();
597
	}
598
599
	function get_design_type() {
600
		$options = get_option( 'options' );
601
		return empty( $options[ 'designType'] ) ? null : $options[ 'designType' ];
602
	}
603
604
	function get_site_goals() {
605
		$options = get_option( 'options' );
606
		return empty( $options[ 'siteGoals'] ) ? null : $options[ 'siteGoals' ];
607
	}
608
}
609