class.wpcom-json-api-site-settings-endpoint.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. <?php
  2. new WPCOM_JSON_API_Site_Settings_Endpoint( array(
  3. 'description' => 'Get detailed settings information about a site.',
  4. 'group' => '__do_not_document',
  5. 'stat' => 'sites:X',
  6. 'max_version' => '1.1',
  7. 'new_version' => '1.2',
  8. 'method' => 'GET',
  9. 'path' => '/sites/%s/settings',
  10. 'path_labels' => array(
  11. '$site' => '(int|string) Site ID or domain',
  12. ),
  13. 'query_parameters' => array(
  14. 'context' => false,
  15. ),
  16. 'response_format' => WPCOM_JSON_API_Site_Settings_Endpoint::$site_format,
  17. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/settings',
  18. ) );
  19. new WPCOM_JSON_API_Site_Settings_Endpoint( array(
  20. 'description' => 'Update settings for a site.',
  21. 'group' => '__do_not_document',
  22. 'stat' => 'sites:X',
  23. 'max_version' => '1.1',
  24. 'new_version' => '1.2',
  25. 'method' => 'POST',
  26. 'path' => '/sites/%s/settings',
  27. 'path_labels' => array(
  28. '$site' => '(int|string) Site ID or domain',
  29. ),
  30. 'request_format' => array(
  31. 'blogname' => '(string) Blog name',
  32. 'blogdescription' => '(string) Blog description',
  33. 'default_pingback_flag' => '(bool) Notify blogs linked from article?',
  34. 'default_ping_status' => '(bool) Allow link notifications from other blogs?',
  35. 'default_comment_status' => '(bool) Allow comments on new articles?',
  36. 'blog_public' => '(string) Site visibility; -1: private, 0: discourage search engines, 1: allow search engines',
  37. 'jetpack_sync_non_public_post_stati' => '(bool) allow sync of post and pages with non-public posts stati',
  38. 'jetpack_relatedposts_enabled' => '(bool) Enable related posts?',
  39. 'jetpack_relatedposts_show_headline' => '(bool) Show headline in related posts?',
  40. 'jetpack_relatedposts_show_thumbnails' => '(bool) Show thumbnails in related posts?',
  41. 'jetpack_protect_whitelist' => '(array) List of IP addresses to whitelist',
  42. 'infinite_scroll' => '(bool) Support infinite scroll of posts?',
  43. 'default_category' => '(int) Default post category',
  44. 'default_post_format' => '(string) Default post format',
  45. 'require_name_email' => '(bool) Require comment authors to fill out name and email?',
  46. 'comment_registration' => '(bool) Require users to be registered and logged in to comment?',
  47. 'close_comments_for_old_posts' => '(bool) Automatically close comments on old posts?',
  48. 'close_comments_days_old' => '(int) Age at which to close comments',
  49. 'thread_comments' => '(bool) Enable threaded comments?',
  50. 'thread_comments_depth' => '(int) Depth to thread comments',
  51. 'page_comments' => '(bool) Break comments into pages?',
  52. 'comments_per_page' => '(int) Number of comments to display per page',
  53. 'default_comments_page' => '(string) newest|oldest Which page of comments to display first',
  54. 'comment_order' => '(string) asc|desc Order to display comments within page',
  55. 'comments_notify' => '(bool) Email me when someone comments?',
  56. 'moderation_notify' => '(bool) Email me when a comment is helf for moderation?',
  57. 'social_notifications_like' => '(bool) Email me when someone likes my post?',
  58. 'social_notifications_reblog' => '(bool) Email me when someone reblogs my post?',
  59. 'social_notifications_subscribe' => '(bool) Email me when someone follows my blog?',
  60. 'comment_moderation' => '(bool) Moderate comments for manual approval?',
  61. 'comment_whitelist' => '(bool) Moderate comments unless author has a previously-approved comment?',
  62. 'comment_max_links' => '(int) Moderate comments that contain X or more links',
  63. 'moderation_keys' => '(string) Words or phrases that trigger comment moderation, one per line',
  64. 'blacklist_keys' => '(string) Words or phrases that mark comment spam, one per line',
  65. 'lang_id' => '(int) ID for language blog is written in',
  66. 'wga' => '(array) Google Analytics Settings',
  67. 'disabled_likes' => '(bool) Are likes globally disabled (they can still be turned on per post)?',
  68. 'disabled_reblogs' => '(bool) Are reblogs disabled on posts?',
  69. 'jetpack_comment_likes_enabled' => '(bool) Are comment likes enabled for all comments?',
  70. 'sharing_button_style' => '(string) Style to use for sharing buttons (icon-text, icon, text, or official)',
  71. 'sharing_label' => '(string) Label to use for sharing buttons, e.g. "Share this:"',
  72. 'sharing_show' => '(string|array:string) Post type or array of types where sharing buttons are to be displayed',
  73. 'sharing_open_links' => '(string) Link target for sharing buttons (same or new)',
  74. 'twitter_via' => '(string) Twitter username to include in tweets when people share using the Twitter button',
  75. 'jetpack-twitter-cards-site-tag' => '(string) The Twitter username of the owner of the site\'s domain.',
  76. 'eventbrite_api_token' => '(int) The Keyring token ID for an Eventbrite token to associate with the site',
  77. 'timezone_string' => '(string) PHP-compatible timezone string like \'UTC-5\'',
  78. 'gmt_offset' => '(int) Site offset from UTC in hours',
  79. 'date_format' => '(string) PHP Date-compatible date format',
  80. 'time_format' => '(string) PHP Date-compatible time format',
  81. 'start_of_week' => '(int) Starting day of week (0 = Sunday, 6 = Saturday)',
  82. 'jetpack_testimonial' => '(bool) Whether testimonial custom post type is enabled for the site',
  83. 'jetpack_testimonial_posts_per_page' => '(int) Number of testimonials to show per page',
  84. 'jetpack_portfolio' => '(bool) Whether portfolio custom post type is enabled for the site',
  85. 'jetpack_portfolio_posts_per_page' => '(int) Number of portfolio projects to show per page',
  86. Jetpack_SEO_Utils::FRONT_PAGE_META_OPTION => '(string) The seo meta description for the site.',
  87. Jetpack_SEO_Titles::TITLE_FORMATS_OPTION => '(array) SEO meta title formats. Allowed keys: front_page, posts, pages, groups, archives',
  88. 'verification_services_codes' => '(array) Website verification codes. Allowed keys: google, pinterest, bing, yandex',
  89. 'markdown_supported' => '(bool) Whether markdown is supported for this site',
  90. 'wpcom_publish_posts_with_markdown' => '(bool) Whether markdown is enabled for posts',
  91. 'wpcom_publish_comments_with_markdown' => '(bool) Whether markdown is enabled for comments',
  92. 'amp_is_enabled' => '(bool) Whether AMP is enabled for this site',
  93. 'site_icon' => '(int) Media attachment ID to use as site icon. Set to zero or an otherwise empty value to clear',
  94. 'api_cache' => '(bool) Turn on/off the Jetpack JSON API cache',
  95. 'posts_per_page' => '(int) Number of posts to show on blog pages',
  96. 'net_neutrality' => '(bool) Whether to show the net neutrality modal for a site',
  97. 'posts_per_rss' => '(int) Number of posts to show in the RSS feed',
  98. 'rss_use_excerpt' => '(bool) Whether the RSS feed will use post excerpts',
  99. ),
  100. 'response_format' => array(
  101. 'updated' => '(array)'
  102. ),
  103. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/settings',
  104. ) );
  105. class WPCOM_JSON_API_Site_Settings_Endpoint extends WPCOM_JSON_API_Endpoint {
  106. public static $site_format = array(
  107. 'ID' => '(int) Site ID',
  108. 'name' => '(string) Title of site',
  109. 'description' => '(string) Tagline or description of site',
  110. 'URL' => '(string) Full URL to the site',
  111. 'lang' => '(string) Primary language code of the site',
  112. 'locale_variant' => '(string) Locale variant code for the site, if set',
  113. 'settings' => '(array) An array of options/settings for the blog. Only viewable by users with post editing rights to the site.',
  114. );
  115. // GET /sites/%s/settings
  116. // POST /sites/%s/settings
  117. function callback( $path = '', $blog_id = 0 ) {
  118. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  119. if ( is_wp_error( $blog_id ) ) {
  120. return $blog_id;
  121. }
  122. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  123. // Source & include the infinite scroll compatibility files prior to loading theme functions
  124. add_filter( 'restapi_theme_action_copy_dirs', array( 'WPCOM_JSON_API_Site_Settings_Endpoint', 'wpcom_restapi_copy_theme_plugin_actions' ) );
  125. $this->load_theme_functions();
  126. }
  127. if ( ! is_user_logged_in() ) {
  128. return new WP_Error( 'Unauthorized', 'You must be logged-in to manage settings.', 401 );
  129. } else if ( ! current_user_can( 'manage_options' ) ) {
  130. return new WP_Error( 'Forbidden', 'You do not have the capability to manage settings for this site.', 403 );
  131. }
  132. if ( 'GET' === $this->api->method ) {
  133. /**
  134. * Fires on each GET request to a specific endpoint.
  135. *
  136. * @module json-api
  137. *
  138. * @since 3.2.0
  139. *
  140. * @param string sites.
  141. */
  142. do_action( 'wpcom_json_api_objects', 'sites' );
  143. return $this->get_settings_response();
  144. } else if ( 'POST' === $this->api->method ) {
  145. return $this->update_settings();
  146. } else {
  147. return new WP_Error( 'bad_request', 'An unsupported request method was used.' );
  148. }
  149. }
  150. /**
  151. * Includes additional theme-specific files to be included in REST API theme
  152. * context loading action copying.
  153. *
  154. * @see WPCOM_JSON_API_Endpoint#load_theme_functions
  155. * @see the_neverending_home_page_theme_support
  156. */
  157. function wpcom_restapi_copy_theme_plugin_actions( $copy_dirs ) {
  158. $theme_name = get_stylesheet();
  159. $default_file_name = WP_CONTENT_DIR . "/mu-plugins/infinity/themes/{$theme_name}.php";
  160. /**
  161. * Filter the path to the Infinite Scroll compatibility file.
  162. *
  163. * @module infinite-scroll
  164. *
  165. * @since 2.0.0
  166. *
  167. * @param string $str IS compatibility file path.
  168. * @param string $theme_name Theme name.
  169. */
  170. $customization_file = apply_filters( 'infinite_scroll_customization_file', $default_file_name, $theme_name );
  171. if ( is_readable( $customization_file ) ) {
  172. require_once $customization_file;
  173. $copy_dirs[] = $customization_file;
  174. }
  175. return $copy_dirs;
  176. }
  177. /**
  178. * Determines whether jetpack_relatedposts is supported
  179. *
  180. * @return bool
  181. */
  182. public function jetpack_relatedposts_supported() {
  183. $wpcom_related_posts_theme_blacklist = array(
  184. 'Expound',
  185. 'Traveler',
  186. 'Opti',
  187. 'Currents',
  188. );
  189. return ( ! in_array( wp_get_theme()->get( 'Name' ), $wpcom_related_posts_theme_blacklist ) );
  190. }
  191. /**
  192. * Returns category details
  193. *
  194. * @return array
  195. */
  196. public function get_category_details( $category ) {
  197. return array(
  198. 'value' => $category->term_id,
  199. 'name' => $category->name
  200. );
  201. }
  202. /**
  203. * Returns an option value as the result of the callable being applied to
  204. * it if a value is set, otherwise null.
  205. *
  206. * @param string $option_name Option name
  207. * @param callable $cast_callable Callable to invoke on option value
  208. * @return int|null Numeric option value or null
  209. */
  210. protected function get_cast_option_value_or_null( $option_name, $cast_callable ) {
  211. $option_value = get_option( $option_name, null );
  212. if ( is_null( $option_value ) ) {
  213. return $option_value;
  214. }
  215. return call_user_func( $cast_callable, $option_value );
  216. }
  217. /**
  218. * Collects the necessary information to return for a get settings response.
  219. *
  220. * @return array
  221. */
  222. public function get_settings_response() {
  223. // Allow update in later versions
  224. /**
  225. * Filter the structure of site settings to return.
  226. *
  227. * @module json-api
  228. *
  229. * @since 3.9.3
  230. *
  231. * @param array $site_format Data structure.
  232. */
  233. $response_format = apply_filters( 'site_settings_site_format', self::$site_format );
  234. $blog_id = (int) $this->api->get_blog_id_for_output();
  235. /** This filter is documented in class.json-api-endpoints.php */
  236. $is_jetpack = true === apply_filters( 'is_jetpack_site', false, $blog_id );
  237. foreach ( array_keys( $response_format ) as $key ) {
  238. // refactoring to change lang parameter to locale in 1.2
  239. if ( $lang_or_locale = $this->get_locale( $key ) ) {
  240. $response[$key] = $lang_or_locale;
  241. continue;
  242. }
  243. switch ( $key ) {
  244. case 'ID' :
  245. $response[$key] = $blog_id;
  246. break;
  247. case 'name' :
  248. $response[$key] = (string) htmlspecialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
  249. break;
  250. case 'description' :
  251. $response[$key] = (string) htmlspecialchars_decode( get_bloginfo( 'description' ), ENT_QUOTES );
  252. break;
  253. case 'URL' :
  254. $response[$key] = (string) home_url();
  255. break;
  256. case 'locale_variant':
  257. if ( function_exists( 'wpcom_l10n_get_blog_locale_variant' ) ) {
  258. $blog_locale_variant = wpcom_l10n_get_blog_locale_variant();
  259. if ( $blog_locale_variant ) {
  260. $response[$key] = $blog_locale_variant;
  261. }
  262. }
  263. break;
  264. case 'settings':
  265. $jetpack_relatedposts_options = Jetpack_Options::get_option( 'relatedposts', array() );
  266. // If the option's enabled key is NOT SET, it is considered enabled by the plugin
  267. if ( ! isset( $jetpack_relatedposts_options['enabled'] ) ) {
  268. $jetpack_relatedposts_options['enabled'] = true;
  269. }
  270. if ( method_exists( 'Jetpack', 'is_module_active' ) ) {
  271. $jetpack_relatedposts_options[ 'enabled' ] = Jetpack::is_module_active( 'related-posts' );
  272. }
  273. // array_values() is necessary to ensure the array starts at index 0.
  274. $post_categories = array_values(
  275. array_map(
  276. array( $this, 'get_category_details' ),
  277. get_categories( array( 'hide_empty' => false ) )
  278. )
  279. );
  280. $api_cache = $is_jetpack ? (bool) get_option( 'jetpack_api_cache_enabled' ) : true;
  281. $net_neutrality_options = get_option( 'net_neutrality_options_2017' );
  282. $net_neutrality = ( $net_neutrality_options && ! empty( $net_neutrality_options['enabled'] ) )
  283. ? true
  284. : false;
  285. $response[ $key ] = array(
  286. // also exists as "options"
  287. 'admin_url' => get_admin_url(),
  288. 'default_ping_status' => (bool) ( 'closed' != get_option( 'default_ping_status' ) ),
  289. 'default_comment_status' => (bool) ( 'closed' != get_option( 'default_comment_status' ) ),
  290. // new stuff starts here
  291. 'blog_public' => (int) get_option( 'blog_public' ),
  292. 'jetpack_sync_non_public_post_stati' => (bool) Jetpack_Options::get_option( 'sync_non_public_post_stati' ),
  293. 'jetpack_relatedposts_allowed' => (bool) $this->jetpack_relatedposts_supported(),
  294. 'jetpack_relatedposts_enabled' => (bool) $jetpack_relatedposts_options[ 'enabled' ],
  295. 'jetpack_relatedposts_show_headline' => (bool) isset( $jetpack_relatedposts_options[ 'show_headline' ] ) ? $jetpack_relatedposts_options[ 'show_headline' ] : false,
  296. 'jetpack_relatedposts_show_thumbnails' => (bool) isset( $jetpack_relatedposts_options[ 'show_thumbnails' ] ) ? $jetpack_relatedposts_options[ 'show_thumbnails' ] : false,
  297. 'default_category' => (int) get_option('default_category'),
  298. 'post_categories' => (array) $post_categories,
  299. 'default_post_format' => get_option( 'default_post_format' ),
  300. 'default_pingback_flag' => (bool) get_option( 'default_pingback_flag' ),
  301. 'require_name_email' => (bool) get_option( 'require_name_email' ),
  302. 'comment_registration' => (bool) get_option( 'comment_registration' ),
  303. 'close_comments_for_old_posts' => (bool) get_option( 'close_comments_for_old_posts' ),
  304. 'close_comments_days_old' => (int) get_option( 'close_comments_days_old' ),
  305. 'thread_comments' => (bool) get_option( 'thread_comments' ),
  306. 'thread_comments_depth' => (int) get_option( 'thread_comments_depth' ),
  307. 'page_comments' => (bool) get_option( 'page_comments' ),
  308. 'comments_per_page' => (int) get_option( 'comments_per_page' ),
  309. 'default_comments_page' => get_option( 'default_comments_page' ),
  310. 'comment_order' => get_option( 'comment_order' ),
  311. 'comments_notify' => (bool) get_option( 'comments_notify' ),
  312. 'moderation_notify' => (bool) get_option( 'moderation_notify' ),
  313. 'social_notifications_like' => ( "on" == get_option( 'social_notifications_like' ) ),
  314. 'social_notifications_reblog' => ( "on" == get_option( 'social_notifications_reblog' ) ),
  315. 'social_notifications_subscribe' => ( "on" == get_option( 'social_notifications_subscribe' ) ),
  316. 'comment_moderation' => (bool) get_option( 'comment_moderation' ),
  317. 'comment_whitelist' => (bool) get_option( 'comment_whitelist' ),
  318. 'comment_max_links' => (int) get_option( 'comment_max_links' ),
  319. 'moderation_keys' => get_option( 'moderation_keys' ),
  320. 'blacklist_keys' => get_option( 'blacklist_keys' ),
  321. 'lang_id' => defined( 'IS_WPCOM' ) && IS_WPCOM
  322. ? get_lang_id_by_code( wpcom_l10n_get_blog_locale_variant( $blog_id, true ) )
  323. : get_option( 'lang_id' ),
  324. 'wga' => $this->get_google_analytics(),
  325. 'disabled_likes' => (bool) get_option( 'disabled_likes' ),
  326. 'disabled_reblogs' => (bool) get_option( 'disabled_reblogs' ),
  327. 'jetpack_comment_likes_enabled' => (bool) get_option( 'jetpack_comment_likes_enabled', false ),
  328. 'twitter_via' => (string) get_option( 'twitter_via' ),
  329. 'jetpack-twitter-cards-site-tag' => (string) get_option( 'jetpack-twitter-cards-site-tag' ),
  330. 'eventbrite_api_token' => $this->get_cast_option_value_or_null( 'eventbrite_api_token', 'intval' ),
  331. 'gmt_offset' => get_option( 'gmt_offset' ),
  332. 'timezone_string' => get_option( 'timezone_string' ),
  333. 'date_format' => get_option( 'date_format' ),
  334. 'time_format' => get_option( 'time_format' ),
  335. 'start_of_week' => get_option( 'start_of_week' ),
  336. 'jetpack_testimonial' => (bool) get_option( 'jetpack_testimonial', '0' ),
  337. 'jetpack_testimonial_posts_per_page' => (int) get_option( 'jetpack_testimonial_posts_per_page', '10' ),
  338. 'jetpack_portfolio' => (bool) get_option( 'jetpack_portfolio', '0' ),
  339. 'jetpack_portfolio_posts_per_page' => (int) get_option( 'jetpack_portfolio_posts_per_page', '10' ),
  340. 'markdown_supported' => true,
  341. 'site_icon' => $this->get_cast_option_value_or_null( 'site_icon', 'intval' ),
  342. Jetpack_SEO_Utils::FRONT_PAGE_META_OPTION => get_option( Jetpack_SEO_Utils::FRONT_PAGE_META_OPTION, '' ),
  343. Jetpack_SEO_Titles::TITLE_FORMATS_OPTION => get_option( Jetpack_SEO_Titles::TITLE_FORMATS_OPTION, array() ),
  344. 'amp_is_supported' => (bool) function_exists( 'wpcom_is_amp_supported' ) && wpcom_is_amp_supported( $blog_id ),
  345. 'amp_is_enabled' => (bool) function_exists( 'wpcom_is_amp_enabled' ) && wpcom_is_amp_enabled( $blog_id ),
  346. 'api_cache' => $api_cache,
  347. 'posts_per_page' => (int) get_option( 'posts_per_page' ),
  348. 'net_neutrality' => $net_neutrality,
  349. 'posts_per_rss' => (int) get_option( 'posts_per_rss' ),
  350. 'rss_use_excerpt' => (bool) get_option( 'rss_use_excerpt' ),
  351. );
  352. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  353. $response[ $key ]['wpcom_publish_posts_with_markdown'] = (bool) WPCom_Markdown::is_posting_enabled();
  354. $response[ $key ]['wpcom_publish_comments_with_markdown'] = (bool) WPCom_Markdown::is_commenting_enabled();
  355. // WPCOM-specific Infinite Scroll Settings
  356. if ( is_callable( array( 'The_Neverending_Home_Page', 'get_settings' ) ) ) {
  357. /**
  358. * Clear the cached copy of widget info so it's pulled fresh from blog options.
  359. * It was primed during the initial load under the __REST API site__'s context.
  360. * @see wp_get_sidebars_widgets https://core.trac.wordpress.org/browser/trunk/src/wp-includes/widgets.php?rev=42374#L931
  361. */
  362. $GLOBALS['_wp_sidebars_widgets'] = array();
  363. $infinite_scroll_settings = The_Neverending_Home_Page::get_settings();
  364. $response[ $key ]['infinite_scroll'] = get_option( 'infinite_scroll', true ) && $infinite_scroll_settings->type === 'scroll';
  365. if ( $infinite_scroll_settings->footer_widgets || 'click' == $infinite_scroll_settings->requested_type ) {
  366. // The blog has footer widgets -- infinite scroll is blocked
  367. $response[ $key ]['infinite_scroll_blocked'] = 'footer';
  368. } else {
  369. $response[ $key ]['infinite_scroll_blocked'] = false;
  370. }
  371. }
  372. }
  373. //allow future versions of this endpoint to support additional settings keys
  374. /**
  375. * Filter the current site setting in the returned response.
  376. *
  377. * @module json-api
  378. *
  379. * @since 3.9.3
  380. *
  381. * @param mixed $response_item A single site setting.
  382. */
  383. $response[ $key ] = apply_filters( 'site_settings_endpoint_get', $response[ $key ] );
  384. if ( class_exists( 'Sharing_Service' ) ) {
  385. $ss = new Sharing_Service();
  386. $sharing = $ss->get_global_options();
  387. $response[ $key ]['sharing_button_style'] = (string) $sharing['button_style'];
  388. $response[ $key ]['sharing_label'] = (string) $sharing['sharing_label'];
  389. $response[ $key ]['sharing_show'] = (array) $sharing['show'];
  390. $response[ $key ]['sharing_open_links'] = (string) $sharing['open_links'];
  391. }
  392. if ( function_exists( 'jetpack_protect_format_whitelist' ) ) {
  393. $response[ $key ]['jetpack_protect_whitelist'] = jetpack_protect_format_whitelist();
  394. }
  395. if ( ! current_user_can( 'edit_posts' ) )
  396. unset( $response[$key] );
  397. break;
  398. }
  399. }
  400. return $response;
  401. }
  402. protected function get_locale( $key ) {
  403. if ( 'lang' == $key ) {
  404. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  405. return (string) get_blog_lang_code();
  406. } else {
  407. return get_locale();
  408. }
  409. }
  410. return false;
  411. }
  412. protected function get_google_analytics () {
  413. $option_name = defined( 'IS_WPCOM' ) && IS_WPCOM ? 'wga' : 'jetpack_wga';
  414. return get_option( $option_name );
  415. }
  416. /**
  417. * Updates site settings for authorized users
  418. *
  419. * @return array
  420. */
  421. public function update_settings() {
  422. // $this->input() retrieves posted arguments whitelisted and casted to the $request_format
  423. // specs that get passed in when this class is instantiated
  424. $input = $this->input();
  425. $unfiltered_input = $this->input( false, false );
  426. /**
  427. * Filters the settings to be updated on the site.
  428. *
  429. * @module json-api
  430. *
  431. * @since 3.6.0
  432. * @since 6.1.1 Added $unfiltered_input parameter.
  433. *
  434. * @param array $input Associative array of site settings to be updated.
  435. * Cast and filtered based on documentation.
  436. * @param array $unfiltered_input Associative array of site settings to be updated.
  437. * Neither cast nor filtered. Contains raw input.
  438. */
  439. $input = apply_filters( 'rest_api_update_site_settings', $input, $unfiltered_input );
  440. $blog_id = get_current_blog_id();
  441. $jetpack_relatedposts_options = array();
  442. $sharing_options = array();
  443. $updated = array();
  444. foreach ( $input as $key => $value ) {
  445. if ( ! is_array( $value ) ) {
  446. $value = trim( $value );
  447. }
  448. $value = wp_unslash( $value );
  449. switch ( $key ) {
  450. case 'default_ping_status':
  451. case 'default_comment_status':
  452. // settings are stored as closed|open
  453. $coerce_value = ( $value ) ? 'open' : 'closed';
  454. if ( update_option( $key, $coerce_value ) ) {
  455. $updated[ $key ] = $value;
  456. };
  457. break;
  458. case 'jetpack_protect_whitelist':
  459. if ( function_exists( 'jetpack_protect_save_whitelist' ) ) {
  460. $result = jetpack_protect_save_whitelist( $value );
  461. if ( is_wp_error( $result ) ) {
  462. return $result;
  463. }
  464. $updated[ $key ] = jetpack_protect_format_whitelist();
  465. }
  466. break;
  467. case 'jetpack_sync_non_public_post_stati':
  468. Jetpack_Options::update_option( 'sync_non_public_post_stati', $value );
  469. break;
  470. case 'jetpack_relatedposts_enabled':
  471. case 'jetpack_relatedposts_show_thumbnails':
  472. case 'jetpack_relatedposts_show_headline':
  473. if ( ! $this->jetpack_relatedposts_supported() ) {
  474. break;
  475. }
  476. if ( 'jetpack_relatedposts_enabled' === $key && method_exists( 'Jetpack', 'is_module_active' ) && $this->jetpack_relatedposts_supported() ) {
  477. $before_action = Jetpack::is_module_active('related-posts');
  478. if ( $value ) {
  479. Jetpack::activate_module( 'related-posts', false, false );
  480. } else {
  481. Jetpack::deactivate_module( 'related-posts' );
  482. }
  483. $after_action = Jetpack::is_module_active('related-posts');
  484. if ( $after_action == $before_action ) {
  485. break;
  486. }
  487. }
  488. $just_the_key = substr( $key, 21 );
  489. $jetpack_relatedposts_options[ $just_the_key ] = $value;
  490. break;
  491. case 'social_notifications_like':
  492. case 'social_notifications_reblog':
  493. case 'social_notifications_subscribe':
  494. // settings are stored as on|off
  495. $coerce_value = ( $value ) ? 'on' : 'off';
  496. if ( update_option( $key, $coerce_value ) ) {
  497. $updated[ $key ] = $value;
  498. }
  499. break;
  500. case 'wga':
  501. case 'jetpack_wga':
  502. if ( ! isset( $value['code'] ) || ! preg_match( '/^$|^UA-[\d-]+$/i', $value['code'] ) ) {
  503. return new WP_Error( 'invalid_code', 'Invalid UA ID' );
  504. }
  505. $is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM;
  506. $option_name = $is_wpcom ? 'wga' : 'jetpack_wga';
  507. $wga = get_option( $option_name, array() );
  508. $wga['code'] = $value['code']; // maintain compatibility with wp-google-analytics
  509. /**
  510. * Allow newer versions of this endpoint to filter in additional fields for Google Analytics
  511. *
  512. * @since 5.4.0
  513. *
  514. * @param array $wga Associative array of existing Google Analytics settings.
  515. * @param array $value Associative array of new Google Analytics settings passed to the endpoint.
  516. */
  517. $wga = apply_filters( 'site_settings_update_wga', $wga, $value );
  518. if ( update_option( $option_name, $wga ) ) {
  519. $updated[ $key ] = $value;
  520. }
  521. $enabled_or_disabled = $wga['code'] ? 'enabled' : 'disabled';
  522. /** This action is documented in modules/widgets/social-media-icons.php */
  523. do_action( 'jetpack_bump_stats_extras', 'google-analytics', $enabled_or_disabled );
  524. if ( $is_wpcom ) {
  525. $business_plugins = WPCOM_Business_Plugins::instance();
  526. $business_plugins->activate_plugin( 'wp-google-analytics' );
  527. }
  528. break;
  529. case 'jetpack_testimonial':
  530. case 'jetpack_portfolio':
  531. case 'jetpack_comment_likes_enabled':
  532. // settings are stored as 1|0
  533. $coerce_value = (int) $value;
  534. if ( update_option( $key, $coerce_value ) ) {
  535. $updated[ $key ] = (bool) $value;
  536. }
  537. break;
  538. case 'jetpack_testimonial_posts_per_page':
  539. case 'jetpack_portfolio_posts_per_page':
  540. // settings are stored as numeric
  541. $coerce_value = (int) $value;
  542. if ( update_option( $key, $coerce_value ) ) {
  543. $updated[ $key ] = $coerce_value;
  544. }
  545. break;
  546. // Sharing options
  547. case 'sharing_button_style':
  548. case 'sharing_show':
  549. case 'sharing_open_links':
  550. $sharing_options[ preg_replace( '/^sharing_/', '', $key ) ] = $value;
  551. break;
  552. case 'sharing_label':
  553. $sharing_options[ $key ] = $value;
  554. break;
  555. // Keyring token option
  556. case 'eventbrite_api_token':
  557. // These options can only be updated for sites hosted on WordPress.com
  558. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  559. if ( empty( $value ) || WPCOM_JSON_API::is_falsy( $value ) ) {
  560. if ( delete_option( $key ) ) {
  561. $updated[ $key ] = null;
  562. }
  563. } else if ( update_option( $key, $value ) ) {
  564. $updated[ $key ] = (int) $value;
  565. }
  566. }
  567. break;
  568. case 'api_cache':
  569. if ( empty( $value ) || WPCOM_JSON_API::is_falsy( $value ) ) {
  570. if ( delete_option( 'jetpack_api_cache_enabled' ) ) {
  571. $updated[ $key ] = false;
  572. }
  573. } else if ( update_option( 'jetpack_api_cache_enabled', true ) ) {
  574. $updated[ $key ] = true;
  575. }
  576. break;
  577. case 'timezone_string':
  578. // Map UTC+- timezones to gmt_offsets and set timezone_string to empty
  579. // https://github.com/WordPress/WordPress/blob/4.4.2/wp-admin/options.php#L175
  580. if ( ! empty( $value ) && preg_match( '/^UTC[+-]/', $value ) ) {
  581. $gmt_offset = preg_replace( '/UTC\+?/', '', $value );
  582. if ( update_option( 'gmt_offset', $gmt_offset ) ) {
  583. $updated[ 'gmt_offset' ] = $gmt_offset;
  584. }
  585. $value = '';
  586. }
  587. // Always set timezone_string either with the given value or with an
  588. // empty string
  589. if ( update_option( $key, $value ) ) {
  590. $updated[ $key ] = $value;
  591. }
  592. break;
  593. case 'date_format':
  594. case 'time_format':
  595. // settings are stored as strings
  596. if ( update_option( $key, sanitize_text_field( $value ) ) ) {
  597. $updated[ $key ] = $value;
  598. }
  599. break;
  600. case 'start_of_week':
  601. // setting is stored as int in 0-6 range (days of week)
  602. $coerce_value = (int) $value;
  603. $limit_value = ( $coerce_value >= 0 && $coerce_value <= 6 ) ? $coerce_value : 0;
  604. if ( update_option( $key, $limit_value ) ) {
  605. $updated[ $key ] = $limit_value;
  606. }
  607. break;
  608. case 'site_icon':
  609. // settings are stored as deletable numeric (all empty
  610. // values as delete intent), validated as media image
  611. if ( empty( $value ) || WPCOM_JSON_API::is_falsy( $value ) ) {
  612. /**
  613. * Fallback mechanism to clear a third party site icon setting. Can be used
  614. * to unset the option when an API request instructs the site to remove the site icon.
  615. *
  616. * @module json-api
  617. *
  618. * @since 4.10
  619. */
  620. if ( delete_option( $key ) || apply_filters( 'rest_api_site_icon_cleared', false ) ) {
  621. $updated[ $key ] = null;
  622. }
  623. } else if ( is_numeric( $value ) ) {
  624. $coerce_value = (int) $value;
  625. if ( wp_attachment_is_image( $coerce_value ) && update_option( $key, $coerce_value ) ) {
  626. $updated[ $key ] = $coerce_value;
  627. }
  628. }
  629. break;
  630. case Jetpack_SEO_Utils::FRONT_PAGE_META_OPTION:
  631. if ( ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() && ! Jetpack_SEO_Utils::has_grandfathered_front_page_meta() ) {
  632. return new WP_Error( 'unauthorized', __( 'SEO tools are not enabled for this site.', 'jetpack' ), 403 );
  633. }
  634. if ( ! is_string( $value ) ) {
  635. return new WP_Error( 'invalid_input', __( 'Invalid SEO meta description value.', 'jetpack' ), 400 );
  636. }
  637. $new_description = Jetpack_SEO_Utils::update_front_page_meta_description( $value );
  638. if ( ! empty( $new_description ) ) {
  639. $updated[ $key ] = $new_description;
  640. }
  641. break;
  642. case Jetpack_SEO_Titles::TITLE_FORMATS_OPTION:
  643. if ( ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) {
  644. return new WP_Error( 'unauthorized', __( 'SEO tools are not enabled for this site.', 'jetpack' ), 403 );
  645. }
  646. if ( ! Jetpack_SEO_Titles::are_valid_title_formats( $value ) ) {
  647. return new WP_Error( 'invalid_input', __( 'Invalid SEO title format.', 'jetpack' ), 400 );
  648. }
  649. $new_title_formats = Jetpack_SEO_Titles::update_title_formats( $value );
  650. if ( ! empty( $new_title_formats ) ) {
  651. $updated[ $key ] = $new_title_formats;
  652. }
  653. break;
  654. case 'verification_services_codes':
  655. $verification_codes = jetpack_verification_validate( $value );
  656. if ( update_option( 'verification_services_codes', $verification_codes ) ) {
  657. $updated[ $key ] = $verification_codes;
  658. }
  659. break;
  660. case 'wpcom_publish_posts_with_markdown':
  661. case 'wpcom_publish_comments_with_markdown':
  662. $coerce_value = (bool) $value;
  663. if ( update_option( $key, $coerce_value ) ) {
  664. $updated[ $key ] = $coerce_value;
  665. }
  666. break;
  667. case 'amp_is_enabled':
  668. if ( function_exists( 'wpcom_update_amp_enabled' ) ) {
  669. $saved = wpcom_update_amp_enabled( $blog_id, $value );
  670. if ( $saved ) {
  671. $updated[ $key ] = (bool) $value;
  672. }
  673. }
  674. break;
  675. case 'net_neutrality':
  676. $original_value = $value;
  677. $value = array( 'enabled' => (bool) $value );
  678. if ( update_option( 'net_neutrality_options_2017', $value ) ) {
  679. $updated[ $key ] = $original_value;
  680. }
  681. break;
  682. case 'rss_use_excerpt':
  683. update_option( 'rss_use_excerpt', (int)(bool) $value );
  684. break;
  685. default:
  686. //allow future versions of this endpoint to support additional settings keys
  687. if ( has_filter( 'site_settings_endpoint_update_' . $key ) ) {
  688. /**
  689. * Filter current site setting value to be updated.
  690. *
  691. * @module json-api
  692. *
  693. * @since 3.9.3
  694. *
  695. * @param mixed $response_item A single site setting value.
  696. */
  697. $value = apply_filters( 'site_settings_endpoint_update_' . $key, $value );
  698. $updated[ $key ] = $value;
  699. continue;
  700. }
  701. // no worries, we've already whitelisted and casted arguments above
  702. if ( update_option( $key, $value ) ) {
  703. $updated[ $key ] = $value;
  704. }
  705. }
  706. }
  707. if ( count( $jetpack_relatedposts_options ) ) {
  708. // track new jetpack_relatedposts options against old
  709. $old_relatedposts_options = Jetpack_Options::get_option( 'relatedposts' );
  710. if ( Jetpack_Options::update_option( 'relatedposts', $jetpack_relatedposts_options ) ) {
  711. foreach ( $jetpack_relatedposts_options as $key => $value ) {
  712. if ( isset( $old_relatedposts_options[ $key ] ) && $value !== $old_relatedposts_options[ $key ] ) {
  713. $updated[ 'jetpack_relatedposts_' . $key ] = $value;
  714. }
  715. }
  716. }
  717. }
  718. if ( ! empty( $sharing_options ) && class_exists( 'Sharing_Service' ) ) {
  719. $ss = new Sharing_Service();
  720. // Merge current values with updated, since Sharing_Service expects
  721. // all values to be included when updating
  722. $current_sharing_options = $ss->get_global_options();
  723. foreach ( $current_sharing_options as $key => $val ) {
  724. if ( ! isset( $sharing_options[ $key ] ) ) {
  725. $sharing_options[ $key ] = $val;
  726. }
  727. }
  728. $updated_social_options = $ss->set_global_options( $sharing_options );
  729. if ( isset( $input['sharing_button_style'] ) ) {
  730. $updated['sharing_button_style'] = (string) $updated_social_options['button_style'];
  731. }
  732. if ( isset( $input['sharing_label'] ) ) {
  733. // Sharing_Service won't report label as updated if set to default
  734. $updated['sharing_label'] = (string) $sharing_options['sharing_label'];
  735. }
  736. if ( isset( $input['sharing_show'] ) ) {
  737. $updated['sharing_show'] = (array) $updated_social_options['show'];
  738. }
  739. if ( isset( $input['sharing_open_links'] ) ) {
  740. $updated['sharing_open_links'] = (string) $updated_social_options['open_links'];
  741. }
  742. }
  743. return array(
  744. 'updated' => $updated
  745. );
  746. }
  747. }