Нема описа

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

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