summaryrefslogtreecommitdiff
blob: 872512b6b17874146f913e5655578dcb72070132 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php

class WPCOM_JSON_API_Site_Settings_V1_2_Endpoint extends WPCOM_JSON_API_Site_Settings_Endpoint {

	public static $site_format = array(
		'ID'             => '(int) Site ID',
		'name'           => '(string) Title of site',
		'description'    => '(string) Tagline or description of site',
		'URL'            => '(string) Full URL to the site',
		'locale'         => '(string) Locale code of the site',
		'locale_variant' => '(string) Locale variant code for the site, if set',
		'settings'       => '(array) An array of options/settings for the blog. Only viewable by users with post editing rights to the site.',
	);


	function callback( $path = '', $blog_id = 0 ) {
		add_filter( 'site_settings_endpoint_update_locale', array( $this, 'update_locale' ) );
		add_filter( 'site_settings_endpoint_get',           array( $this, 'return_locale' ) );
		add_filter( 'site_settings_site_format',            array( $this, 'site_format' ) );
		return parent::callback( $path, $blog_id );
	}


	protected function get_locale( $key ) {
		if ( 'locale' == $key ) {
			if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
				return (string) get_blog_lang_code();
			} else {
				return get_locale();
			}
		}

		return false;
	}

	public function return_locale( $settings ) {
		return $settings + array( 'locale' => $this->get_locale( 'locale' ) );
	}

	public function update_locale( $value ) {
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
			$lang_id = get_lang_id_by_code( $value );
			if ( ! empty( $lang_id ) ) {
				if ( update_option( 'lang_id', $lang_id ) ) {
					return true;
				}
			}
		}
		return false;
	}

	public function site_format( $format ) {
		return self::$site_format;
	}
}