PageRenderTime 54ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php

https://gitlab.com/juanito.abelo/nlmobile
PHP | 349 lines | 269 code | 44 blank | 36 comment | 49 complexity | f8e41d64e4bf6be823009561e5a13e60 MD5 | raw file
  1. <?php
  2. class WPCOM_JSON_API_Site_Settings_Endpoint extends WPCOM_JSON_API_Endpoint {
  3. public static $site_format = array(
  4. 'ID' => '(int) Site ID',
  5. 'name' => '(string) Title of site',
  6. 'description' => '(string) Tagline or description of site',
  7. 'URL' => '(string) Full URL to the site',
  8. 'lang' => '(string) Primary language code of the site',
  9. 'settings' => '(array) An array of options/settings for the blog. Only viewable by users with post editing rights to the site.',
  10. );
  11. // GET /sites/%s/settings
  12. // POST /sites/%s/settings
  13. function callback( $path = '', $blog_id = 0 ) {
  14. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  15. if ( is_wp_error( $blog_id ) ) {
  16. return $blog_id;
  17. }
  18. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  19. $this->load_theme_functions();
  20. }
  21. if ( ! is_user_logged_in() ) {
  22. return new WP_Error( 'Unauthorized', 'You must be logged-in to manage settings.', 401 );
  23. } else if ( ! current_user_can( 'manage_options' ) ) {
  24. return new WP_Error( 'Forbidden', 'You do not have the capability to manage settings for this site.', 403 );
  25. }
  26. if ( 'GET' === $this->api->method ) {
  27. do_action( 'wpcom_json_api_objects', 'sites' );
  28. return $this->get_settings_response();
  29. } else if ( 'POST' === $this->api->method ) {
  30. return $this->update_settings();
  31. } else {
  32. return new WP_Error( 'bad_request', 'An unsupported request method was used.' );
  33. }
  34. }
  35. /**
  36. * Determines whether jetpack_relatedposts is supported
  37. *
  38. * @return (bool)
  39. */
  40. public function jetpack_relatedposts_supported() {
  41. $wpcom_related_posts_theme_blacklist = array(
  42. 'Expound',
  43. 'Traveler',
  44. 'Opti',
  45. 'Currents',
  46. );
  47. return ( ! in_array( wp_get_theme()->get( 'Name' ), $wpcom_related_posts_theme_blacklist ) );
  48. }
  49. /**
  50. * Returns category details
  51. *
  52. * @return (array)
  53. */
  54. public function get_category_details( $category ) {
  55. return array(
  56. 'value' => $category->term_id,
  57. 'name' => $category->name
  58. );
  59. }
  60. /**
  61. * Collects the necessary information to return for a get settings response.
  62. *
  63. * @return (array)
  64. */
  65. public function get_settings_response() {
  66. $response_format = self::$site_format;
  67. $blog_id = (int) $this->api->get_blog_id_for_output();
  68. $is_jetpack = true === apply_filters( 'is_jetpack_site', false, $blog_id );
  69. foreach ( array_keys( $response_format ) as $key ) {
  70. switch ( $key ) {
  71. case 'ID' :
  72. $response[$key] = $blog_id;
  73. break;
  74. case 'name' :
  75. $response[$key] = (string) htmlspecialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
  76. break;
  77. case 'description' :
  78. $response[$key] = (string) htmlspecialchars_decode( get_bloginfo( 'description' ), ENT_QUOTES );
  79. break;
  80. case 'URL' :
  81. $response[$key] = (string) home_url();
  82. break;
  83. case 'lang' :
  84. $response[$key] = (string) get_bloginfo( 'language' );
  85. break;
  86. case 'settings':
  87. $jetpack_relatedposts_options = Jetpack_Options::get_option( 'relatedposts' );
  88. if ( method_exists( 'Jetpack', 'is_module_active' ) ) {
  89. $jetpack_relatedposts_options[ 'enabled' ] = Jetpack::is_module_active( 'related-posts' );
  90. }
  91. // array_values() is necessary to ensure the array starts at index 0.
  92. $post_categories = array_values(
  93. array_map(
  94. array( $this, 'get_category_details' ),
  95. get_categories( array( 'hide_empty' => false ) )
  96. )
  97. );
  98. $response[$key] = array(
  99. // also exists as "options"
  100. 'admin_url' => get_admin_url(),
  101. 'default_ping_status' => (bool) ( 'closed' != get_option( 'default_ping_status' ) ),
  102. 'default_comment_status' => (bool) ( 'closed' != get_option( 'default_comment_status' ) ),
  103. // new stuff starts here
  104. 'blog_public' => (int) get_option( 'blog_public' ),
  105. 'jetpack_sync_non_public_post_stati' => (bool) Jetpack_Options::get_option( 'sync_non_public_post_stati' ),
  106. 'jetpack_relatedposts_allowed' => (bool) $this->jetpack_relatedposts_supported(),
  107. 'jetpack_relatedposts_enabled' => (bool) $jetpack_relatedposts_options[ 'enabled' ],
  108. 'jetpack_relatedposts_show_headline' => (bool) $jetpack_relatedposts_options[ 'show_headline' ],
  109. 'jetpack_relatedposts_show_thumbnails' => (bool) $jetpack_relatedposts_options[ 'show_thumbnails' ],
  110. 'default_category' => get_option('default_category'),
  111. 'post_categories' => (array) $post_categories,
  112. 'default_post_format' => get_option( 'default_post_format' ),
  113. 'default_pingback_flag' => (bool) get_option( 'default_pingback_flag' ),
  114. 'require_name_email' => (bool) get_option( 'require_name_email' ),
  115. 'comment_registration' => (bool) get_option( 'comment_registration' ),
  116. 'close_comments_for_old_posts' => (bool) get_option( 'close_comments_for_old_posts' ),
  117. 'close_comments_days_old' => (int) get_option( 'close_comments_days_old' ),
  118. 'thread_comments' => (bool) get_option( 'thread_comments' ),
  119. 'thread_comments_depth' => (int) get_option( 'thread_comments_depth' ),
  120. 'page_comments' => (bool) get_option( 'page_comments' ),
  121. 'comments_per_page' => (int) get_option( 'comments_per_page' ),
  122. 'default_comments_page' => get_option( 'default_comments_page' ),
  123. 'comment_order' => get_option( 'comment_order' ),
  124. 'comments_notify' => (bool) get_option( 'comments_notify' ),
  125. 'moderation_notify' => (bool) get_option( 'moderation_notify' ),
  126. 'social_notifications_like' => ( "on" == get_option( 'social_notifications_like' ) ),
  127. 'social_notifications_reblog' => ( "on" == get_option( 'social_notifications_reblog' ) ),
  128. 'social_notifications_subscribe' => ( "on" == get_option( 'social_notifications_subscribe' ) ),
  129. 'comment_moderation' => (bool) get_option( 'comment_moderation' ),
  130. 'comment_whitelist' => (bool) get_option( 'comment_whitelist' ),
  131. 'comment_max_links' => (int) get_option( 'comment_max_links' ),
  132. 'moderation_keys' => get_option( 'moderation_keys' ),
  133. 'blacklist_keys' => get_option( 'blacklist_keys' ),
  134. 'lang_id' => get_option( 'lang_id' ),
  135. 'wga' => get_option( 'wga' ),
  136. 'disabled_likes' => (bool) get_option( 'disabled_likes' ),
  137. 'disabled_reblogs' => (bool) get_option( 'disabled_reblogs' ),
  138. 'jetpack_comment_likes_enabled' => (bool) get_option( 'jetpack_comment_likes_enabled', false ),
  139. 'twitter_via' => (string) get_option( 'twitter_via' ),
  140. 'jetpack-twitter-cards-site-tag' => (string) get_option( 'jetpack-twitter-cards-site-tag' ),
  141. );
  142. if ( class_exists( 'Sharing_Service' ) ) {
  143. $ss = new Sharing_Service();
  144. $sharing = $ss->get_global_options();
  145. $response[ $key ]['sharing_button_style'] = (string) $sharing['button_style'];
  146. $response[ $key ]['sharing_label'] = (string) $sharing['sharing_label'];
  147. $response[ $key ]['sharing_show'] = (array) $sharing['show'];
  148. $response[ $key ]['sharing_open_links'] = (string) $sharing['open_links'];
  149. }
  150. if ( function_exists( 'jetpack_protect_format_whitelist' ) ) {
  151. $response[ $key ]['jetpack_protect_whitelist'] = jetpack_protect_format_whitelist();
  152. }
  153. if ( ! current_user_can( 'edit_posts' ) )
  154. unset( $response[$key] );
  155. break;
  156. }
  157. }
  158. return $response;
  159. }
  160. /**
  161. * Updates site settings for authorized users
  162. *
  163. * @return (array)
  164. */
  165. public function update_settings() {
  166. // $this->input() retrieves posted arguments whitelisted and casted to the $request_format
  167. // specs that get passed in when this class is instantiated
  168. $input = $this->input();
  169. $jetpack_relatedposts_options = array();
  170. $sharing_options = array();
  171. $updated = array();
  172. foreach ( $input as $key => $value ) {
  173. if ( ! is_array( $value ) ) {
  174. $value = trim( $value );
  175. }
  176. $value = wp_unslash( $value );
  177. switch ( $key ) {
  178. case 'default_ping_status':
  179. case 'default_comment_status':
  180. // settings are stored as closed|open
  181. $coerce_value = ( $value ) ? 'open' : 'closed';
  182. if ( update_option( $key, $coerce_value ) ) {
  183. $updated[ $key ] = $value;
  184. };
  185. break;
  186. case 'jetpack_protect_whitelist':
  187. if ( function_exists( 'jetpack_protect_save_whitelist' ) ) {
  188. $result = jetpack_protect_save_whitelist( $value );
  189. if ( is_wp_error( $result ) ) {
  190. return $result;
  191. }
  192. $updated[ $key ] = jetpack_protect_format_whitelist();
  193. }
  194. break;
  195. case 'jetpack_sync_non_public_post_stati':
  196. Jetpack_Options::update_option( 'sync_non_public_post_stati', $value );
  197. break;
  198. case 'jetpack_relatedposts_enabled':
  199. case 'jetpack_relatedposts_show_thumbnails':
  200. case 'jetpack_relatedposts_show_headline':
  201. if ( ! $this->jetpack_relatedposts_supported() ) {
  202. break;
  203. }
  204. if ( 'jetpack_relatedposts_enabled' === $key && method_exists( 'Jetpack', 'is_module_active' ) && $this->jetpack_relatedposts_supported() ) {
  205. $before_action = Jetpack::is_module_active('related-posts');
  206. if ( $value ) {
  207. Jetpack::activate_module( 'related-posts', false, false );
  208. } else {
  209. Jetpack::deactivate_module( 'related-posts' );
  210. }
  211. $after_action = Jetpack::is_module_active('related-posts');
  212. if ( $after_action == $before_action ) {
  213. break;
  214. }
  215. }
  216. $just_the_key = substr( $key, 21 );
  217. $jetpack_relatedposts_options[ $just_the_key ] = $value;
  218. break;
  219. case 'social_notifications_like':
  220. case 'social_notifications_reblog':
  221. case 'social_notifications_subscribe':
  222. // settings are stored as on|off
  223. $coerce_value = ( $value ) ? 'on' : 'off';
  224. if ( update_option( $key, $coerce_value ) ) {
  225. $updated[ $key ] = $value;
  226. }
  227. break;
  228. case 'wga':
  229. if ( ! isset( $value['code'] ) || ! preg_match( '/^$|^UA-[\d-]+$/i', $value['code'] ) ) {
  230. return new WP_Error( 'invalid_code', 'Invalid UA ID' );
  231. }
  232. $wga = get_option( 'wga', array() );
  233. $wga['code'] = $value['code']; // maintain compatibility with wp-google-analytics
  234. if ( update_option( 'wga', $wga ) ) {
  235. $updated[ $key ] = $value;
  236. }
  237. $enabled_or_disabled = $wga['code'] ? 'enabled' : 'disabled';
  238. bump_stats_extras( 'google-analytics', $enabled_or_disabled );
  239. $business_plugins = WPCOM_Business_Plugins::instance();
  240. $business_plugins->activate_plugin( 'wp-google-analytics' );
  241. break;
  242. case 'jetpack_comment_likes_enabled':
  243. // settings are stored as 1|0
  244. $coerce_value = (int) $value;
  245. if ( update_option( $key, $coerce_value ) ) {
  246. $updated[ $key ] = $value;
  247. }
  248. break;
  249. // Sharing options
  250. case 'sharing_button_style':
  251. case 'sharing_show':
  252. case 'sharing_open_links':
  253. $sharing_options[ preg_replace( '/^sharing_/', '', $key ) ] = $value;
  254. break;
  255. case 'sharing_label':
  256. $sharing_options[ $key ] = $value;
  257. break;
  258. // no worries, we've already whitelisted and casted arguments above
  259. default:
  260. if ( update_option( $key, $value ) ) {
  261. $updated[ $key ] = $value;
  262. }
  263. }
  264. }
  265. if ( count( $jetpack_relatedposts_options ) ) {
  266. // track new jetpack_relatedposts options against old
  267. $old_relatedposts_options = Jetpack_Options::get_option( 'relatedposts' );
  268. if ( Jetpack_Options::update_option( 'relatedposts', $jetpack_relatedposts_options ) ) {
  269. foreach( $jetpack_relatedposts_options as $key => $value ) {
  270. if ( $value !== $old_relatedposts_options[ $key ] ) {
  271. $updated[ 'jetpack_relatedposts_' . $key ] = $value;
  272. }
  273. }
  274. }
  275. }
  276. if ( ! empty( $sharing_options ) && class_exists( 'Sharing_Service' ) ) {
  277. $ss = new Sharing_Service();
  278. // Merge current values with updated, since Sharing_Service expects
  279. // all values to be included when updating
  280. $current_sharing_options = $ss->get_global_options();
  281. foreach ( $current_sharing_options as $key => $val ) {
  282. if ( ! isset( $sharing_options[ $key ] ) ) {
  283. $sharing_options[ $key ] = $val;
  284. }
  285. }
  286. $updated_social_options = $ss->set_global_options( $sharing_options );
  287. if ( isset( $input['sharing_button_style'] ) ) {
  288. $updated['sharing_button_style'] = (string) $updated_social_options['button_style'];
  289. }
  290. if ( isset( $input['sharing_label'] ) ) {
  291. // Sharing_Service won't report label as updated if set to default
  292. $updated['sharing_label'] = (string) $sharing_options['sharing_label'];
  293. }
  294. if ( isset( $input['sharing_show'] ) ) {
  295. $updated['sharing_show'] = (array) $updated_social_options['show'];
  296. }
  297. if ( isset( $input['sharing_open_links'] ) ) {
  298. $updated['sharing_open_links'] = (string) $updated_social_options['open_links'];
  299. }
  300. }
  301. return array(
  302. 'updated' => $updated
  303. );
  304. }
  305. }