Completed
Push — add/wordads-feature-check ( 1f9dc4...c3818c )
by
unknown
25:09 queued 08:52
created

SAL_Site::get_locale()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
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
			'suppress_filters' => false,
323
		) );
324
325
		if ( ! $posts || ! isset( $posts[0]->ID ) || ! $posts[0]->ID ) {
326
			$page = get_page_by_path( $name );
327
328
			if ( ! $page ) {
329
				return new WP_Error( 'unknown_post', 'Unknown post', 404 );
330
			}
331
332
			return $page->ID;
333
		}
334
335
		return (int) $posts[0]->ID;
336
	}
337
338
	/**
339
	 * Get post by name
340
	 *
341
	 * Attempts to match name on post title and page path
342
	 *
343
	 * @param string $name
344
	 * @param string $context (display or edit)
345
	 *
346
	 * @return object Post object on success, WP_Error object on failure
347
	 **/
348
	public function get_post_by_name( $name, $context ) {
349
		$post_id = $this->get_post_id_by_name( $name );
350
		if ( is_wp_error( $post_id ) ) {
351
			return $post_id;
352
		}
353
354
		return $this->get_post_by_id( $post_id, $context );
355
	}
356
357
	function user_can_manage() {
358
		current_user_can( 'manage_options' );
359
	}
360
361
	function get_xmlrpc_url() {
362
		$xmlrpc_scheme = apply_filters( 'wpcom_json_api_xmlrpc_scheme', parse_url( get_option( 'home' ), PHP_URL_SCHEME ) );
363
		return site_url( 'xmlrpc.php', $xmlrpc_scheme );
364
	}
365
366
	function get_registered_date() {
367
		if ( function_exists( 'get_blog_details' ) ) {
368
			$blog_details = get_blog_details();
369
			if ( ! empty( $blog_details->registered ) ) {
370
				return WPCOM_JSON_API_Date::format_date( $blog_details->registered );
371
			}
372
		}
373
374
		return '0000-00-00T00:00:00+00:00';
375
	}
376
377
	function get_capabilities() {
378
		return array(
379
			'edit_pages'          => current_user_can( 'edit_pages' ),
380
			'edit_posts'          => current_user_can( 'edit_posts' ),
381
			'edit_others_posts'   => current_user_can( 'edit_others_posts' ),
382
			'edit_others_pages'   => current_user_can( 'edit_others_pages' ),
383
			'delete_posts'        => current_user_can( 'delete_posts' ),
384
			'delete_others_posts' => current_user_can( 'delete_others_posts' ),
385
			'edit_theme_options'  => current_user_can( 'edit_theme_options' ),
386
			'edit_users'          => current_user_can( 'edit_users' ),
387
			'list_users'          => current_user_can( 'list_users' ),
388
			'manage_categories'   => current_user_can( 'manage_categories' ),
389
			'manage_options'      => current_user_can( 'manage_options' ),
390
			'moderate_comments'   => current_user_can( 'moderate_comments' ),
391
			'activate_wordads'    => wpcom_get_blog_owner() === (int) get_current_user_id(),
392
			'promote_users'       => current_user_can( 'promote_users' ),
393
			'publish_posts'       => current_user_can( 'publish_posts' ),
394
			'upload_files'        => current_user_can( 'upload_files' ),
395
			'delete_users'        => current_user_can( 'delete_users' ),
396
			'remove_users'        => current_user_can( 'remove_users' ),
397
			'view_stats'          => stats_is_blog_user( $this->blog_id )
398
		);
399
	}
400
401
	function is_visible() {
402
		if ( is_user_logged_in() ) {
403
			$current_user = wp_get_current_user();
404
			$visible      = (array) get_user_meta( $current_user->ID, 'blog_visibility', true );
405
406
			$is_visible = true;
407
			if ( isset( $visible[ $this->blog_id ] ) ) {
408
				$is_visible = (bool) $visible[ $this->blog_id ];
409
			}
410
411
			// null and true are visible
412
			return $is_visible;
413
		}
414
415
		return null;
416
	}
417
418
	function get_logo() {
419
420
		// Set an empty response array.
421
		$logo_setting = array(
422
			'id'    => (int) 0,
423
			'sizes' => array(),
424
			'url'   => '',
425
		);
426
427
		// Get current site logo values.
428
		$logo = get_option( 'site_logo' );
429
430
		// Update the response array if there's a site logo currenty active.
431
		if ( $logo && 0 != $logo['id'] ) {
432
			$logo_setting['id']  = $logo['id'];
433
			$logo_setting['url'] = $logo['url'];
434
435
			foreach ( $logo['sizes'] as $size => $properties ) {
436
				$logo_setting['sizes'][ $size ] = $properties;
437
			}
438
		}
439
440
		return $logo_setting;
441
	}
442
443
	function get_timezone() {
444
		return (string) get_option( 'timezone_string' );
445
	}
446
447
	function get_gmt_offset() {
448
		return (float) get_option( 'gmt_offset' );
449
	}
450
451
	function get_login_url() {
452
		return wp_login_url();
453
	}
454
455
	function get_admin_url() {
456
		return get_admin_url();
457
	}
458
459
	function get_unmapped_url() {
460
		return get_site_url( get_current_blog_id() );
461
	}
462
463
	function get_theme_slug() {
464
		return get_option( 'stylesheet' );
465
	}
466
467
	function get_header_image() {
468
		return get_theme_mod( 'header_image_data' );
469
	}
470
471
	function get_background_color() {
472
		return get_theme_mod( 'background_color' );
473
	}
474
475
	function get_image_default_link_type() {
476
		return get_option( 'image_default_link_type' );
477
	}
478
479
	function get_image_thumbnail_width() {
480
		return (int) get_option( 'thumbnail_size_w' );
481
	}
482
483
	function get_image_thumbnail_height() {
484
		return (int) get_option( 'thumbnail_size_h' );
485
	}
486
487
	function get_image_thumbnail_crop() {
488
		return get_option( 'thumbnail_crop' );
489
	}
490
491
	function get_image_medium_width() {
492
		return (int) get_option( 'medium_size_w' );
493
	}
494
495
	function get_image_medium_height() {
496
		return (int) get_option( 'medium_size_h' );
497
	}
498
499
	function get_image_large_width() {
500
		return (int) get_option( 'large_size_w' );
501
	}
502
503
	function get_image_large_height() {
504
		return (int) get_option( 'large_size_h' );
505
	}
506
507
	function get_permalink_structure() {
508
		return get_option( 'permalink_structure' );
509
	}
510
511
	function get_default_post_format() {
512
		return get_option( 'default_post_format' );
513
	}
514
515
	function get_default_category() {
516
		return (int) get_option( 'default_category' );
517
	}
518
519
	function get_show_on_front() {
520
		return get_option( 'show_on_front' );
521
	}
522
523
	function is_custom_front_page() {
524
		return ( 'page' === $this->get_show_on_front() );
525
	}
526
527
	function get_default_likes_enabled() {
528
		return (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
529
	}
530
531
	function get_default_sharing_status() {
532
		$default_sharing_status = false;
533
		if ( class_exists( 'Sharing_Service' ) ) {
534
			$ss                     = new Sharing_Service();
535
			$blog_services          = $ss->get_blog_services();
536
			$default_sharing_status = ! empty( $blog_services['visible'] );
537
		}
538
		return (bool) $default_sharing_status;
539
	}
540
541
	function get_default_comment_status() {
542
		return 'closed' !== get_option( 'default_comment_status' );
543
	}
544
545
	function default_ping_status() {
546
		return 'closed' !== get_option( 'default_ping_status' );
547
	}
548
549
	function is_publicize_permanently_disabled() {
550
		$publicize_permanently_disabled = false;
551
		if ( function_exists( 'is_publicize_permanently_disabled' ) ) {
552
			$publicize_permanently_disabled = is_publicize_permanently_disabled( $this->blog_id );
553
		}
554
		return $publicize_permanently_disabled;
555
	}
556
557
	function get_page_on_front() {
558
		return (int) get_option( 'page_on_front' );
559
	}
560
561
	function get_page_for_posts() {
562
		return (int) get_option( 'page_for_posts' );
563
	}
564
565
	function is_headstart() {
566
		return get_option( 'headstart' );
567
	}
568
569
	function get_wordpress_version() {
570
		global $wp_version;
571
		return $wp_version;
572
	}
573
574
	function is_domain_only() {
575
		$options = get_option( 'options' );
576
		return ! empty ( $options['is_domain_only'] ) ? (bool) $options['is_domain_only'] : false;
577
	}
578
579
	function get_blog_public() {
580
		return (int) get_option( 'blog_public' );
581
	}
582
583
	function has_pending_automated_transfer() {
584
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
585
			require_once( WP_CONTENT_DIR . '/lib/automated-transfer/utils.php' );
586
			return A8C\Automated_Transfer\Utils\has_site_pending_automated_transfer( $this->blog_id );
587
		}
588
589
		return false;
590
	}
591
592
	function signup_is_store() {
593
		return $this->get_design_type() === 'store';
594
	}
595
596
	function get_roles() {
597
		return new WP_Roles();
598
	}
599
600
	function get_design_type() {
601
		$options = get_option( 'options' );
602
		return empty( $options[ 'designType'] ) ? null : $options[ 'designType' ];
603
	}
604
605
	function get_site_goals() {
606
		$options = get_option( 'options' );
607
		return empty( $options[ 'siteGoals'] ) ? null : $options[ 'siteGoals' ];
608
	}
609
}
610