Completed
Push — add/sync-rest-2 ( ada61b...a7dbc1 )
by
unknown
09:34
created

SAL_Site::get_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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