class.core-rest-api-endpoints.php 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924
  1. <?php
  2. /**
  3. * Register WP REST API endpoints for Jetpack.
  4. *
  5. * @author Automattic
  6. */
  7. /**
  8. * Disable direct access.
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13. // Load WP_Error for error messages.
  14. require_once ABSPATH . '/wp-includes/class-wp-error.php';
  15. // Register endpoints when WP REST API is initialized.
  16. add_action( 'rest_api_init', array( 'Jetpack_Core_Json_Api_Endpoints', 'register_endpoints' ) );
  17. /**
  18. * Class Jetpack_Core_Json_Api_Endpoints
  19. *
  20. * @since 4.3.0
  21. */
  22. class Jetpack_Core_Json_Api_Endpoints {
  23. /**
  24. * @var string Generic error message when user is not allowed to perform an action.
  25. */
  26. public static $user_permissions_error_msg;
  27. /**
  28. * @var array Roles that can access Stats once they're granted access.
  29. */
  30. public static $stats_roles;
  31. /**
  32. * Declare the Jetpack REST API endpoints.
  33. *
  34. * @since 4.3.0
  35. */
  36. public static function register_endpoints() {
  37. // Load API endpoint base classes
  38. require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-xmlrpc-consumer-endpoint.php';
  39. // Load API endpoints
  40. require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php';
  41. require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php';
  42. require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-widgets-endpoints.php';
  43. self::$user_permissions_error_msg = esc_html__(
  44. 'You do not have the correct user permissions to perform this action.
  45. Please contact your site admin if you think this is a mistake.',
  46. 'jetpack'
  47. );
  48. self::$stats_roles = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' );
  49. Jetpack::load_xml_rpc_client();
  50. $ixr_client = new Jetpack_IXR_Client( array( 'user_id' => get_current_user_id() ) );
  51. $core_api_endpoint = new Jetpack_Core_API_Data( $ixr_client );
  52. $module_list_endpoint = new Jetpack_Core_API_Module_List_Endpoint();
  53. $module_data_endpoint = new Jetpack_Core_API_Module_Data_Endpoint();
  54. $module_toggle_endpoint = new Jetpack_Core_API_Module_Toggle_Endpoint( new Jetpack_IXR_Client() );
  55. $site_endpoint = new Jetpack_Core_API_Site_Endpoint();
  56. $widget_endpoint = new Jetpack_Core_API_Widget_Endpoint();
  57. register_rest_route( 'jetpack/v4', 'plans', array(
  58. 'methods' => WP_REST_Server::READABLE,
  59. 'callback' => __CLASS__ . '::get_plans',
  60. 'permission_callback' => __CLASS__ . '::connect_url_permission_callback',
  61. ) );
  62. register_rest_route( 'jetpack/v4', '/jitm', array(
  63. 'methods' => WP_REST_Server::READABLE,
  64. 'callback' => __CLASS__ . '::get_jitm_message',
  65. ) );
  66. register_rest_route( 'jetpack/v4', '/jitm', array(
  67. 'methods' => WP_REST_Server::CREATABLE,
  68. 'callback' => __CLASS__ . '::delete_jitm_message'
  69. ) );
  70. // Register a site
  71. register_rest_route( 'jetpack/v4', '/verify_registration', array(
  72. 'methods' => WP_REST_Server::EDITABLE,
  73. 'callback' => __CLASS__ . '::verify_registration',
  74. ) );
  75. // Authorize a remote user
  76. register_rest_route( 'jetpack/v4', '/remote_authorize', array(
  77. 'methods' => WP_REST_Server::EDITABLE,
  78. 'callback' => __CLASS__ . '::remote_authorize',
  79. ) );
  80. // Get current connection status of Jetpack
  81. register_rest_route( 'jetpack/v4', '/connection', array(
  82. 'methods' => WP_REST_Server::READABLE,
  83. 'callback' => __CLASS__ . '::jetpack_connection_status',
  84. ) );
  85. register_rest_route( 'jetpack/v4', '/rewind', array(
  86. 'methods' => WP_REST_Server::READABLE,
  87. 'callback' => __CLASS__ . '::get_rewind_data',
  88. 'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
  89. ) );
  90. // Fetches a fresh connect URL
  91. register_rest_route( 'jetpack/v4', '/connection/url', array(
  92. 'methods' => WP_REST_Server::READABLE,
  93. 'callback' => __CLASS__ . '::build_connect_url',
  94. 'permission_callback' => __CLASS__ . '::connect_url_permission_callback',
  95. ) );
  96. // Get current user connection data
  97. register_rest_route( 'jetpack/v4', '/connection/data', array(
  98. 'methods' => WP_REST_Server::READABLE,
  99. 'callback' => __CLASS__ . '::get_user_connection_data',
  100. 'permission_callback' => __CLASS__ . '::get_user_connection_data_permission_callback',
  101. ) );
  102. // Set the connection owner
  103. register_rest_route( 'jetpack/v4', '/connection/owner', array(
  104. 'methods' => WP_REST_Server::EDITABLE,
  105. 'callback' => __CLASS__ . '::set_connection_owner',
  106. 'permission_callback' => __CLASS__ . '::set_connection_owner_permission_callback',
  107. ) );
  108. // Current user: get or set tracking settings.
  109. register_rest_route( 'jetpack/v4', '/tracking/settings', array(
  110. array(
  111. 'methods' => WP_REST_Server::READABLE,
  112. 'callback' => __CLASS__ . '::get_user_tracking_settings',
  113. 'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
  114. ),
  115. array(
  116. 'methods' => WP_REST_Server::EDITABLE,
  117. 'callback' => __CLASS__ . '::update_user_tracking_settings',
  118. 'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
  119. 'args' => array(
  120. 'tracks_opt_out' => array( 'type' => 'boolean' ),
  121. ),
  122. ),
  123. ) );
  124. // Disconnect site from WordPress.com servers
  125. register_rest_route( 'jetpack/v4', '/connection', array(
  126. 'methods' => WP_REST_Server::EDITABLE,
  127. 'callback' => __CLASS__ . '::disconnect_site',
  128. 'permission_callback' => __CLASS__ . '::disconnect_site_permission_callback',
  129. ) );
  130. // Disconnect/unlink user from WordPress.com servers
  131. register_rest_route( 'jetpack/v4', '/connection/user', array(
  132. 'methods' => WP_REST_Server::EDITABLE,
  133. 'callback' => __CLASS__ . '::unlink_user',
  134. 'permission_callback' => __CLASS__ . '::unlink_user_permission_callback',
  135. ) );
  136. // Get current site data
  137. register_rest_route( 'jetpack/v4', '/site', array(
  138. 'methods' => WP_REST_Server::READABLE,
  139. 'callback' => __CLASS__ . '::get_site_data',
  140. 'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
  141. ) );
  142. // Get current site data
  143. register_rest_route( 'jetpack/v4', '/site/features', array(
  144. 'methods' => WP_REST_Server::READABLE,
  145. 'callback' => array( $site_endpoint, 'get_features' ),
  146. 'permission_callback' => array( $site_endpoint , 'can_request' ),
  147. ) );
  148. // Confirm that a site in identity crisis should be in staging mode
  149. register_rest_route( 'jetpack/v4', '/identity-crisis/confirm-safe-mode', array(
  150. 'methods' => WP_REST_Server::EDITABLE,
  151. 'callback' => __CLASS__ . '::confirm_safe_mode',
  152. 'permission_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check',
  153. ) );
  154. // IDC resolve: create an entirely new shadow site for this URL.
  155. register_rest_route( 'jetpack/v4', '/identity-crisis/start-fresh', array(
  156. 'methods' => WP_REST_Server::EDITABLE,
  157. 'callback' => __CLASS__ . '::start_fresh_connection',
  158. 'permission_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check',
  159. ) );
  160. // Handles the request to migrate stats and subscribers during an identity crisis.
  161. register_rest_route( 'jetpack/v4', 'identity-crisis/migrate', array(
  162. 'methods' => WP_REST_Server::EDITABLE,
  163. 'callback' => __CLASS__ . '::migrate_stats_and_subscribers',
  164. 'permissison_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check',
  165. ) );
  166. // Return all modules
  167. register_rest_route( 'jetpack/v4', '/module/all', array(
  168. 'methods' => WP_REST_Server::READABLE,
  169. 'callback' => array( $module_list_endpoint, 'process' ),
  170. 'permission_callback' => array( $module_list_endpoint, 'can_request' ),
  171. ) );
  172. // Activate many modules
  173. register_rest_route( 'jetpack/v4', '/module/all/active', array(
  174. 'methods' => WP_REST_Server::EDITABLE,
  175. 'callback' => array( $module_list_endpoint, 'process' ),
  176. 'permission_callback' => array( $module_list_endpoint, 'can_request' ),
  177. 'args' => array(
  178. 'modules' => array(
  179. 'default' => '',
  180. 'type' => 'array',
  181. 'items' => array(
  182. 'type' => 'string',
  183. ),
  184. 'required' => true,
  185. 'validate_callback' => __CLASS__ . '::validate_module_list',
  186. ),
  187. 'active' => array(
  188. 'default' => true,
  189. 'type' => 'boolean',
  190. 'required' => false,
  191. 'validate_callback' => __CLASS__ . '::validate_boolean',
  192. ),
  193. )
  194. ) );
  195. // Return a single module and update it when needed
  196. register_rest_route( 'jetpack/v4', '/module/(?P<slug>[a-z\-]+)', array(
  197. 'methods' => WP_REST_Server::READABLE,
  198. 'callback' => array( $core_api_endpoint, 'process' ),
  199. 'permission_callback' => array( $core_api_endpoint, 'can_request' ),
  200. ) );
  201. // Activate and deactivate a module
  202. register_rest_route( 'jetpack/v4', '/module/(?P<slug>[a-z\-]+)/active', array(
  203. 'methods' => WP_REST_Server::EDITABLE,
  204. 'callback' => array( $module_toggle_endpoint, 'process' ),
  205. 'permission_callback' => array( $module_toggle_endpoint, 'can_request' ),
  206. 'args' => array(
  207. 'active' => array(
  208. 'default' => true,
  209. 'type' => 'boolean',
  210. 'required' => true,
  211. 'validate_callback' => __CLASS__ . '::validate_boolean',
  212. ),
  213. )
  214. ) );
  215. // Update a module
  216. register_rest_route( 'jetpack/v4', '/module/(?P<slug>[a-z\-]+)', array(
  217. 'methods' => WP_REST_Server::EDITABLE,
  218. 'callback' => array( $core_api_endpoint, 'process' ),
  219. 'permission_callback' => array( $core_api_endpoint, 'can_request' ),
  220. 'args' => self::get_updateable_parameters( 'any' )
  221. ) );
  222. // Get data for a specific module, i.e. Protect block count, WPCOM stats,
  223. // Akismet spam count, etc.
  224. register_rest_route( 'jetpack/v4', '/module/(?P<slug>[a-z\-]+)/data', array(
  225. 'methods' => WP_REST_Server::READABLE,
  226. 'callback' => array( $module_data_endpoint, 'process' ),
  227. 'permission_callback' => array( $module_data_endpoint, 'can_request' ),
  228. 'args' => array(
  229. 'range' => array(
  230. 'default' => 'day',
  231. 'type' => 'string',
  232. 'required' => false,
  233. 'validate_callback' => __CLASS__ . '::validate_string',
  234. ),
  235. )
  236. ) );
  237. // Check if the API key for a specific service is valid or not
  238. register_rest_route( 'jetpack/v4', '/module/(?P<service>[a-z\-]+)/key/check', array(
  239. 'methods' => WP_REST_Server::READABLE,
  240. 'callback' => array( $module_data_endpoint, 'key_check' ),
  241. 'permission_callback' => __CLASS__ . '::update_settings_permission_check',
  242. 'sanitize_callback' => 'sanitize_text_field',
  243. ) );
  244. register_rest_route( 'jetpack/v4', '/module/(?P<service>[a-z\-]+)/key/check', array(
  245. 'methods' => WP_REST_Server::EDITABLE,
  246. 'callback' => array( $module_data_endpoint, 'key_check' ),
  247. 'permission_callback' => __CLASS__ . '::update_settings_permission_check',
  248. 'sanitize_callback' => 'sanitize_text_field',
  249. 'args' => array(
  250. 'api_key' => array(
  251. 'default' => '',
  252. 'type' => 'string',
  253. 'validate_callback' => __CLASS__ . '::validate_alphanum',
  254. ),
  255. )
  256. ) );
  257. // Update any Jetpack module option or setting
  258. register_rest_route( 'jetpack/v4', '/settings', array(
  259. 'methods' => WP_REST_Server::EDITABLE,
  260. 'callback' => array( $core_api_endpoint, 'process' ),
  261. 'permission_callback' => array( $core_api_endpoint, 'can_request' ),
  262. 'args' => self::get_updateable_parameters( 'any' )
  263. ) );
  264. // Update a module
  265. register_rest_route( 'jetpack/v4', '/settings/(?P<slug>[a-z\-]+)', array(
  266. 'methods' => WP_REST_Server::EDITABLE,
  267. 'callback' => array( $core_api_endpoint, 'process' ),
  268. 'permission_callback' => array( $core_api_endpoint, 'can_request' ),
  269. 'args' => self::get_updateable_parameters()
  270. ) );
  271. // Return all module settings
  272. register_rest_route( 'jetpack/v4', '/settings/', array(
  273. 'methods' => WP_REST_Server::READABLE,
  274. 'callback' => array( $core_api_endpoint, 'process' ),
  275. 'permission_callback' => array( $core_api_endpoint, 'can_request' ),
  276. ) );
  277. // Reset all Jetpack options
  278. register_rest_route( 'jetpack/v4', '/options/(?P<options>[a-z\-]+)', array(
  279. 'methods' => WP_REST_Server::EDITABLE,
  280. 'callback' => __CLASS__ . '::reset_jetpack_options',
  281. 'permission_callback' => __CLASS__ . '::manage_modules_permission_check',
  282. ) );
  283. // Return current Jumpstart status
  284. register_rest_route( 'jetpack/v4', '/jumpstart', array(
  285. 'methods' => WP_REST_Server::READABLE,
  286. 'callback' => __CLASS__ . '::jumpstart_status',
  287. 'permission_callback' => __CLASS__ . '::update_settings_permission_check',
  288. ) );
  289. // Update Jumpstart
  290. register_rest_route( 'jetpack/v4', '/jumpstart', array(
  291. 'methods' => WP_REST_Server::EDITABLE,
  292. 'callback' => __CLASS__ . '::jumpstart_toggle',
  293. 'permission_callback' => __CLASS__ . '::manage_modules_permission_check',
  294. 'args' => array(
  295. 'active' => array(
  296. 'required' => true,
  297. 'validate_callback' => __CLASS__ . '::validate_boolean',
  298. ),
  299. ),
  300. ) );
  301. // Updates: get number of plugin updates available
  302. register_rest_route( 'jetpack/v4', '/updates/plugins', array(
  303. 'methods' => WP_REST_Server::READABLE,
  304. 'callback' => __CLASS__ . '::get_plugin_update_count',
  305. 'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
  306. ) );
  307. // Dismiss Jetpack Notices
  308. register_rest_route( 'jetpack/v4', '/notice/(?P<notice>[a-z\-_]+)', array(
  309. 'methods' => WP_REST_Server::EDITABLE,
  310. 'callback' => __CLASS__ . '::dismiss_notice',
  311. 'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
  312. ) );
  313. // Plugins: get list of all plugins.
  314. register_rest_route( 'jetpack/v4', '/plugins', array(
  315. 'methods' => WP_REST_Server::READABLE,
  316. 'callback' => __CLASS__ . '::get_plugins',
  317. 'permission_callback' => __CLASS__ . '::activate_plugins_permission_check',
  318. ) );
  319. // Plugins: check if the plugin is active.
  320. register_rest_route( 'jetpack/v4', '/plugin/(?P<plugin>[a-z\/\.\-_]+)', array(
  321. 'methods' => WP_REST_Server::READABLE,
  322. 'callback' => __CLASS__ . '::get_plugin',
  323. 'permission_callback' => __CLASS__ . '::activate_plugins_permission_check',
  324. ) );
  325. // Widgets: get information about a widget that supports it.
  326. register_rest_route( 'jetpack/v4', '/widgets/(?P<id>[0-9a-z\-_]+)', array(
  327. 'methods' => WP_REST_Server::READABLE,
  328. 'callback' => array( $widget_endpoint, 'process' ),
  329. 'permission_callback' => array( $widget_endpoint, 'can_request' ),
  330. ) );
  331. }
  332. public static function get_plans( $request ) {
  333. $request = Jetpack_Client::wpcom_json_api_request_as_user(
  334. '/plans?_locale=' . get_user_locale(),
  335. '2',
  336. array(
  337. 'method' => 'GET',
  338. 'headers' => array(
  339. 'X-Forwarded-For' => Jetpack::current_user_ip( true ),
  340. ),
  341. )
  342. );
  343. $body = wp_remote_retrieve_body( $request );
  344. if ( 200 === wp_remote_retrieve_response_code( $request ) ) {
  345. $data = $body;
  346. } else {
  347. // something went wrong so we'll just return the response without caching
  348. return $body;
  349. }
  350. return $data;
  351. }
  352. /**
  353. * Asks for a jitm, unless they've been disabled, in which case it returns an empty array
  354. *
  355. * @param $request WP_REST_Request
  356. *
  357. * @return array An array of jitms
  358. */
  359. public static function get_jitm_message( $request ) {
  360. require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-jitm.php' );
  361. $jitm = Jetpack_JITM::init();
  362. if ( ! $jitm ) {
  363. return array();
  364. }
  365. return $jitm->get_messages( $request['message_path'], urldecode_deep( $request['query'] ) );
  366. }
  367. /**
  368. * Dismisses a jitm
  369. * @param $request WP_REST_Request The request
  370. *
  371. * @return bool Always True
  372. */
  373. public static function delete_jitm_message( $request ) {
  374. require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-jitm.php' );
  375. $jitm = Jetpack_JITM::init();
  376. if ( ! $jitm ) {
  377. return true;
  378. }
  379. return $jitm->dismiss( $request['id'], $request['feature_class'] );
  380. }
  381. /**
  382. * Handles verification that a site is registered
  383. *
  384. * @since 5.4.0
  385. *
  386. * @param WP_REST_Request $request The request sent to the WP REST API.
  387. *
  388. * @return array|wp-error
  389. */
  390. public static function verify_registration( $request ) {
  391. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php';
  392. $xmlrpc_server = new Jetpack_XMLRPC_Server();
  393. $result = $xmlrpc_server->verify_registration( array( $request['secret_1'], $request['state'] ) );
  394. if ( is_a( $result, 'IXR_Error' ) ) {
  395. $result = new WP_Error( $result->code, $result->message );
  396. }
  397. return $result;
  398. }
  399. /**
  400. * Handles verification that a site is registered
  401. *
  402. * @since 5.4.0
  403. *
  404. * @param WP_REST_Request $request The request sent to the WP REST API.
  405. *
  406. * @return array|wp-error
  407. */
  408. public static function remote_authorize( $request ) {
  409. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php';
  410. $xmlrpc_server = new Jetpack_XMLRPC_Server();
  411. $result = $xmlrpc_server->remote_authorize( $request );
  412. if ( is_a( $result, 'IXR_Error' ) ) {
  413. $result = new WP_Error( $result->code, $result->message );
  414. }
  415. return $result;
  416. }
  417. /**
  418. * Handles dismissing of Jetpack Notices
  419. *
  420. * @since 4.3.0
  421. *
  422. * @param WP_REST_Request $request The request sent to the WP REST API.
  423. *
  424. * @return array|wp-error
  425. */
  426. public static function dismiss_notice( $request ) {
  427. $notice = $request['notice'];
  428. if ( ! isset( $request['dismissed'] ) || $request['dismissed'] !== true ) {
  429. return new WP_Error( 'invalid_param', esc_html__( 'Invalid parameter "dismissed".', 'jetpack' ), array( 'status' => 404 ) );
  430. }
  431. if ( isset( $notice ) && ! empty( $notice ) ) {
  432. switch( $notice ) {
  433. case 'feedback_dash_request':
  434. case 'welcome':
  435. $notices = get_option( 'jetpack_dismissed_notices', array() );
  436. $notices[ $notice ] = true;
  437. update_option( 'jetpack_dismissed_notices', $notices );
  438. return rest_ensure_response( get_option( 'jetpack_dismissed_notices', array() ) );
  439. default:
  440. return new WP_Error( 'invalid_param', esc_html__( 'Invalid parameter "notice".', 'jetpack' ), array( 'status' => 404 ) );
  441. }
  442. }
  443. return new WP_Error( 'required_param', esc_html__( 'Missing parameter "notice".', 'jetpack' ), array( 'status' => 404 ) );
  444. }
  445. /**
  446. * Verify that the user can disconnect the site.
  447. *
  448. * @since 4.3.0
  449. *
  450. * @return bool|WP_Error True if user is able to disconnect the site.
  451. */
  452. public static function disconnect_site_permission_callback() {
  453. if ( current_user_can( 'jetpack_disconnect' ) ) {
  454. return true;
  455. }
  456. return new WP_Error( 'invalid_user_permission_jetpack_disconnect', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
  457. }
  458. /**
  459. * Verify that the user can get a connect/link URL
  460. *
  461. * @since 4.3.0
  462. *
  463. * @return bool|WP_Error True if user is able to disconnect the site.
  464. */
  465. public static function connect_url_permission_callback() {
  466. if ( current_user_can( 'jetpack_connect_user' ) ) {
  467. return true;
  468. }
  469. return new WP_Error( 'invalid_user_permission_jetpack_disconnect', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
  470. }
  471. /**
  472. * Verify that a user can get the data about the current user.
  473. * Only those who can connect.
  474. *
  475. * @since 4.3.0
  476. *
  477. * @uses Jetpack::is_user_connected();
  478. *
  479. * @return bool|WP_Error True if user is able to unlink.
  480. */
  481. public static function get_user_connection_data_permission_callback() {
  482. if ( current_user_can( 'jetpack_connect_user' ) ) {
  483. return true;
  484. }
  485. return new WP_Error( 'invalid_user_permission_user_connection_data', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
  486. }
  487. /**
  488. * Check that user has permission to change the master user.
  489. *
  490. * @since 6.2.0
  491. *
  492. * @return bool|WP_Error True if user is able to change master user.
  493. */
  494. public static function set_connection_owner_permission_callback() {
  495. if ( get_current_user_id() === Jetpack_Options::get_option( 'master_user' ) ) {
  496. return true;
  497. }
  498. return new WP_Error( 'invalid_user_permission_set_connection_owner', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
  499. }
  500. /**
  501. * Verify that a user can use the /connection/user endpoint. Has to be a registered user and be currently linked.
  502. *
  503. * @since 4.3.0
  504. *
  505. * @uses Jetpack::is_user_connected();
  506. *
  507. * @return bool|WP_Error True if user is able to unlink.
  508. */
  509. public static function unlink_user_permission_callback() {
  510. if ( current_user_can( 'jetpack_connect_user' ) && Jetpack::is_user_connected( get_current_user_id() ) ) {
  511. return true;
  512. }
  513. return new WP_Error( 'invalid_user_permission_unlink_user', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
  514. }
  515. /**
  516. * Verify that user can manage Jetpack modules.
  517. *
  518. * @since 4.3.0
  519. *
  520. * @return bool Whether user has the capability 'jetpack_manage_modules'.
  521. */
  522. public static function manage_modules_permission_check() {
  523. if ( current_user_can( 'jetpack_manage_modules' ) ) {
  524. return true;
  525. }
  526. return new WP_Error( 'invalid_user_permission_manage_modules', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
  527. }
  528. /**
  529. * Verify that user can update Jetpack modules.
  530. *
  531. * @since 4.3.0
  532. *
  533. * @return bool Whether user has the capability 'jetpack_configure_modules'.
  534. */
  535. public static function configure_modules_permission_check() {
  536. if ( current_user_can( 'jetpack_configure_modules' ) ) {
  537. return true;
  538. }
  539. return new WP_Error( 'invalid_user_permission_configure_modules', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
  540. }
  541. /**
  542. * Verify that user can view Jetpack admin page.
  543. *
  544. * @since 4.3.0
  545. *
  546. * @return bool Whether user has the capability 'jetpack_admin_page'.
  547. */
  548. public static function view_admin_page_permission_check() {
  549. if ( current_user_can( 'jetpack_admin_page' ) ) {
  550. return true;
  551. }
  552. return new WP_Error( 'invalid_user_permission_view_admin', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
  553. }
  554. /**
  555. * Verify that user can mitigate an identity crisis.
  556. *
  557. * @since 4.4.0
  558. *
  559. * @return bool Whether user has capability 'jetpack_disconnect'.
  560. */
  561. public static function identity_crisis_mitigation_permission_check() {
  562. if ( current_user_can( 'jetpack_disconnect' ) ) {
  563. return true;
  564. }
  565. return new WP_Error( 'invalid_user_permission_identity_crisis', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
  566. }
  567. /**
  568. * Verify that user can update Jetpack general settings.
  569. *
  570. * @since 4.3.0
  571. *
  572. * @return bool Whether user has the capability 'update_settings_permission_check'.
  573. */
  574. public static function update_settings_permission_check() {
  575. if ( current_user_can( 'jetpack_configure_modules' ) ) {
  576. return true;
  577. }
  578. return new WP_Error( 'invalid_user_permission_manage_settings', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
  579. }
  580. /**
  581. * Verify that user can view Jetpack admin page and can activate plugins.
  582. *
  583. * @since 4.3.0
  584. *
  585. * @return bool Whether user has the capability 'jetpack_admin_page' and 'activate_plugins'.
  586. */
  587. public static function activate_plugins_permission_check() {
  588. if ( current_user_can( 'jetpack_admin_page' ) && current_user_can( 'activate_plugins' ) ) {
  589. return true;
  590. }
  591. return new WP_Error( 'invalid_user_permission_activate_plugins', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
  592. }
  593. /**
  594. * Contextual HTTP error code for authorization failure.
  595. *
  596. * Taken from rest_authorization_required_code() in WP-API plugin until is added to core.
  597. * @see https://github.com/WP-API/WP-API/commit/7ba0ae6fe4f605d5ffe4ee85b1cd5f9fb46900a6
  598. *
  599. * @since 4.3.0
  600. *
  601. * @return int
  602. */
  603. public static function rest_authorization_required_code() {
  604. return is_user_logged_in() ? 403 : 401;
  605. }
  606. /**
  607. * Get connection status for this Jetpack site.
  608. *
  609. * @since 4.3.0
  610. *
  611. * @return bool True if site is connected
  612. */
  613. public static function jetpack_connection_status() {
  614. return rest_ensure_response( array(
  615. 'isActive' => Jetpack::is_active(),
  616. 'isStaging' => Jetpack::is_staging_site(),
  617. 'devMode' => array(
  618. 'isActive' => Jetpack::is_development_mode(),
  619. 'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG,
  620. 'url' => site_url() && false === strpos( site_url(), '.' ),
  621. 'filter' => apply_filters( 'jetpack_development_mode', false ),
  622. ),
  623. )
  624. );
  625. }
  626. public static function rewind_data() {
  627. $site_id = Jetpack_Options::get_option( 'id' );
  628. if ( ! $site_id ) {
  629. return new WP_Error( 'site_id_missing' );
  630. }
  631. $response = Jetpack_Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d/rewind', $site_id ) .'?force=wpcom', '2', array(), null, 'wpcom' );
  632. if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
  633. return new WP_Error( 'rewind_data_fetch_failed' );
  634. }
  635. $body = wp_remote_retrieve_body( $response );
  636. return json_decode( $body );
  637. }
  638. /**
  639. * Get rewind data
  640. *
  641. * @since 5.7.0
  642. *
  643. * @return array Array of rewind properties.
  644. */
  645. public static function get_rewind_data() {
  646. $rewind_data = self::rewind_data();
  647. if ( ! is_wp_error( $rewind_data ) ) {
  648. return rest_ensure_response( array(
  649. 'code' => 'success',
  650. 'message' => esc_html__( 'Rewind data correctly received.', 'jetpack' ),
  651. 'data' => wp_json_encode( $rewind_data ),
  652. )
  653. );
  654. }
  655. if ( $rewind_data->get_error_code() === 'rewind_data_fetch_failed' ) {
  656. return new WP_Error( 'rewind_data_fetch_failed', esc_html__( 'Failed fetching rewind data. Try again later.', 'jetpack' ), array( 'status' => 400 ) );
  657. }
  658. if ( $rewind_data->get_error_code() === 'site_id_missing' ) {
  659. return new WP_Error( 'site_id_missing', esc_html__( 'The ID of this site does not exist.', 'jetpack' ), array( 'status' => 404 ) );
  660. }
  661. return new WP_Error(
  662. 'error_get_rewind_data',
  663. esc_html__( 'Could not retrieve Rewind data.', 'jetpack' ),
  664. array( 'status' => 500 )
  665. );
  666. }
  667. /**
  668. * Disconnects Jetpack from the WordPress.com Servers
  669. *
  670. * @uses Jetpack::disconnect();
  671. * @since 4.3.0
  672. *
  673. * @param WP_REST_Request $request The request sent to the WP REST API.
  674. *
  675. * @return bool|WP_Error True if Jetpack successfully disconnected.
  676. */
  677. public static function disconnect_site( $request ) {
  678. if ( ! isset( $request['isActive'] ) || $request['isActive'] !== false ) {
  679. return new WP_Error( 'invalid_param', esc_html__( 'Invalid Parameter', 'jetpack' ), array( 'status' => 404 ) );
  680. }
  681. if ( Jetpack::is_active() ) {
  682. Jetpack::disconnect();
  683. return rest_ensure_response( array( 'code' => 'success' ) );
  684. }
  685. return new WP_Error( 'disconnect_failed', esc_html__( 'Was not able to disconnect the site. Please try again.', 'jetpack' ), array( 'status' => 400 ) );
  686. }
  687. /**
  688. * Gets a new connect raw URL with fresh nonce.
  689. *
  690. * @uses Jetpack::disconnect();
  691. * @since 4.3.0
  692. *
  693. * @param WP_REST_Request $request The request sent to the WP REST API.
  694. *
  695. * @return string|WP_Error A raw URL if the connection URL could be built; error message otherwise.
  696. */
  697. public static function build_connect_url() {
  698. $url = Jetpack::init()->build_connect_url( true, false, false );
  699. if ( $url ) {
  700. return rest_ensure_response( $url );
  701. }
  702. return new WP_Error( 'build_connect_url_failed', esc_html__( 'Unable to build the connect URL. Please reload the page and try again.', 'jetpack' ), array( 'status' => 400 ) );
  703. }
  704. /**
  705. * Get miscellaneous user data related to the connection. Similar data available in old "My Jetpack".
  706. * Information about the master/primary user.
  707. * Information about the current user.
  708. *
  709. * @since 4.3.0
  710. *
  711. * @param WP_REST_Request $request The request sent to the WP REST API.
  712. *
  713. * @return object
  714. */
  715. public static function get_user_connection_data() {
  716. require_once( JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-react-page.php' );
  717. $response = array(
  718. // 'othersLinked' => Jetpack::get_other_linked_admins(),
  719. 'currentUser' => jetpack_current_user_data(),
  720. );
  721. return rest_ensure_response( $response );
  722. }
  723. /**
  724. * Change the master user.
  725. *
  726. * @since 6.2.0
  727. *
  728. * @param WP_REST_Request $request The request sent to the WP REST API.
  729. *
  730. * @return bool|WP_Error True if owner successfully changed.
  731. */
  732. public static function set_connection_owner( $request ) {
  733. if ( ! isset( $request['owner'] ) ) {
  734. return new WP_Error(
  735. 'invalid_param',
  736. esc_html__( 'Invalid Parameter', 'jetpack' ),
  737. array( 'status' => 400 )
  738. );
  739. }
  740. $new_owner_id = $request['owner'];
  741. if ( ! user_can( $new_owner_id, 'administrator' ) ) {
  742. return new WP_Error(
  743. 'new_owner_not_admin',
  744. esc_html__( 'New owner is not admin', 'jetpack' ),
  745. array( 'status' => 400 )
  746. );
  747. }
  748. if ( $new_owner_id === get_current_user_id() ) {
  749. return new WP_Error(
  750. 'new_owner_is_current_user',
  751. esc_html__( 'New owner is same as current user', 'jetpack' ),
  752. array( 'status' => 400 )
  753. );
  754. }
  755. if ( ! Jetpack::is_user_connected( $new_owner_id ) ) {
  756. return new WP_Error(
  757. 'new_owner_not_connected',
  758. esc_html__( 'New owner is not connected', 'jetpack' ),
  759. array( 'status' => 400 )
  760. );
  761. }
  762. // Update the master user in Jetpack
  763. $updated = Jetpack_Options::update_option( 'master_user', $new_owner_id );
  764. // Notify WPCOM about the master user change
  765. Jetpack::load_xml_rpc_client();
  766. $xml = new Jetpack_IXR_Client( array(
  767. 'user_id' => get_current_user_id(),
  768. ) );
  769. $xml->query( 'jetpack.switchBlogOwner', array(
  770. 'new_blog_owner' => $new_owner_id,
  771. ) );
  772. if ( $updated && ! $xml->isError() ) {
  773. return rest_ensure_response(
  774. array(
  775. 'code' => 'success',
  776. )
  777. );
  778. }
  779. return new WP_Error(
  780. 'error_setting_new_owner',
  781. esc_html__( 'Could not confirm new owner.', 'jetpack' ),
  782. array( 'status' => 500 )
  783. );
  784. }
  785. /**
  786. * Unlinks current user from the WordPress.com Servers.
  787. *
  788. * @since 4.3.0
  789. * @uses Jetpack::unlink_user
  790. *
  791. * @param WP_REST_Request $request The request sent to the WP REST API.
  792. *
  793. * @return bool|WP_Error True if user successfully unlinked.
  794. */
  795. public static function unlink_user( $request ) {
  796. if ( ! isset( $request['linked'] ) || $request['linked'] !== false ) {
  797. return new WP_Error( 'invalid_param', esc_html__( 'Invalid Parameter', 'jetpack' ), array( 'status' => 404 ) );
  798. }
  799. if ( Jetpack::unlink_user() ) {
  800. return rest_ensure_response(
  801. array(
  802. 'code' => 'success'
  803. )
  804. );
  805. }
  806. return new WP_Error( 'unlink_user_failed', esc_html__( 'Was not able to unlink the user. Please try again.', 'jetpack' ), array( 'status' => 400 ) );
  807. }
  808. /**
  809. * Gets current user's tracking settings.
  810. *
  811. * @since 6.0.0
  812. *
  813. * @param WP_REST_Request $request The request sent to the WP REST API.
  814. *
  815. * @return WP_REST_Response|WP_Error Response, else error.
  816. */
  817. public static function get_user_tracking_settings( $request ) {
  818. if ( ! Jetpack::is_user_connected() ) {
  819. $response = array(
  820. 'tracks_opt_out' => true, // Default to opt-out if not connected to wp.com.
  821. );
  822. } else {
  823. $response = Jetpack_Client::wpcom_json_api_request_as_user(
  824. '/jetpack-user-tracking',
  825. 'v2',
  826. array(
  827. 'method' => 'GET',
  828. 'headers' => array(
  829. 'X-Forwarded-For' => Jetpack::current_user_ip( true ),
  830. ),
  831. )
  832. );
  833. if ( ! is_wp_error( $response ) ) {
  834. $response = json_decode( wp_remote_retrieve_body( $response ), true );
  835. }
  836. }
  837. return rest_ensure_response( $response );
  838. }
  839. /**
  840. * Updates current user's tracking settings.
  841. *
  842. * @since 6.0.0
  843. *
  844. * @param WP_REST_Request $request The request sent to the WP REST API.
  845. *
  846. * @return WP_REST_Response|WP_Error Response, else error.
  847. */
  848. public static function update_user_tracking_settings( $request ) {
  849. if ( ! Jetpack::is_user_connected() ) {
  850. $response = array(
  851. 'tracks_opt_out' => true, // Default to opt-out if not connected to wp.com.
  852. );
  853. } else {
  854. $response = Jetpack_Client::wpcom_json_api_request_as_user(
  855. '/jetpack-user-tracking',
  856. 'v2',
  857. array(
  858. 'method' => 'PUT',
  859. 'headers' => array(
  860. 'Content-Type' => 'application/json',
  861. 'X-Forwarded-For' => Jetpack::current_user_ip( true ),
  862. ),
  863. ),
  864. wp_json_encode( $request->get_params() )
  865. );
  866. if ( ! is_wp_error( $response ) ) {
  867. $response = json_decode( wp_remote_retrieve_body( $response ), true );
  868. }
  869. }
  870. return rest_ensure_response( $response );
  871. }
  872. /**
  873. * Fetch site data from .com including the site's current plan.
  874. *
  875. * @since 5.5.0
  876. *
  877. * @return array Array of site properties.
  878. */
  879. public static function site_data() {
  880. $site_id = Jetpack_Options::get_option( 'id' );
  881. if ( ! $site_id ) {
  882. new WP_Error( 'site_id_missing' );
  883. }
  884. $response = Jetpack_Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d', $site_id ) .'?force=wpcom', '1.1' );
  885. if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
  886. return new WP_Error( 'site_data_fetch_failed' );
  887. }
  888. // Save plan details in the database for future use without API calls
  889. $results = json_decode( $response['body'], true );
  890. if ( is_array( $results ) && isset( $results['plan'] ) ) {
  891. // Set flag for newly purchased plan
  892. $current_plan = Jetpack::get_active_plan();
  893. if ( $current_plan['product_slug'] !== $results['plan']['product_slug'] && 'jetpack_free' !== $results['plan']['product_slug'] ) {
  894. update_option( 'show_welcome_for_new_plan', true ) ;
  895. }
  896. update_option( 'jetpack_active_plan', $results['plan'] );
  897. }
  898. $body = wp_remote_retrieve_body( $response );
  899. return json_decode( $body );
  900. }
  901. /**
  902. * Get site data, including for example, the site's current plan.
  903. *
  904. * @since 4.3.0
  905. *
  906. * @return array Array of site properties.
  907. */
  908. public static function get_site_data() {
  909. $site_data = self::site_data();
  910. if ( ! is_wp_error( $site_data ) ) {
  911. return rest_ensure_response( array(
  912. 'code' => 'success',
  913. 'message' => esc_html__( 'Site data correctly received.', 'jetpack' ),
  914. 'data' => json_encode( $site_data ),
  915. )
  916. );
  917. }
  918. if ( $site_data->get_error_code() === 'site_data_fetch_failed' ) {
  919. return new WP_Error( 'site_data_fetch_failed', esc_html__( 'Failed fetching site data. Try again later.', 'jetpack' ), array( 'status' => 400 ) );
  920. }
  921. if ( $site_data->get_error_code() === 'site_id_missing' ) {
  922. return new WP_Error( 'site_id_missing', esc_html__( 'The ID of this site does not exist.', 'jetpack' ), array( 'status' => 404 ) );
  923. }
  924. }
  925. /**
  926. * Handles identity crisis mitigation, confirming safe mode for this site.
  927. *
  928. * @since 4.4.0
  929. *
  930. * @return bool | WP_Error True if option is properly set.
  931. */
  932. public static function confirm_safe_mode() {
  933. $updated = Jetpack_Options::update_option( 'safe_mode_confirmed', true );
  934. if ( $updated ) {
  935. return rest_ensure_response(
  936. array(
  937. 'code' => 'success'
  938. )
  939. );
  940. }
  941. return new WP_Error(
  942. 'error_setting_jetpack_safe_mode',
  943. esc_html__( 'Could not confirm safe mode.', 'jetpack' ),
  944. array( 'status' => 500 )
  945. );
  946. }
  947. /**
  948. * Handles identity crisis mitigation, migrating stats and subscribers from old url to this, new url.
  949. *
  950. * @since 4.4.0
  951. *
  952. * @return bool | WP_Error True if option is properly set.
  953. */
  954. public static function migrate_stats_and_subscribers() {
  955. if ( Jetpack_Options::get_option( 'sync_error_idc' ) && ! Jetpack_Options::delete_option( 'sync_error_idc' ) ) {
  956. return new WP_Error(
  957. 'error_deleting_sync_error_idc',
  958. esc_html__( 'Could not delete sync error option.', 'jetpack' ),
  959. array( 'status' => 500 )
  960. );
  961. }
  962. if ( Jetpack_Options::get_option( 'migrate_for_idc' ) || Jetpack_Options::update_option( 'migrate_for_idc', true ) ) {
  963. return rest_ensure_response(
  964. array(
  965. 'code' => 'success'
  966. )
  967. );
  968. }
  969. return new WP_Error(
  970. 'error_setting_jetpack_migrate',
  971. esc_html__( 'Could not confirm migration.', 'jetpack' ),
  972. array( 'status' => 500 )
  973. );
  974. }
  975. /**
  976. * This IDC resolution will disconnect the site and re-connect to a completely new
  977. * and separate shadow site than the original.
  978. *
  979. * It will first will disconnect the site without phoning home as to not disturb the production site.
  980. * It then builds a fresh connection URL and sends it back along with the response.
  981. *
  982. * @since 4.4.0
  983. * @return bool|WP_Error
  984. */
  985. public static function start_fresh_connection() {
  986. // First clear the options / disconnect.
  987. Jetpack::disconnect();
  988. return self::build_connect_url();
  989. }
  990. /**
  991. * Reset Jetpack options
  992. *
  993. * @since 4.3.0
  994. *
  995. * @param WP_REST_Request $request {
  996. * Array of parameters received by request.
  997. *
  998. * @type string $options Available options to reset are options|modules
  999. * }
  1000. *
  1001. * @return bool|WP_Error True if options were reset. Otherwise, a WP_Error instance with the corresponding error.
  1002. */
  1003. public static function reset_jetpack_options( $request ) {
  1004. if ( ! isset( $request['reset'] ) || $request['reset'] !== true ) {
  1005. return new WP_Error( 'invalid_param', esc_html__( 'Invalid Parameter', 'jetpack' ), array( 'status' => 404 ) );
  1006. }
  1007. if ( isset( $request['options'] ) ) {
  1008. $data = $request['options'];
  1009. switch( $data ) {
  1010. case ( 'options' ) :
  1011. $options_to_reset = Jetpack::get_jetpack_options_for_reset();
  1012. // Reset the Jetpack options
  1013. foreach ( $options_to_reset['jp_options'] as $option_to_reset ) {
  1014. Jetpack_Options::delete_option( $option_to_reset );
  1015. }
  1016. foreach ( $options_to_reset['wp_options'] as $option_to_reset ) {
  1017. delete_option( $option_to_reset );
  1018. }
  1019. // Reset to default modules
  1020. $default_modules = Jetpack::get_default_modules();
  1021. Jetpack::update_active_modules( $default_modules );
  1022. // Jumpstart option is special
  1023. Jetpack_Options::update_option( 'jumpstart', 'new_connection' );
  1024. return rest_ensure_response( array(
  1025. 'code' => 'success',
  1026. 'message' => esc_html__( 'Jetpack options reset.', 'jetpack' ),
  1027. ) );
  1028. break;
  1029. case 'modules':
  1030. $default_modules = Jetpack::get_default_modules();
  1031. Jetpack::update_active_modules( $default_modules );
  1032. return rest_ensure_response( array(
  1033. 'code' => 'success',
  1034. 'message' => esc_html__( 'Modules reset to default.', 'jetpack' ),
  1035. ) );
  1036. break;
  1037. default:
  1038. return new WP_Error( 'invalid_param', esc_html__( 'Invalid Parameter', 'jetpack' ), array( 'status' => 404 ) );
  1039. }
  1040. }
  1041. return new WP_Error( 'required_param', esc_html__( 'Missing parameter "type".', 'jetpack' ), array( 'status' => 404 ) );
  1042. }
  1043. /**
  1044. * Retrieves the current status of Jumpstart.
  1045. *
  1046. * @since 4.5.0
  1047. *
  1048. * @return bool
  1049. */
  1050. public static function jumpstart_status() {
  1051. return array(
  1052. 'status' => Jetpack_Options::get_option( 'jumpstart' )
  1053. );
  1054. }
  1055. /**
  1056. * Toggles activation or deactivation of the JumpStart
  1057. *
  1058. * @since 4.3.0
  1059. *
  1060. * @param WP_REST_Request $request The request sent to the WP REST API.
  1061. *
  1062. * @return bool|WP_Error True if toggling Jumpstart succeeded. Otherwise, a WP_Error instance with the corresponding error.
  1063. */
  1064. public static function jumpstart_toggle( $request ) {
  1065. if ( $request[ 'active' ] ) {
  1066. return self::jumpstart_activate( $request );
  1067. } else {
  1068. return self::jumpstart_deactivate( $request );
  1069. }
  1070. }
  1071. /**
  1072. * Activates a series of valid Jetpack modules and initializes some options.
  1073. *
  1074. * @since 4.3.0
  1075. *
  1076. * @param WP_REST_Request $request The request sent to the WP REST API.
  1077. *
  1078. * @return bool|WP_Error True if Jumpstart succeeded. Otherwise, a WP_Error instance with the corresponding error.
  1079. */
  1080. public static function jumpstart_activate( $request ) {
  1081. $modules = Jetpack::get_available_modules();
  1082. $activate_modules = array();
  1083. foreach ( $modules as $module ) {
  1084. $module_info = Jetpack::get_module( $module );
  1085. if ( isset( $module_info['feature'] ) && is_array( $module_info['feature'] ) && in_array( 'Jumpstart', $module_info['feature'] ) ) {
  1086. $activate_modules[] = $module;
  1087. }
  1088. }
  1089. // Collect success/error messages like modules that are properly activated.
  1090. $result = array(
  1091. 'activated_modules' => array(),
  1092. 'failed_modules' => array(),
  1093. );
  1094. // Update the jumpstart option
  1095. if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) {
  1096. $result['jumpstart_activated'] = Jetpack_Options::update_option( 'jumpstart', 'jumpstart_activated' );
  1097. }
  1098. // Check for possible conflicting plugins
  1099. $module_slugs_filtered = Jetpack::init()->filter_default_modules( $activate_modules );
  1100. foreach ( $module_slugs_filtered as $module_slug ) {
  1101. Jetpack::log( 'activate', $module_slug );
  1102. if ( Jetpack::activate_module( $module_slug, false, false ) ) {
  1103. $result['activated_modules'][] = $module_slug;
  1104. } else {
  1105. $result['failed_modules'][] = $module_slug;
  1106. }
  1107. }
  1108. // Set the default sharing buttons and set to display on posts if none have been set.
  1109. $sharing_services = get_option( 'sharing-services' );
  1110. $sharing_options = get_option( 'sharing-options' );
  1111. if ( empty( $sharing_services['visible'] ) ) {
  1112. // Default buttons to set
  1113. $visible = array(
  1114. 'twitter',
  1115. 'facebook',
  1116. 'google-plus-1',
  1117. );
  1118. $hidden = array();
  1119. // Set some sharing settings
  1120. if ( class_exists( 'Sharing_Service' ) ) {
  1121. $sharing = new Sharing_Service();
  1122. $sharing_options['global'] = array(
  1123. 'button_style' => 'icon',
  1124. 'sharing_label' => $sharing->default_sharing_label,
  1125. 'open_links' => 'same',
  1126. 'show' => array( 'post' ),
  1127. 'custom' => isset( $sharing_options['global']['custom'] ) ? $sharing_options['global']['custom'] : array()
  1128. );
  1129. $result['sharing_options'] = update_option( 'sharing-options', $sharing_options );
  1130. $result['sharing_services'] = update_option( 'sharing-services', array( 'visible' => $visible, 'hidden' => $hidden ) );
  1131. }
  1132. }
  1133. // If all Jumpstart modules were activated
  1134. if ( empty( $result['failed_modules'] ) ) {
  1135. return rest_ensure_response( array(
  1136. 'code' => 'success',
  1137. 'message' => esc_html__( 'Jumpstart done.', 'jetpack' ),
  1138. 'data' => $result,
  1139. ) );
  1140. }
  1141. return new WP_Error( 'jumpstart_failed', esc_html( sprintf( _n( 'Jumpstart failed activating this module: %s.', 'Jumpstart failed activating these modules: %s.', count( $result['failed_modules'] ), 'jetpack' ), join( ', ', $result['failed_modules'] ) ) ), array( 'status' => 400 ) );
  1142. }
  1143. /**
  1144. * Dismisses Jumpstart so user is not prompted to go through it again.
  1145. *
  1146. * @since 4.3.0
  1147. *
  1148. * @param WP_REST_Request $request The request sent to the WP REST API.
  1149. *
  1150. * @return bool|WP_Error True if Jumpstart was disabled or was nothing to dismiss. Otherwise, a WP_Error instance with a message.
  1151. */
  1152. public static function jumpstart_deactivate( $request ) {
  1153. // If dismissed, flag the jumpstart option as such.
  1154. if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) {
  1155. if ( Jetpack_Options::update_option( 'jumpstart', 'jumpstart_dismissed' ) ) {
  1156. return rest_ensure_response( array(
  1157. 'code' => 'success',
  1158. 'message' => esc_html__( 'Jumpstart dismissed.', 'jetpack' ),
  1159. ) );
  1160. } else {
  1161. return new WP_Error( 'jumpstart_failed_dismiss', esc_html__( 'Jumpstart could not be dismissed.', 'jetpack' ), array( 'status' => 400 ) );
  1162. }
  1163. }
  1164. // If this was not a new connection and there was nothing to dismiss, don't fail.
  1165. return rest_ensure_response( array(
  1166. 'code' => 'success',
  1167. 'message' => esc_html__( 'Nothing to dismiss. This was not a new connection.', 'jetpack' ),
  1168. ) );
  1169. }
  1170. /**
  1171. * Get the query parameters to update module options or general settings.
  1172. *
  1173. * @since 4.3.0
  1174. * @since 4.4.0 Accepts a $selector parameter.
  1175. *
  1176. * @param string $selector Selects a set of options to update, Can be empty, a module slug or 'any'.
  1177. *
  1178. * @return array
  1179. */
  1180. public static function get_updateable_parameters( $selector = '' ) {
  1181. $parameters = array(
  1182. 'context' => array(
  1183. 'default' => 'edit',
  1184. ),
  1185. );
  1186. return array_merge( $parameters, self::get_updateable_data_list( $selector ) );
  1187. }
  1188. /**
  1189. * Returns a list of module options or general settings that can be updated.
  1190. *
  1191. * @since 4.3.0
  1192. * @since 4.4.0 Accepts 'any' as a parameter which will make it return the entire list.
  1193. *
  1194. * @param string|array $selector Module slug, 'any', or an array of parameters.
  1195. * If empty, it's assumed we're updating a module and we'll try to get its slug.
  1196. * If 'any' the full list is returned.
  1197. * If it's an array of parameters, includes the elements by matching keys.
  1198. *
  1199. * @return array
  1200. */
  1201. public static function get_updateable_data_list( $selector = '' ) {
  1202. $options = array(
  1203. // Carousel
  1204. 'carousel_background_color' => array(
  1205. 'description' => esc_html__( 'Color scheme.', 'jetpack' ),
  1206. 'type' => 'string',
  1207. 'default' => 'black',
  1208. 'enum' => array(
  1209. 'black',
  1210. 'white',
  1211. ),
  1212. 'enum_labels' => array(
  1213. 'black' => esc_html__( 'Black', 'jetpack' ),
  1214. 'white' => esc_html__( 'White', 'jetpack' ),
  1215. ),
  1216. 'validate_callback' => __CLASS__ . '::validate_list_item',
  1217. 'jp_group' => 'carousel',
  1218. ),
  1219. 'carousel_display_exif' => array(
  1220. 'description' => wp_kses( sprintf( __( 'Show photo metadata (<a href="http://en.wikipedia.org/wiki/Exchangeable_image_file_format" target="_blank">Exif</a>) in carousel, when available.', 'jetpack' ) ), array( 'a' => array( 'href' => true, 'target' => true ) ) ),
  1221. 'type' => 'boolean',
  1222. 'default' => 0,
  1223. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1224. 'jp_group' => 'carousel',
  1225. ),
  1226. // Comments
  1227. 'highlander_comment_form_prompt' => array(
  1228. 'description' => esc_html__( 'Greeting Text', 'jetpack' ),
  1229. 'type' => 'string',
  1230. 'default' => esc_html__( 'Leave a Reply', 'jetpack' ),
  1231. 'sanitize_callback' => 'sanitize_text_field',
  1232. 'jp_group' => 'comments',
  1233. ),
  1234. 'jetpack_comment_form_color_scheme' => array(
  1235. 'description' => esc_html__( "Color scheme", 'jetpack' ),
  1236. 'type' => 'string',
  1237. 'default' => 'light',
  1238. 'enum' => array(
  1239. 'light',
  1240. 'dark',
  1241. 'transparent',
  1242. ),
  1243. 'enum_labels' => array(
  1244. 'light' => esc_html__( 'Light', 'jetpack' ),
  1245. 'dark' => esc_html__( 'Dark', 'jetpack' ),
  1246. 'transparent' => esc_html__( 'Transparent', 'jetpack' ),
  1247. ),
  1248. 'validate_callback' => __CLASS__ . '::validate_list_item',
  1249. 'jp_group' => 'comments',
  1250. ),
  1251. // Custom Content Types
  1252. 'jetpack_portfolio' => array(
  1253. 'description' => esc_html__( 'Enable or disable Jetpack portfolio post type.', 'jetpack' ),
  1254. 'type' => 'boolean',
  1255. 'default' => 0,
  1256. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1257. 'jp_group' => 'custom-content-types',
  1258. ),
  1259. 'jetpack_portfolio_posts_per_page' => array(
  1260. 'description' => esc_html__( 'Number of entries to show at most in Portfolio pages.', 'jetpack' ),
  1261. 'type' => 'integer',
  1262. 'default' => 10,
  1263. 'validate_callback' => __CLASS__ . '::validate_posint',
  1264. 'jp_group' => 'custom-content-types',
  1265. ),
  1266. 'jetpack_testimonial' => array(
  1267. 'description' => esc_html__( 'Enable or disable Jetpack testimonial post type.', 'jetpack' ),
  1268. 'type' => 'boolean',
  1269. 'default' => 0,
  1270. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1271. 'jp_group' => 'custom-content-types',
  1272. ),
  1273. 'jetpack_testimonial_posts_per_page' => array(
  1274. 'description' => esc_html__( 'Number of entries to show at most in Testimonial pages.', 'jetpack' ),
  1275. 'type' => 'integer',
  1276. 'default' => 10,
  1277. 'validate_callback' => __CLASS__ . '::validate_posint',
  1278. 'jp_group' => 'custom-content-types',
  1279. ),
  1280. // Galleries
  1281. 'tiled_galleries' => array(
  1282. 'description' => esc_html__( 'Display all your gallery pictures in a cool mosaic.', 'jetpack' ),
  1283. 'type' => 'boolean',
  1284. 'default' => 0,
  1285. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1286. 'jp_group' => 'tiled-gallery',
  1287. ),
  1288. 'gravatar_disable_hovercards' => array(
  1289. 'description' => esc_html__( "View people's profiles when you mouse over their Gravatars", 'jetpack' ),
  1290. 'type' => 'string',
  1291. 'default' => 'enabled',
  1292. // Not visible. This is used as the checkbox value.
  1293. 'enum' => array(
  1294. 'enabled',
  1295. 'disabled',
  1296. ),
  1297. 'enum_labels' => array(
  1298. 'enabled' => esc_html__( 'Enabled', 'jetpack' ),
  1299. 'disabled' => esc_html__( 'Disabled', 'jetpack' ),
  1300. ),
  1301. 'validate_callback' => __CLASS__ . '::validate_list_item',
  1302. 'jp_group' => 'gravatar-hovercards',
  1303. ),
  1304. // Infinite Scroll
  1305. 'infinite_scroll' => array(
  1306. 'description' => esc_html__( 'To infinity and beyond', 'jetpack' ),
  1307. 'type' => 'boolean',
  1308. 'default' => 1,
  1309. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1310. 'jp_group' => 'infinite-scroll',
  1311. ),
  1312. 'infinite_scroll_google_analytics' => array(
  1313. 'description' => esc_html__( 'Use Google Analytics with Infinite Scroll', 'jetpack' ),
  1314. 'type' => 'boolean',
  1315. 'default' => 0,
  1316. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1317. 'jp_group' => 'infinite-scroll',
  1318. ),
  1319. // Likes
  1320. 'wpl_default' => array(
  1321. 'description' => esc_html__( 'WordPress.com Likes are', 'jetpack' ),
  1322. 'type' => 'string',
  1323. 'default' => 'on',
  1324. 'enum' => array(
  1325. 'on',
  1326. 'off',
  1327. ),
  1328. 'enum_labels' => array(
  1329. 'on' => esc_html__( 'On for all posts', 'jetpack' ),
  1330. 'off' => esc_html__( 'Turned on per post', 'jetpack' ),
  1331. ),
  1332. 'validate_callback' => __CLASS__ . '::validate_list_item',
  1333. 'jp_group' => 'likes',
  1334. ),
  1335. 'social_notifications_like' => array(
  1336. 'description' => esc_html__( 'Send email notification when someone likes a post', 'jetpack' ),
  1337. 'type' => 'boolean',
  1338. 'default' => 1,
  1339. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1340. 'jp_group' => 'likes',
  1341. ),
  1342. // Markdown
  1343. 'wpcom_publish_comments_with_markdown' => array(
  1344. 'description' => esc_html__( 'Use Markdown for comments.', 'jetpack' ),
  1345. 'type' => 'boolean',
  1346. 'default' => 0,
  1347. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1348. 'jp_group' => 'markdown',
  1349. ),
  1350. 'wpcom_publish_posts_with_markdown' => array(
  1351. 'description' => esc_html__( 'Use Markdown for posts.', 'jetpack' ),
  1352. 'type' => 'boolean',
  1353. 'default' => 0,
  1354. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1355. 'jp_group' => 'markdown',
  1356. ),
  1357. // Mobile Theme
  1358. 'wp_mobile_excerpt' => array(
  1359. 'description' => esc_html__( 'Excerpts', 'jetpack' ),
  1360. 'type' => 'boolean',
  1361. 'default' => 0,
  1362. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1363. 'jp_group' => 'minileven',
  1364. ),
  1365. 'wp_mobile_featured_images' => array(
  1366. 'description' => esc_html__( 'Featured Images', 'jetpack' ),
  1367. 'type' => 'boolean',
  1368. 'default' => 0,
  1369. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1370. 'jp_group' => 'minileven',
  1371. ),
  1372. 'wp_mobile_app_promos' => array(
  1373. 'description' => esc_html__( 'Show a promo for the WordPress mobile apps in the footer of the mobile theme.', 'jetpack' ),
  1374. 'type' => 'boolean',
  1375. 'default' => 0,
  1376. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1377. 'jp_group' => 'minileven',
  1378. ),
  1379. // Monitor
  1380. 'monitor_receive_notifications' => array(
  1381. 'description' => esc_html__( 'Receive Monitor Email Notifications.', 'jetpack' ),
  1382. 'type' => 'boolean',
  1383. 'default' => 0,
  1384. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1385. 'jp_group' => 'monitor',
  1386. ),
  1387. // Post by Email
  1388. 'post_by_email_address' => array(
  1389. 'description' => esc_html__( 'Email Address', 'jetpack' ),
  1390. 'type' => 'string',
  1391. 'default' => 'noop',
  1392. 'enum' => array(
  1393. 'noop',
  1394. 'create',
  1395. 'regenerate',
  1396. 'delete',
  1397. ),
  1398. 'enum_labels' => array(
  1399. 'noop' => '',
  1400. 'create' => esc_html__( 'Create Post by Email address', 'jetpack' ),
  1401. 'regenerate' => esc_html__( 'Regenerate Post by Email address', 'jetpack' ),
  1402. 'delete' => esc_html__( 'Delete Post by Email address', 'jetpack' ),
  1403. ),
  1404. 'validate_callback' => __CLASS__ . '::validate_list_item',
  1405. 'jp_group' => 'post-by-email',
  1406. ),
  1407. // Protect
  1408. 'jetpack_protect_key' => array(
  1409. 'description' => esc_html__( 'Protect API key', 'jetpack' ),
  1410. 'type' => 'string',
  1411. 'default' => '',
  1412. 'validate_callback' => __CLASS__ . '::validate_alphanum',
  1413. 'jp_group' => 'protect',
  1414. ),
  1415. 'jetpack_protect_global_whitelist' => array(
  1416. 'description' => esc_html__( 'Protect global whitelist', 'jetpack' ),
  1417. 'type' => 'string',
  1418. 'default' => '',
  1419. 'validate_callback' => __CLASS__ . '::validate_string',
  1420. 'sanitize_callback' => 'esc_textarea',
  1421. 'jp_group' => 'protect',
  1422. ),
  1423. // Sharing
  1424. 'sharing_services' => array(
  1425. 'description' => esc_html__( 'Enabled Services and those hidden behind a button', 'jetpack' ),
  1426. 'type' => 'object',
  1427. 'default' => array(
  1428. 'visible' => array( 'twitter', 'facebook', 'google-plus-1' ),
  1429. 'hidden' => array(),
  1430. ),
  1431. 'validate_callback' => __CLASS__ . '::validate_services',
  1432. 'jp_group' => 'sharedaddy',
  1433. ),
  1434. 'button_style' => array(
  1435. 'description' => esc_html__( 'Button Style', 'jetpack' ),
  1436. 'type' => 'string',
  1437. 'default' => 'icon',
  1438. 'enum' => array(
  1439. 'icon-text',
  1440. 'icon',
  1441. 'text',
  1442. 'official',
  1443. ),
  1444. 'enum_labels' => array(
  1445. 'icon-text' => esc_html__( 'Icon + text', 'jetpack' ),
  1446. 'icon' => esc_html__( 'Icon only', 'jetpack' ),
  1447. 'text' => esc_html__( 'Text only', 'jetpack' ),
  1448. 'official' => esc_html__( 'Official buttons', 'jetpack' ),
  1449. ),
  1450. 'validate_callback' => __CLASS__ . '::validate_list_item',
  1451. 'jp_group' => 'sharedaddy',
  1452. ),
  1453. 'sharing_label' => array(
  1454. 'description' => esc_html__( 'Sharing Label', 'jetpack' ),
  1455. 'type' => 'string',
  1456. 'default' => '',
  1457. 'validate_callback' => __CLASS__ . '::validate_string',
  1458. 'sanitize_callback' => 'esc_html',
  1459. 'jp_group' => 'sharedaddy',
  1460. ),
  1461. 'show' => array(
  1462. 'description' => esc_html__( 'Views where buttons are shown', 'jetpack' ),
  1463. 'type' => 'array',
  1464. 'items' => array(
  1465. 'type' => 'string'
  1466. ),
  1467. 'default' => array( 'post' ),
  1468. 'validate_callback' => __CLASS__ . '::validate_sharing_show',
  1469. 'jp_group' => 'sharedaddy',
  1470. ),
  1471. 'jetpack-twitter-cards-site-tag' => array(
  1472. 'description' => esc_html__( "The Twitter username of the owner of this site's domain.", 'jetpack' ),
  1473. 'type' => 'string',
  1474. 'default' => '',
  1475. 'validate_callback' => __CLASS__ . '::validate_twitter_username',
  1476. 'sanitize_callback' => 'esc_html',
  1477. 'jp_group' => 'sharedaddy',
  1478. ),
  1479. 'sharedaddy_disable_resources' => array(
  1480. 'description' => esc_html__( 'Disable CSS and JS', 'jetpack' ),
  1481. 'type' => 'boolean',
  1482. 'default' => 0,
  1483. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1484. 'jp_group' => 'sharedaddy',
  1485. ),
  1486. 'custom' => array(
  1487. 'description' => esc_html__( 'Custom sharing services added by user.', 'jetpack' ),
  1488. 'type' => 'object',
  1489. 'default' => array(
  1490. 'sharing_name' => '',
  1491. 'sharing_url' => '',
  1492. 'sharing_icon' => '',
  1493. ),
  1494. 'validate_callback' => __CLASS__ . '::validate_custom_service',
  1495. 'jp_group' => 'sharedaddy',
  1496. ),
  1497. // Not an option, but an action that can be perfomed on the list of custom services passing the service ID.
  1498. 'sharing_delete_service' => array(
  1499. 'description' => esc_html__( 'Delete custom sharing service.', 'jetpack' ),
  1500. 'type' => 'string',
  1501. 'default' => '',
  1502. 'validate_callback' => __CLASS__ . '::validate_custom_service_id',
  1503. 'jp_group' => 'sharedaddy',
  1504. ),
  1505. // SSO
  1506. 'jetpack_sso_require_two_step' => array(
  1507. 'description' => esc_html__( 'Require Two-Step Authentication', 'jetpack' ),
  1508. 'type' => 'boolean',
  1509. 'default' => 0,
  1510. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1511. 'jp_group' => 'sso',
  1512. ),
  1513. 'jetpack_sso_match_by_email' => array(
  1514. 'description' => esc_html__( 'Match by Email', 'jetpack' ),
  1515. 'type' => 'boolean',
  1516. 'default' => 0,
  1517. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1518. 'jp_group' => 'sso',
  1519. ),
  1520. // Subscriptions
  1521. 'stb_enabled' => array(
  1522. 'description' => esc_html__( "Show a <em>'follow blog'</em> option in the comment form", 'jetpack' ),
  1523. 'type' => 'boolean',
  1524. 'default' => 1,
  1525. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1526. 'jp_group' => 'subscriptions',
  1527. ),
  1528. 'stc_enabled' => array(
  1529. 'description' => esc_html__( "Show a <em>'follow comments'</em> option in the comment form", 'jetpack' ),
  1530. 'type' => 'boolean',
  1531. 'default' => 1,
  1532. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1533. 'jp_group' => 'subscriptions',
  1534. ),
  1535. // Related Posts
  1536. 'show_headline' => array(
  1537. 'description' => esc_html__( 'Highlight related content with a heading', 'jetpack' ),
  1538. 'type' => 'boolean',
  1539. 'default' => 1,
  1540. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1541. 'jp_group' => 'related-posts',
  1542. ),
  1543. 'show_thumbnails' => array(
  1544. 'description' => esc_html__( 'Show a thumbnail image where available', 'jetpack' ),
  1545. 'type' => 'boolean',
  1546. 'default' => 0,
  1547. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1548. 'jp_group' => 'related-posts',
  1549. ),
  1550. // Spelling and Grammar - After the Deadline
  1551. 'onpublish' => array(
  1552. 'description' => esc_html__( 'Proofread when a post or page is first published.', 'jetpack' ),
  1553. 'type' => 'boolean',
  1554. 'default' => 0,
  1555. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1556. 'jp_group' => 'after-the-deadline',
  1557. ),
  1558. 'onupdate' => array(
  1559. 'description' => esc_html__( 'Proofread when a post or page is updated.', 'jetpack' ),
  1560. 'type' => 'boolean',
  1561. 'default' => 0,
  1562. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1563. 'jp_group' => 'after-the-deadline',
  1564. ),
  1565. 'Bias Language' => array(
  1566. 'description' => esc_html__( 'Bias Language', 'jetpack' ),
  1567. 'type' => 'boolean',
  1568. 'default' => 0,
  1569. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1570. 'jp_group' => 'after-the-deadline',
  1571. ),
  1572. 'Cliches' => array(
  1573. 'description' => esc_html__( 'Clichés', 'jetpack' ),
  1574. 'type' => 'boolean',
  1575. 'default' => 0,
  1576. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1577. 'jp_group' => 'after-the-deadline',
  1578. ),
  1579. 'Complex Expression' => array(
  1580. 'description' => esc_html__( 'Complex Phrases', 'jetpack' ),
  1581. 'type' => 'boolean',
  1582. 'default' => 0,
  1583. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1584. 'jp_group' => 'after-the-deadline',
  1585. ),
  1586. 'Diacritical Marks' => array(
  1587. 'description' => esc_html__( 'Diacritical Marks', 'jetpack' ),
  1588. 'type' => 'boolean',
  1589. 'default' => 0,
  1590. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1591. 'jp_group' => 'after-the-deadline',
  1592. ),
  1593. 'Double Negative' => array(
  1594. 'description' => esc_html__( 'Double Negatives', 'jetpack' ),
  1595. 'type' => 'boolean',
  1596. 'default' => 0,
  1597. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1598. 'jp_group' => 'after-the-deadline',
  1599. ),
  1600. 'Hidden Verbs' => array(
  1601. 'description' => esc_html__( 'Hidden Verbs', 'jetpack' ),
  1602. 'type' => 'boolean',
  1603. 'default' => 0,
  1604. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1605. 'jp_group' => 'after-the-deadline',
  1606. ),
  1607. 'Jargon Language' => array(
  1608. 'description' => esc_html__( 'Jargon', 'jetpack' ),
  1609. 'type' => 'boolean',
  1610. 'default' => 0,
  1611. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1612. 'jp_group' => 'after-the-deadline',
  1613. ),
  1614. 'Passive voice' => array(
  1615. 'description' => esc_html__( 'Passive Voice', 'jetpack' ),
  1616. 'type' => 'boolean',
  1617. 'default' => 0,
  1618. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1619. 'jp_group' => 'after-the-deadline',
  1620. ),
  1621. 'Phrases to Avoid' => array(
  1622. 'description' => esc_html__( 'Phrases to Avoid', 'jetpack' ),
  1623. 'type' => 'boolean',
  1624. 'default' => 0,
  1625. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1626. 'jp_group' => 'after-the-deadline',
  1627. ),
  1628. 'Redundant Expression' => array(
  1629. 'description' => esc_html__( 'Redundant Phrases', 'jetpack' ),
  1630. 'type' => 'boolean',
  1631. 'default' => 0,
  1632. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1633. 'jp_group' => 'after-the-deadline',
  1634. ),
  1635. 'guess_lang' => array(
  1636. 'description' => esc_html__( 'Use automatically detected language to proofread posts and pages', 'jetpack' ),
  1637. 'type' => 'boolean',
  1638. 'default' => 0,
  1639. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1640. 'jp_group' => 'after-the-deadline',
  1641. ),
  1642. 'ignored_phrases' => array(
  1643. 'description' => esc_html__( 'Add Phrase to be ignored', 'jetpack' ),
  1644. 'type' => 'string',
  1645. 'default' => '',
  1646. 'sanitize_callback' => 'esc_html',
  1647. 'jp_group' => 'after-the-deadline',
  1648. ),
  1649. 'unignore_phrase' => array(
  1650. 'description' => esc_html__( 'Remove Phrase from being ignored', 'jetpack' ),
  1651. 'type' => 'string',
  1652. 'default' => '',
  1653. 'sanitize_callback' => 'esc_html',
  1654. 'jp_group' => 'after-the-deadline',
  1655. ),
  1656. // Verification Tools
  1657. 'google' => array(
  1658. 'description' => esc_html__( 'Google Search Console', 'jetpack' ),
  1659. 'type' => 'string',
  1660. 'default' => '',
  1661. 'validate_callback' => __CLASS__ . '::validate_verification_service',
  1662. 'jp_group' => 'verification-tools',
  1663. ),
  1664. 'bing' => array(
  1665. 'description' => esc_html__( 'Bing Webmaster Center', 'jetpack' ),
  1666. 'type' => 'string',
  1667. 'default' => '',
  1668. 'validate_callback' => __CLASS__ . '::validate_verification_service',
  1669. 'jp_group' => 'verification-tools',
  1670. ),
  1671. 'pinterest' => array(
  1672. 'description' => esc_html__( 'Pinterest Site Verification', 'jetpack' ),
  1673. 'type' => 'string',
  1674. 'default' => '',
  1675. 'validate_callback' => __CLASS__ . '::validate_verification_service',
  1676. 'jp_group' => 'verification-tools',
  1677. ),
  1678. 'yandex' => array(
  1679. 'description' => esc_html__( 'Yandex Site Verification', 'jetpack' ),
  1680. 'type' => 'string',
  1681. 'default' => '',
  1682. 'validate_callback' => __CLASS__ . '::validate_verification_service',
  1683. 'jp_group' => 'verification-tools',
  1684. ),
  1685. 'enable_header_ad' => array(
  1686. 'description' => esc_html__( 'Display an ad unit at the top of each page.', 'jetpack' ),
  1687. 'type' => 'boolean',
  1688. 'default' => 1,
  1689. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1690. 'jp_group' => 'wordads',
  1691. ),
  1692. 'wordads_approved' => array(
  1693. 'description' => esc_html__( 'Is site approved for WordAds?', 'jetpack' ),
  1694. 'type' => 'boolean',
  1695. 'default' => 0,
  1696. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1697. 'jp_group' => 'wordads',
  1698. ),
  1699. 'wordads_second_belowpost' => array(
  1700. 'description' => esc_html__( 'Display second ad below post?', 'jetpack' ),
  1701. 'type' => 'boolean',
  1702. 'default' => 1,
  1703. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1704. 'jp_group' => 'wordads',
  1705. ),
  1706. 'wordads_display_front_page' => array(
  1707. 'description' => esc_html__( 'Display ads on the front page?', 'jetpack' ),
  1708. 'type' => 'boolean',
  1709. 'default' => 1,
  1710. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1711. 'jp_group' => 'wordads',
  1712. ),
  1713. 'wordads_display_post' => array(
  1714. 'description' => esc_html__( 'Display ads on posts?', 'jetpack' ),
  1715. 'type' => 'boolean',
  1716. 'default' => 1,
  1717. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1718. 'jp_group' => 'wordads',
  1719. ),
  1720. 'wordads_display_page' => array(
  1721. 'description' => esc_html__( 'Display ads on pages?', 'jetpack' ),
  1722. 'type' => 'boolean',
  1723. 'default' => 1,
  1724. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1725. 'jp_group' => 'wordads',
  1726. ),
  1727. 'wordads_display_archive' => array(
  1728. 'description' => esc_html__( 'Display ads on archive pages?', 'jetpack' ),
  1729. 'type' => 'boolean',
  1730. 'default' => 1,
  1731. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1732. 'jp_group' => 'wordads',
  1733. ),
  1734. 'wordads_custom_adstxt' => array(
  1735. 'description' => esc_html__( 'Custom ads.txt entries', 'jetpack' ),
  1736. 'type' => 'string',
  1737. 'default' => '',
  1738. 'validate_callback' => __CLASS__ . '::validate_string',
  1739. 'sanitize_callback' => 'sanitize_textarea_field',
  1740. 'jp_group' => 'wordads',
  1741. ),
  1742. // Google Analytics
  1743. 'google_analytics_tracking_id' => array(
  1744. 'description' => esc_html__( 'Google Analytics', 'jetpack' ),
  1745. 'type' => 'string',
  1746. 'default' => '',
  1747. 'validate_callback' => __CLASS__ . '::validate_alphanum',
  1748. 'jp_group' => 'google-analytics',
  1749. ),
  1750. // Stats
  1751. 'admin_bar' => array(
  1752. 'description' => esc_html__( 'Put a chart showing 48 hours of views in the admin bar.', 'jetpack' ),
  1753. 'type' => 'boolean',
  1754. 'default' => 1,
  1755. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1756. 'jp_group' => 'stats',
  1757. ),
  1758. 'roles' => array(
  1759. 'description' => esc_html__( 'Select the roles that will be able to view stats reports.', 'jetpack' ),
  1760. 'type' => 'array',
  1761. 'items' => array(
  1762. 'type' => 'string'
  1763. ),
  1764. 'default' => array( 'administrator' ),
  1765. 'validate_callback' => __CLASS__ . '::validate_stats_roles',
  1766. 'sanitize_callback' => __CLASS__ . '::sanitize_stats_allowed_roles',
  1767. 'jp_group' => 'stats',
  1768. ),
  1769. 'count_roles' => array(
  1770. 'description' => esc_html__( 'Count the page views of registered users who are logged in.', 'jetpack' ),
  1771. 'type' => 'array',
  1772. 'items' => array(
  1773. 'type' => 'string'
  1774. ),
  1775. 'default' => array( 'administrator' ),
  1776. 'validate_callback' => __CLASS__ . '::validate_stats_roles',
  1777. 'jp_group' => 'stats',
  1778. ),
  1779. 'blog_id' => array(
  1780. 'description' => esc_html__( 'Blog ID.', 'jetpack' ),
  1781. 'type' => 'boolean',
  1782. 'default' => 0,
  1783. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1784. 'jp_group' => 'stats',
  1785. ),
  1786. 'do_not_track' => array(
  1787. 'description' => esc_html__( 'Do not track.', 'jetpack' ),
  1788. 'type' => 'boolean',
  1789. 'default' => 1,
  1790. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1791. 'jp_group' => 'stats',
  1792. ),
  1793. 'hide_smile' => array(
  1794. 'description' => esc_html__( 'Hide the stats smiley face image.', 'jetpack' ),
  1795. 'type' => 'boolean',
  1796. 'default' => 1,
  1797. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1798. 'jp_group' => 'stats',
  1799. ),
  1800. 'version' => array(
  1801. 'description' => esc_html__( 'Version.', 'jetpack' ),
  1802. 'type' => 'integer',
  1803. 'default' => 9,
  1804. 'validate_callback' => __CLASS__ . '::validate_posint',
  1805. 'jp_group' => 'stats',
  1806. ),
  1807. // Akismet - Not a module, but a plugin. The options can be passed and handled differently.
  1808. 'akismet_show_user_comments_approved' => array(
  1809. 'description' => '',
  1810. 'type' => 'boolean',
  1811. 'default' => 0,
  1812. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1813. 'jp_group' => 'settings',
  1814. ),
  1815. 'wordpress_api_key' => array(
  1816. 'description' => '',
  1817. 'type' => 'string',
  1818. 'default' => '',
  1819. 'validate_callback' => __CLASS__ . '::validate_alphanum',
  1820. 'jp_group' => 'settings',
  1821. ),
  1822. // Apps card on dashboard
  1823. 'dismiss_dash_app_card' => array(
  1824. 'description' => '',
  1825. 'type' => 'boolean',
  1826. 'default' => 0,
  1827. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1828. 'jp_group' => 'settings',
  1829. ),
  1830. // Empty stats card dismiss
  1831. 'dismiss_empty_stats_card' => array(
  1832. 'description' => '',
  1833. 'type' => 'boolean',
  1834. 'default' => 0,
  1835. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1836. 'jp_group' => 'settings',
  1837. ),
  1838. 'lang_id' => array(
  1839. 'description' => esc_html__( 'Primary language for the site.', 'jetpack' ),
  1840. 'type' => 'string',
  1841. 'default' => 'en_US',
  1842. 'jp_group' => 'settings',
  1843. ),
  1844. 'onboarding' => array(
  1845. 'description' => '',
  1846. 'type' => 'object',
  1847. 'default' => array(
  1848. 'siteTitle' => '',
  1849. 'siteDescription' => '',
  1850. 'siteType' => 'personal',
  1851. 'homepageFormat' => 'posts',
  1852. 'addContactForm' => 0,
  1853. 'businessAddress' => array(
  1854. 'name' => '',
  1855. 'street' => '',
  1856. 'city' => '',
  1857. 'state' => '',
  1858. 'zip' => '',
  1859. ),
  1860. 'installWooCommerce' => false,
  1861. ),
  1862. 'validate_callback' => __CLASS__ . '::validate_onboarding',
  1863. 'jp_group' => 'settings',
  1864. ),
  1865. // Show welcome for newly purchased plan
  1866. 'show_welcome_for_new_plan' => array(
  1867. 'description' => '',
  1868. 'type' => 'boolean',
  1869. 'default' => 0,
  1870. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1871. 'jp_group' => 'settings',
  1872. ),
  1873. );
  1874. // Add modules to list so they can be toggled
  1875. $modules = Jetpack::get_available_modules();
  1876. if ( is_array( $modules ) && ! empty( $modules ) ) {
  1877. $module_args = array(
  1878. 'description' => '',
  1879. 'type' => 'boolean',
  1880. 'default' => 0,
  1881. 'validate_callback' => __CLASS__ . '::validate_boolean',
  1882. 'jp_group' => 'modules',
  1883. );
  1884. foreach( $modules as $module ) {
  1885. $options[ $module ] = $module_args;
  1886. }
  1887. }
  1888. if ( is_array( $selector ) ) {
  1889. // Return only those options whose keys match $selector keys
  1890. return array_intersect_key( $options, $selector );
  1891. }
  1892. if ( 'any' === $selector ) {
  1893. // Toggle module or update any module option or any general setting
  1894. return $options;
  1895. }
  1896. // We're updating the options for a single module.
  1897. if ( empty( $selector ) ) {
  1898. $selector = self::get_module_requested();
  1899. }
  1900. $selected = array();
  1901. foreach ( $options as $option => $attributes ) {
  1902. // Not adding an isset( $attributes['jp_group'] ) because if it's not set, it must be fixed, otherwise options will fail.
  1903. if ( $selector === $attributes['jp_group'] ) {
  1904. $selected[ $option ] = $attributes;
  1905. }
  1906. }
  1907. return $selected;
  1908. }
  1909. /**
  1910. * Validates that the parameters are proper values that can be set during Jetpack onboarding.
  1911. *
  1912. * @since 5.4.0
  1913. *
  1914. * @param array $onboarding_data Values to check.
  1915. * @param WP_REST_Request $request The request sent to the WP REST API.
  1916. * @param string $param Name of the parameter passed to endpoint holding $value.
  1917. *
  1918. * @return bool|WP_Error
  1919. */
  1920. public static function validate_onboarding( $onboarding_data, $request, $param ) {
  1921. if ( ! is_array( $onboarding_data ) ) {
  1922. return new WP_Error( 'invalid_param', esc_html__( 'Not valid onboarding data.', 'jetpack' ) );
  1923. }
  1924. foreach ( $onboarding_data as $value ) {
  1925. if ( is_string( $value ) ) {
  1926. $onboarding_choice = self::validate_string( $value, $request, $param );
  1927. } elseif ( is_array( $value ) ) {
  1928. $onboarding_choice = self::validate_onboarding( $value, $request, $param );
  1929. } else {
  1930. $onboarding_choice = self::validate_boolean( $value, $request, $param );
  1931. }
  1932. if ( is_wp_error( $onboarding_choice ) ) {
  1933. return $onboarding_choice;
  1934. }
  1935. }
  1936. return true;
  1937. }
  1938. /**
  1939. * Validates that the parameter is either a pure boolean or a numeric string that can be mapped to a boolean.
  1940. *
  1941. * @since 4.3.0
  1942. *
  1943. * @param string|bool $value Value to check.
  1944. * @param WP_REST_Request $request The request sent to the WP REST API.
  1945. * @param string $param Name of the parameter passed to endpoint holding $value.
  1946. *
  1947. * @return bool|WP_Error
  1948. */
  1949. public static function validate_boolean( $value, $request, $param ) {
  1950. if ( ! is_bool( $value ) && ! ( ( ctype_digit( $value ) || is_numeric( $value ) ) && in_array( $value, array( 0, 1 ) ) ) ) {
  1951. return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be true, false, 0 or 1.', 'jetpack' ), $param ) );
  1952. }
  1953. return true;
  1954. }
  1955. /**
  1956. * Validates that the parameter is a positive integer.
  1957. *
  1958. * @since 4.3.0
  1959. *
  1960. * @param int $value Value to check.
  1961. * @param WP_REST_Request $request The request sent to the WP REST API.
  1962. * @param string $param Name of the parameter passed to endpoint holding $value.
  1963. *
  1964. * @return bool|WP_Error
  1965. */
  1966. public static function validate_posint( $value = 0, $request, $param ) {
  1967. if ( ! is_numeric( $value ) || $value <= 0 ) {
  1968. return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be a positive integer.', 'jetpack' ), $param ) );
  1969. }
  1970. return true;
  1971. }
  1972. /**
  1973. * Validates that the parameter belongs to a list of admitted values.
  1974. *
  1975. * @since 4.3.0
  1976. *
  1977. * @param string $value Value to check.
  1978. * @param WP_REST_Request $request The request sent to the WP REST API.
  1979. * @param string $param Name of the parameter passed to endpoint holding $value.
  1980. *
  1981. * @return bool|WP_Error
  1982. */
  1983. public static function validate_list_item( $value = '', $request, $param ) {
  1984. $attributes = $request->get_attributes();
  1985. if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) {
  1986. return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s not recognized', 'jetpack' ), $param ) );
  1987. }
  1988. $args = $attributes['args'][ $param ];
  1989. if ( ! empty( $args['enum'] ) ) {
  1990. // If it's an associative array, use the keys to check that the value is among those admitted.
  1991. $enum = ( count( array_filter( array_keys( $args['enum'] ), 'is_string' ) ) > 0 ) ? array_keys( $args['enum'] ) : $args['enum'];
  1992. if ( ! in_array( $value, $enum ) ) {
  1993. return new WP_Error( 'invalid_param_value', sprintf(
  1994. /* Translators: first variable is the parameter passed to endpoint that holds the list item, the second is a list of admitted values. */
  1995. esc_html__( '%1$s must be one of %2$s', 'jetpack' ), $param, implode( ', ', $enum )
  1996. ) );
  1997. }
  1998. }
  1999. return true;
  2000. }
  2001. /**
  2002. * Validates that the parameter belongs to a list of admitted values.
  2003. *
  2004. * @since 4.3.0
  2005. *
  2006. * @param string $value Value to check.
  2007. * @param WP_REST_Request $request The request sent to the WP REST API.
  2008. * @param string $param Name of the parameter passed to endpoint holding $value.
  2009. *
  2010. * @return bool|WP_Error
  2011. */
  2012. public static function validate_module_list( $value = '', $request, $param ) {
  2013. if ( ! is_array( $value ) ) {
  2014. return new WP_Error( 'invalid_param_value', sprintf( esc_html__( '%s must be an array', 'jetpack' ), $param ) );
  2015. }
  2016. $modules = Jetpack::get_available_modules();
  2017. if ( count( array_intersect( $value, $modules ) ) != count( $value ) ) {
  2018. return new WP_Error( 'invalid_param_value', sprintf( esc_html__( '%s must be a list of valid modules', 'jetpack' ), $param ) );
  2019. }
  2020. return true;
  2021. }
  2022. /**
  2023. * Validates that the parameter is an alphanumeric or empty string (to be able to clear the field).
  2024. *
  2025. * @since 4.3.0
  2026. *
  2027. * @param string $value Value to check.
  2028. * @param WP_REST_Request $request The request sent to the WP REST API.
  2029. * @param string $param Name of the parameter passed to endpoint holding $value.
  2030. *
  2031. * @return bool|WP_Error
  2032. */
  2033. public static function validate_alphanum( $value = '', $request, $param ) {
  2034. if ( ! empty( $value ) && ( ! is_string( $value ) || ! preg_match( '/^[a-z0-9]+$/i', $value ) ) ) {
  2035. return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be an alphanumeric string.', 'jetpack' ), $param ) );
  2036. }
  2037. return true;
  2038. }
  2039. /**
  2040. * Validates that the parameter is a tag or id for a verification service, or an empty string (to be able to clear the field).
  2041. *
  2042. * @since 4.6.0
  2043. *
  2044. * @param string $value Value to check.
  2045. * @param WP_REST_Request $request
  2046. * @param string $param Name of the parameter passed to endpoint holding $value.
  2047. *
  2048. * @return bool|WP_Error
  2049. */
  2050. public static function validate_verification_service( $value = '', $request, $param ) {
  2051. if ( ! empty( $value ) && ! ( is_string( $value ) && ( preg_match( '/^[a-z0-9_-]+$/i', $value ) || jetpack_verification_get_code( $value ) !== false ) ) ) {
  2052. return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be an alphanumeric string or a verification tag.', 'jetpack' ), $param ) );
  2053. }
  2054. return true;
  2055. }
  2056. /**
  2057. * Validates that the parameter is among the roles allowed for Stats.
  2058. *
  2059. * @since 4.3.0
  2060. *
  2061. * @param string|bool $value Value to check.
  2062. * @param WP_REST_Request $request The request sent to the WP REST API.
  2063. * @param string $param Name of the parameter passed to endpoint holding $value.
  2064. *
  2065. * @return bool|WP_Error
  2066. */
  2067. public static function validate_stats_roles( $value, $request, $param ) {
  2068. if ( ! empty( $value ) && ! array_intersect( self::$stats_roles, $value ) ) {
  2069. return new WP_Error( 'invalid_param', sprintf(
  2070. /* Translators: first variable is the name of a parameter passed to endpoint holding the role that will be checked, the second is a list of roles allowed to see stats. The parameter is checked against this list. */
  2071. esc_html__( '%1$s must be %2$s.', 'jetpack' ), $param, join( ', ', self::$stats_roles )
  2072. ) );
  2073. }
  2074. return true;
  2075. }
  2076. /**
  2077. * Validates that the parameter is among the views where the Sharing can be displayed.
  2078. *
  2079. * @since 4.3.0
  2080. *
  2081. * @param string|bool $value Value to check.
  2082. * @param WP_REST_Request $request The request sent to the WP REST API.
  2083. * @param string $param Name of the parameter passed to endpoint holding $value.
  2084. *
  2085. * @return bool|WP_Error
  2086. */
  2087. public static function validate_sharing_show( $value, $request, $param ) {
  2088. $views = array( 'index', 'post', 'page', 'attachment', 'jetpack-portfolio' );
  2089. if ( ! is_array( $value ) ) {
  2090. return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be an array of post types.', 'jetpack' ), $param ) );
  2091. }
  2092. if ( ! array_intersect( $views, $value ) ) {
  2093. return new WP_Error( 'invalid_param', sprintf(
  2094. /* Translators: first variable is the name of a parameter passed to endpoint holding the post type where Sharing will be displayed, the second is a list of post types where Sharing can be displayed */
  2095. esc_html__( '%1$s must be %2$s.', 'jetpack' ), $param, join( ', ', $views )
  2096. ) );
  2097. }
  2098. return true;
  2099. }
  2100. /**
  2101. * Validates that the parameter is among the views where the Sharing can be displayed.
  2102. *
  2103. * @since 4.3.0
  2104. *
  2105. * @param string|bool $value {
  2106. * Value to check received by request.
  2107. *
  2108. * @type array $visible List of slug of services to share to that are displayed directly in the page.
  2109. * @type array $hidden List of slug of services to share to that are concealed in a folding menu.
  2110. * }
  2111. * @param WP_REST_Request $request The request sent to the WP REST API.
  2112. * @param string $param Name of the parameter passed to endpoint holding $value.
  2113. *
  2114. * @return bool|WP_Error
  2115. */
  2116. public static function validate_services( $value, $request, $param ) {
  2117. if ( ! is_array( $value ) || ! isset( $value['visible'] ) || ! isset( $value['hidden'] ) ) {
  2118. return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be an array with visible and hidden items.', 'jetpack' ), $param ) );
  2119. }
  2120. // Allow to clear everything.
  2121. if ( empty( $value['visible'] ) && empty( $value['hidden'] ) ) {
  2122. return true;
  2123. }
  2124. if ( ! class_exists( 'Sharing_Service' ) && ! include_once( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
  2125. return new WP_Error( 'invalid_param', esc_html__( 'Failed loading required dependency Sharing_Service.', 'jetpack' ) );
  2126. }
  2127. $sharer = new Sharing_Service();
  2128. $services = array_keys( $sharer->get_all_services() );
  2129. if (
  2130. ( ! empty( $value['visible'] ) && ! array_intersect( $value['visible'], $services ) )
  2131. ||
  2132. ( ! empty( $value['hidden'] ) && ! array_intersect( $value['hidden'], $services ) ) )
  2133. {
  2134. return new WP_Error( 'invalid_param', sprintf(
  2135. /* Translators: placeholder 1 is a parameter holding the services passed to endpoint, placeholder 2 is a list of all Jetpack Sharing services */
  2136. esc_html__( '%1$s visible and hidden items must be a list of %2$s.', 'jetpack' ), $param, join( ', ', $services )
  2137. ) );
  2138. }
  2139. return true;
  2140. }
  2141. /**
  2142. * Validates that the parameter has enough information to build a custom sharing button.
  2143. *
  2144. * @since 4.3.0
  2145. *
  2146. * @param string|bool $value Value to check.
  2147. * @param WP_REST_Request $request The request sent to the WP REST API.
  2148. * @param string $param Name of the parameter passed to endpoint holding $value.
  2149. *
  2150. * @return bool|WP_Error
  2151. */
  2152. public static function validate_custom_service( $value, $request, $param ) {
  2153. if ( ! is_array( $value ) || ! isset( $value['sharing_name'] ) || ! isset( $value['sharing_url'] ) || ! isset( $value['sharing_icon'] ) ) {
  2154. return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be an array with sharing name, url and icon.', 'jetpack' ), $param ) );
  2155. }
  2156. // Allow to clear everything.
  2157. if ( empty( $value['sharing_name'] ) && empty( $value['sharing_url'] ) && empty( $value['sharing_icon'] ) ) {
  2158. return true;
  2159. }
  2160. if ( ! class_exists( 'Sharing_Service' ) && ! include_once( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
  2161. return new WP_Error( 'invalid_param', esc_html__( 'Failed loading required dependency Sharing_Service.', 'jetpack' ) );
  2162. }
  2163. if ( ( ! empty( $value['sharing_name'] ) && ! is_string( $value['sharing_name'] ) )
  2164. || ( ! empty( $value['sharing_url'] ) && ! is_string( $value['sharing_url'] ) )
  2165. || ( ! empty( $value['sharing_icon'] ) && ! is_string( $value['sharing_icon'] ) ) ) {
  2166. return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s needs sharing name, url and icon.', 'jetpack' ), $param ) );
  2167. }
  2168. return true;
  2169. }
  2170. /**
  2171. * Validates that the parameter is a custom sharing service ID like 'custom-1461976264'.
  2172. *
  2173. * @since 4.3.0
  2174. *
  2175. * @param string $value Value to check.
  2176. * @param WP_REST_Request $request The request sent to the WP REST API.
  2177. * @param string $param Name of the parameter passed to endpoint holding $value.
  2178. *
  2179. * @return bool|WP_Error
  2180. */
  2181. public static function validate_custom_service_id( $value = '', $request, $param ) {
  2182. if ( ! empty( $value ) && ( ! is_string( $value ) || ! preg_match( '/custom\-[0-1]+/i', $value ) ) ) {
  2183. return new WP_Error( 'invalid_param', sprintf( esc_html__( "%s must be a string prefixed with 'custom-' and followed by a numeric ID.", 'jetpack' ), $param ) );
  2184. }
  2185. if ( ! class_exists( 'Sharing_Service' ) && ! include_once( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
  2186. return new WP_Error( 'invalid_param', esc_html__( 'Failed loading required dependency Sharing_Service.', 'jetpack' ) );
  2187. }
  2188. $sharer = new Sharing_Service();
  2189. $services = array_keys( $sharer->get_all_services() );
  2190. if ( ! empty( $value ) && ! in_array( $value, $services ) ) {
  2191. return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s is not a registered custom sharing service.', 'jetpack' ), $param ) );
  2192. }
  2193. return true;
  2194. }
  2195. /**
  2196. * Validates that the parameter is a Twitter username or empty string (to be able to clear the field).
  2197. *
  2198. * @since 4.3.0
  2199. *
  2200. * @param string $value Value to check.
  2201. * @param WP_REST_Request $request
  2202. * @param string $param Name of the parameter passed to endpoint holding $value.
  2203. *
  2204. * @return bool|WP_Error
  2205. */
  2206. public static function validate_twitter_username( $value = '', $request, $param ) {
  2207. if ( ! empty( $value ) && ( ! is_string( $value ) || ! preg_match( '/^@?\w{1,15}$/i', $value ) ) ) {
  2208. return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be a Twitter username.', 'jetpack' ), $param ) );
  2209. }
  2210. return true;
  2211. }
  2212. /**
  2213. * Validates that the parameter is a string.
  2214. *
  2215. * @since 4.3.0
  2216. *
  2217. * @param string $value Value to check.
  2218. * @param WP_REST_Request $request The request sent to the WP REST API.
  2219. * @param string $param Name of the parameter passed to endpoint holding $value.
  2220. *
  2221. * @return bool|WP_Error
  2222. */
  2223. public static function validate_string( $value = '', $request, $param ) {
  2224. if ( ! is_string( $value ) ) {
  2225. return new WP_Error( 'invalid_param', sprintf( esc_html__( '%s must be a string.', 'jetpack' ), $param ) );
  2226. }
  2227. return true;
  2228. }
  2229. /**
  2230. * If for some reason the roles allowed to see Stats are empty (for example, user tampering with checkboxes),
  2231. * return an array with only 'administrator' as the allowed role and save it for 'roles' option.
  2232. *
  2233. * @since 4.3.0
  2234. *
  2235. * @param string|bool $value Value to check.
  2236. *
  2237. * @return bool|array
  2238. */
  2239. public static function sanitize_stats_allowed_roles( $value ) {
  2240. if ( empty( $value ) ) {
  2241. return array( 'administrator' );
  2242. }
  2243. return $value;
  2244. }
  2245. /**
  2246. * Get the currently accessed route and return the module slug in it.
  2247. *
  2248. * @since 4.3.0
  2249. *
  2250. * @param string $route Regular expression for the endpoint with the module slug to return.
  2251. *
  2252. * @return array|string
  2253. */
  2254. public static function get_module_requested( $route = '/module/(?P<slug>[a-z\-]+)' ) {
  2255. if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
  2256. return '';
  2257. }
  2258. preg_match( "#$route#", $GLOBALS['wp']->query_vars['rest_route'], $module );
  2259. if ( empty( $module['slug'] ) ) {
  2260. return '';
  2261. }
  2262. return $module['slug'];
  2263. }
  2264. /**
  2265. * Adds extra information for modules.
  2266. *
  2267. * @since 4.3.0
  2268. *
  2269. * @param string|array $modules Can be a single module or a list of modules.
  2270. * @param null|string $slug Slug of the module in the first parameter.
  2271. *
  2272. * @return array|string
  2273. */
  2274. public static function prepare_modules_for_response( $modules = '', $slug = null ) {
  2275. global $wp_rewrite;
  2276. /** This filter is documented in modules/sitemaps/sitemaps.php */
  2277. $location = apply_filters( 'jetpack_sitemap_location', '' );
  2278. if ( $wp_rewrite->using_index_permalinks() ) {
  2279. $sitemap_url = home_url( '/index.php' . $location . '/sitemap.xml' );
  2280. $news_sitemap_url = home_url( '/index.php' . $location . '/news-sitemap.xml' );
  2281. } else if ( $wp_rewrite->using_permalinks() ) {
  2282. $sitemap_url = home_url( $location . '/sitemap.xml' );
  2283. $news_sitemap_url = home_url( $location . '/news-sitemap.xml' );
  2284. } else {
  2285. $sitemap_url = home_url( $location . '/?jetpack-sitemap=sitemap.xml' );
  2286. $news_sitemap_url = home_url( $location . '/?jetpack-sitemap=news-sitemap.xml' );
  2287. }
  2288. if ( is_null( $slug ) && isset( $modules['sitemaps'] ) ) {
  2289. // Is a list of modules
  2290. $modules['sitemaps']['extra']['sitemap_url'] = $sitemap_url;
  2291. $modules['sitemaps']['extra']['news_sitemap_url'] = $news_sitemap_url;
  2292. } elseif ( 'sitemaps' == $slug ) {
  2293. // It's a single module
  2294. $modules['extra']['sitemap_url'] = $sitemap_url;
  2295. $modules['extra']['news_sitemap_url'] = $news_sitemap_url;
  2296. }
  2297. return $modules;
  2298. }
  2299. /**
  2300. * Remove 'validate_callback' item from options available for module.
  2301. * Fetch current option value and add to array of module options.
  2302. * Prepare values of module options that need special handling, like those saved in wpcom.
  2303. *
  2304. * @since 4.3.0
  2305. *
  2306. * @param string $module Module slug.
  2307. * @return array
  2308. */
  2309. public static function prepare_options_for_response( $module = '' ) {
  2310. $options = self::get_updateable_data_list( $module );
  2311. if ( ! is_array( $options ) || empty( $options ) ) {
  2312. return $options;
  2313. }
  2314. // Some modules need special treatment.
  2315. switch ( $module ) {
  2316. case 'monitor':
  2317. // Status of user notifications
  2318. $options['monitor_receive_notifications']['current_value'] = self::cast_value( self::get_remote_value( 'monitor', 'monitor_receive_notifications' ), $options['monitor_receive_notifications'] );
  2319. break;
  2320. case 'post-by-email':
  2321. // Email address
  2322. $options['post_by_email_address']['current_value'] = self::cast_value( self::get_remote_value( 'post-by-email', 'post_by_email_address' ), $options['post_by_email_address'] );
  2323. break;
  2324. case 'protect':
  2325. // Protect
  2326. $options['jetpack_protect_key']['current_value'] = get_site_option( 'jetpack_protect_key', false );
  2327. if ( ! function_exists( 'jetpack_protect_format_whitelist' ) ) {
  2328. include_once( JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php' );
  2329. }
  2330. $options['jetpack_protect_global_whitelist']['current_value'] = jetpack_protect_format_whitelist();
  2331. break;
  2332. case 'related-posts':
  2333. // It's local, but it must be broken apart since it's saved as an array.
  2334. $options = self::split_options( $options, Jetpack_Options::get_option( 'relatedposts' ) );
  2335. break;
  2336. case 'verification-tools':
  2337. // It's local, but it must be broken apart since it's saved as an array.
  2338. $options = self::split_options( $options, get_option( 'verification_services_codes' ) );
  2339. break;
  2340. case 'google-analytics':
  2341. $wga = get_option( 'jetpack_wga' );
  2342. $code = '';
  2343. if ( is_array( $wga ) && array_key_exists( 'code', $wga ) ) {
  2344. $code = $wga[ 'code' ];
  2345. }
  2346. $options[ 'google_analytics_tracking_id' ][ 'current_value' ] = $code;
  2347. break;
  2348. case 'sharedaddy':
  2349. // It's local, but it must be broken apart since it's saved as an array.
  2350. if ( ! class_exists( 'Sharing_Service' ) && ! include_once( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
  2351. break;
  2352. }
  2353. $sharer = new Sharing_Service();
  2354. $options = self::split_options( $options, $sharer->get_global_options() );
  2355. $options['sharing_services']['current_value'] = $sharer->get_blog_services();
  2356. $other_sharedaddy_options = array( 'jetpack-twitter-cards-site-tag', 'sharedaddy_disable_resources', 'sharing_delete_service' );
  2357. foreach ( $other_sharedaddy_options as $key ) {
  2358. $default_value = isset( $options[ $key ]['default'] ) ? $options[ $key ]['default'] : '';
  2359. $current_value = get_option( $key, $default_value );
  2360. $options[ $key ]['current_value'] = self::cast_value( $current_value, $options[ $key ] );
  2361. }
  2362. break;
  2363. case 'after-the-deadline':
  2364. if ( ! function_exists( 'AtD_get_options' ) ) {
  2365. include_once( JETPACK__PLUGIN_DIR . 'modules/after-the-deadline.php' );
  2366. }
  2367. $atd_options = array_merge( AtD_get_options( get_current_user_id(), 'AtD_options' ), AtD_get_options( get_current_user_id(), 'AtD_check_when' ) );
  2368. unset( $atd_options['name'] );
  2369. foreach ( $atd_options as $key => $value ) {
  2370. $options[ $key ]['current_value'] = self::cast_value( $value, $options[ $key ] );
  2371. }
  2372. $atd_options = AtD_get_options( get_current_user_id(), 'AtD_guess_lang' );
  2373. $options['guess_lang']['current_value'] = self::cast_value( isset( $atd_options['true'] ), $options[ 'guess_lang' ] );
  2374. $options['ignored_phrases']['current_value'] = AtD_get_setting( get_current_user_id(), 'AtD_ignored_phrases' );
  2375. unset( $options['unignore_phrase'] );
  2376. break;
  2377. case 'stats':
  2378. // It's local, but it must be broken apart since it's saved as an array.
  2379. if ( ! function_exists( 'stats_get_options' ) ) {
  2380. include_once( JETPACK__PLUGIN_DIR . 'modules/stats.php' );
  2381. }
  2382. $options = self::split_options( $options, stats_get_options() );
  2383. break;
  2384. default:
  2385. // These option are just stored as plain WordPress options.
  2386. foreach ( $options as $key => $value ) {
  2387. $default_value = isset( $options[ $key ]['default'] ) ? $options[ $key ]['default'] : '';
  2388. $current_value = get_option( $key, $default_value );
  2389. $options[ $key ]['current_value'] = self::cast_value( $current_value, $options[ $key ] );
  2390. }
  2391. }
  2392. // At this point some options have current_value not set because they're options
  2393. // that only get written on update, so we set current_value to the default one.
  2394. foreach ( $options as $key => $value ) {
  2395. // We don't need validate_callback in the response
  2396. if ( isset( $options[ $key ]['validate_callback'] ) ) {
  2397. unset( $options[ $key ]['validate_callback'] );
  2398. }
  2399. $default_value = isset( $options[ $key ]['default'] ) ? $options[ $key ]['default'] : '';
  2400. if ( ! array_key_exists( 'current_value', $options[ $key ] ) ) {
  2401. $options[ $key ]['current_value'] = self::cast_value( $default_value, $options[ $key ] );
  2402. }
  2403. }
  2404. return $options;
  2405. }
  2406. /**
  2407. * Splits module options saved as arrays like relatedposts or verification_services_codes into separate options to be returned in the response.
  2408. *
  2409. * @since 4.3.0
  2410. *
  2411. * @param array $separate_options Array of options admitted by the module.
  2412. * @param array $grouped_options Option saved as array to be splitted.
  2413. * @param string $prefix Optional prefix for the separate option keys.
  2414. *
  2415. * @return array
  2416. */
  2417. public static function split_options( $separate_options, $grouped_options, $prefix = '' ) {
  2418. if ( is_array( $grouped_options ) ) {
  2419. foreach ( $grouped_options as $key => $value ) {
  2420. $option_key = $prefix . $key;
  2421. if ( isset( $separate_options[ $option_key ] ) ) {
  2422. $separate_options[ $option_key ]['current_value'] = self::cast_value( $grouped_options[ $key ], $separate_options[ $option_key ] );
  2423. }
  2424. }
  2425. }
  2426. return $separate_options;
  2427. }
  2428. /**
  2429. * Perform a casting to the value specified in the option definition.
  2430. *
  2431. * @since 4.3.0
  2432. *
  2433. * @param mixed $value Value to cast to the proper type.
  2434. * @param array $definition Type to cast the value to.
  2435. *
  2436. * @return bool|float|int|string
  2437. */
  2438. public static function cast_value( $value, $definition ) {
  2439. if ( $value === 'NULL' ) {
  2440. return null;
  2441. }
  2442. if ( isset( $definition['type'] ) ) {
  2443. switch ( $definition['type'] ) {
  2444. case 'boolean':
  2445. if ( 'true' === $value ) {
  2446. return true;
  2447. } elseif ( 'false' === $value ) {
  2448. return false;
  2449. }
  2450. return (bool) $value;
  2451. break;
  2452. case 'integer':
  2453. return (int) $value;
  2454. break;
  2455. case 'float':
  2456. return (float) $value;
  2457. break;
  2458. case 'string':
  2459. return (string) $value;
  2460. break;
  2461. }
  2462. }
  2463. return $value;
  2464. }
  2465. /**
  2466. * Get a value not saved locally.
  2467. *
  2468. * @since 4.3.0
  2469. *
  2470. * @param string $module Module slug.
  2471. * @param string $option Option name.
  2472. *
  2473. * @return bool Whether user is receiving notifications or not.
  2474. */
  2475. public static function get_remote_value( $module, $option ) {
  2476. if ( in_array( $module, array( 'post-by-email' ), true ) ) {
  2477. $option .= get_current_user_id();
  2478. }
  2479. // If option doesn't exist, 'does_not_exist' will be returned.
  2480. $value = get_option( $option, 'does_not_exist' );
  2481. // If option exists, just return it.
  2482. if ( 'does_not_exist' !== $value ) {
  2483. return $value;
  2484. }
  2485. // Only check a remote option if Jetpack is connected.
  2486. if ( ! Jetpack::is_active() ) {
  2487. return false;
  2488. }
  2489. // Do what is necessary for each module.
  2490. switch ( $module ) {
  2491. case 'monitor':
  2492. // Load the class to use the method. If class can't be found, do nothing.
  2493. if ( ! class_exists( 'Jetpack_Monitor' ) && ! include_once( Jetpack::get_module_path( $module ) ) ) {
  2494. return false;
  2495. }
  2496. $value = Jetpack_Monitor::user_receives_notifications( false );
  2497. break;
  2498. case 'post-by-email':
  2499. // Load the class to use the method. If class can't be found, do nothing.
  2500. if ( ! class_exists( 'Jetpack_Post_By_Email' ) && ! include_once( Jetpack::get_module_path( $module ) ) ) {
  2501. return false;
  2502. }
  2503. $post_by_email = new Jetpack_Post_By_Email();
  2504. $value = $post_by_email->get_post_by_email_address();
  2505. if ( $value === null ) {
  2506. $value = 'NULL'; // sentinel value so it actually gets set
  2507. }
  2508. break;
  2509. }
  2510. // Normalize value to boolean.
  2511. if ( is_wp_error( $value ) || is_null( $value ) ) {
  2512. $value = false;
  2513. }
  2514. // Save option to use it next time.
  2515. update_option( $option, $value );
  2516. return $value;
  2517. }
  2518. /**
  2519. * Get number of plugin updates available.
  2520. *
  2521. * @since 4.3.0
  2522. *
  2523. * @return mixed|WP_Error Number of plugin updates available. Otherwise, a WP_Error instance with the corresponding error.
  2524. */
  2525. public static function get_plugin_update_count() {
  2526. $updates = wp_get_update_data();
  2527. if ( isset( $updates['counts'] ) && isset( $updates['counts']['plugins'] ) ) {
  2528. $count = $updates['counts']['plugins'];
  2529. if ( 0 == $count ) {
  2530. $response = array(
  2531. 'code' => 'success',
  2532. 'message' => esc_html__( 'All plugins are up-to-date. Keep up the good work!', 'jetpack' ),
  2533. 'count' => 0,
  2534. );
  2535. } else {
  2536. $response = array(
  2537. 'code' => 'updates-available',
  2538. 'message' => esc_html( sprintf( _n( '%s plugin need updating.', '%s plugins need updating.', $count, 'jetpack' ), $count ) ),
  2539. 'count' => $count,
  2540. );
  2541. }
  2542. return rest_ensure_response( $response );
  2543. }
  2544. return new WP_Error( 'not_found', esc_html__( 'Could not check updates for plugins on this site.', 'jetpack' ), array( 'status' => 404 ) );
  2545. }
  2546. /**
  2547. * Returns a list of all plugins in the site.
  2548. *
  2549. * @since 4.2.0
  2550. * @uses get_plugins()
  2551. *
  2552. * @return array
  2553. */
  2554. private static function core_get_plugins() {
  2555. if ( ! function_exists( 'get_plugins' ) ) {
  2556. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  2557. }
  2558. /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
  2559. $plugins = apply_filters( 'all_plugins', get_plugins() );
  2560. if ( is_array( $plugins ) && ! empty( $plugins ) ) {
  2561. foreach ( $plugins as $plugin_slug => $plugin_data ) {
  2562. $plugins[ $plugin_slug ]['active'] = self::core_is_plugin_active( $plugin_slug );
  2563. }
  2564. return $plugins;
  2565. }
  2566. return array();
  2567. }
  2568. /**
  2569. * Checks if the queried plugin is active.
  2570. *
  2571. * @since 4.2.0
  2572. * @uses is_plugin_active()
  2573. *
  2574. * @return bool
  2575. */
  2576. private static function core_is_plugin_active( $plugin ) {
  2577. if ( ! function_exists( 'is_plugin_active' ) ) {
  2578. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  2579. }
  2580. return is_plugin_active( $plugin );
  2581. }
  2582. /**
  2583. * Get plugins data in site.
  2584. *
  2585. * @since 4.2.0
  2586. *
  2587. * @return WP_REST_Response|WP_Error List of plugins in the site. Otherwise, a WP_Error instance with the corresponding error.
  2588. */
  2589. public static function get_plugins() {
  2590. $plugins = self::core_get_plugins();
  2591. if ( ! empty( $plugins ) ) {
  2592. return rest_ensure_response( $plugins );
  2593. }
  2594. return new WP_Error( 'not_found', esc_html__( 'Unable to list plugins.', 'jetpack' ), array( 'status' => 404 ) );
  2595. }
  2596. /**
  2597. * Get data about the queried plugin. Currently it only returns whether the plugin is active or not.
  2598. *
  2599. * @since 4.2.0
  2600. *
  2601. * @param WP_REST_Request $request {
  2602. * Array of parameters received by request.
  2603. *
  2604. * @type string $slug Plugin slug with the syntax 'plugin-directory/plugin-main-file.php'.
  2605. * }
  2606. *
  2607. * @return bool|WP_Error True if module was activated. Otherwise, a WP_Error instance with the corresponding error.
  2608. */
  2609. public static function get_plugin( $request ) {
  2610. $plugins = self::core_get_plugins();
  2611. if ( empty( $plugins ) ) {
  2612. return new WP_Error( 'no_plugins_found', esc_html__( 'This site has no plugins.', 'jetpack' ), array( 'status' => 404 ) );
  2613. }
  2614. $plugin = stripslashes( $request['plugin'] );
  2615. if ( ! in_array( $plugin, array_keys( $plugins ) ) ) {
  2616. return new WP_Error( 'plugin_not_found', esc_html( sprintf( __( 'Plugin %s is not installed.', 'jetpack' ), $plugin ) ), array( 'status' => 404 ) );
  2617. }
  2618. $plugin_data = $plugins[ $plugin ];
  2619. $plugin_data['active'] = self::core_is_plugin_active( $plugin );
  2620. return rest_ensure_response( array(
  2621. 'code' => 'success',
  2622. 'message' => esc_html__( 'Plugin found.', 'jetpack' ),
  2623. 'data' => $plugin_data
  2624. ) );
  2625. }
  2626. } // class end