class.jetpack-sync-module-callables.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. require_once dirname( __FILE__ ) . '/class.jetpack-sync-functions.php';
  3. class Jetpack_Sync_Module_Callables extends Jetpack_Sync_Module {
  4. const CALLABLES_CHECKSUM_OPTION_NAME = 'jetpack_callables_sync_checksum';
  5. const CALLABLES_AWAIT_TRANSIENT_NAME = 'jetpack_sync_callables_await';
  6. private $callable_whitelist;
  7. public function name() {
  8. return 'functions';
  9. }
  10. public function set_defaults() {
  11. if ( is_multisite() ) {
  12. $this->callable_whitelist = array_merge( Jetpack_Sync_Defaults::get_callable_whitelist(), Jetpack_Sync_Defaults::get_multisite_callable_whitelist() );
  13. } else {
  14. $this->callable_whitelist = Jetpack_Sync_Defaults::get_callable_whitelist();
  15. }
  16. }
  17. public function init_listeners( $callable ) {
  18. add_action( 'jetpack_sync_callable', $callable, 10, 2 );
  19. add_action( 'admin_init', array( $this, 'set_plugin_action_links' ), 9999 ); // Should happen very late
  20. // For some options, we should always send the change right away!
  21. $always_send_updates_to_these_options = array(
  22. 'jetpack_active_modules',
  23. 'home',
  24. 'siteurl',
  25. 'jetpack_sync_error_idc',
  26. );
  27. foreach( $always_send_updates_to_these_options as $option ) {
  28. add_action( "update_option_{$option}", array( $this, 'unlock_sync_callable' ) );
  29. }
  30. // Provide a hook so that hosts can send changes to certain callables right away.
  31. // Especially useful when a host uses constants to change home and siteurl.
  32. add_action( 'jetpack_sync_unlock_sync_callable', array( $this, 'unlock_sync_callable' ) );
  33. // get_plugins and wp_version
  34. // gets fired when new code gets installed, updates etc.
  35. add_action( 'upgrader_process_complete', array( $this, 'unlock_plugin_action_link_and_callables' ) );
  36. add_action( 'update_option_active_plugins', array( $this, 'unlock_plugin_action_link_and_callables' ) );
  37. }
  38. public function init_full_sync_listeners( $callable ) {
  39. add_action( 'jetpack_full_sync_callables', $callable );
  40. }
  41. public function init_before_send() {
  42. add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_callables' ) );
  43. // full sync
  44. add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) );
  45. }
  46. public function reset_data() {
  47. delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
  48. delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
  49. $url_callables = array( 'home_url', 'site_url', 'main_network_site_url' );
  50. foreach( $url_callables as $callable ) {
  51. delete_option( Jetpack_Sync_Functions::HTTPS_CHECK_OPTION_PREFIX . $callable );
  52. }
  53. }
  54. function set_callable_whitelist( $callables ) {
  55. $this->callable_whitelist = $callables;
  56. }
  57. function get_callable_whitelist() {
  58. return $this->callable_whitelist;
  59. }
  60. public function get_all_callables() {
  61. // get_all_callables should run as the master user always.
  62. $current_user_id = get_current_user_id();
  63. wp_set_current_user( Jetpack_Options::get_option( 'master_user' ) );
  64. $callables = array_combine(
  65. array_keys( $this->get_callable_whitelist() ),
  66. array_map( array( $this, 'get_callable' ), array_values( $this->get_callable_whitelist() ) )
  67. );
  68. wp_set_current_user( $current_user_id );
  69. return $callables;
  70. }
  71. private function get_callable( $callable ) {
  72. return call_user_func( $callable );
  73. }
  74. public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
  75. /**
  76. * Tells the client to sync all callables to the server
  77. *
  78. * @since 4.2.0
  79. *
  80. * @param boolean Whether to expand callables (should always be true)
  81. */
  82. do_action( 'jetpack_full_sync_callables', true );
  83. // The number of actions enqueued, and next module state (true == done)
  84. return array( 1, true );
  85. }
  86. public function estimate_full_sync_actions( $config ) {
  87. return 1;
  88. }
  89. public function get_full_sync_actions() {
  90. return array( 'jetpack_full_sync_callables' );
  91. }
  92. public function unlock_sync_callable() {
  93. delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
  94. }
  95. public function unlock_plugin_action_link_and_callables() {
  96. delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
  97. delete_transient( 'jetpack_plugin_api_action_links_refresh' );
  98. add_filter( 'jetpack_check_and_send_callables', '__return_true' );
  99. }
  100. public function set_plugin_action_links() {
  101. if (
  102. ! class_exists( 'DOMDocument' ) ||
  103. ! function_exists ( 'libxml_use_internal_errors' ) ||
  104. ! function_exists ( 'mb_convert_encoding' )
  105. ) {
  106. return;
  107. }
  108. $plugins_action_links = array();
  109. // Is the transient lock in place?
  110. $plugins_lock = get_transient( 'jetpack_plugin_api_action_links_refresh', false );
  111. if ( ! empty( $plugins_lock ) ) {
  112. return;
  113. }
  114. $plugins = array_keys( Jetpack_Sync_Functions::get_plugins() );
  115. foreach ( $plugins as $plugin_file ) {
  116. /**
  117. * Plugins often like to unset things but things break if they are not able to.
  118. */
  119. $action_links = array(
  120. 'deactivate' => '',
  121. 'activate' => '',
  122. 'details' => '',
  123. 'delete' => '',
  124. 'edit' => ''
  125. );
  126. /** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */
  127. $action_links = apply_filters( 'plugin_action_links', $action_links, $plugin_file, null, 'all' );
  128. /** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */
  129. $action_links = apply_filters( "plugin_action_links_{$plugin_file}", $action_links, $plugin_file, null, 'all' );
  130. $action_links = array_filter( $action_links );
  131. $formatted_action_links = null;
  132. if ( ! empty( $action_links ) && count( $action_links ) > 0 ) {
  133. $dom_doc = new DOMDocument;
  134. foreach ( $action_links as $action_link ) {
  135. // The @ is not enough to suppress errors when dealing with libxml,
  136. // we have to tell it directly how we want to handle errors.
  137. libxml_use_internal_errors( true );
  138. $dom_doc->loadHTML( mb_convert_encoding( $action_link, 'HTML-ENTITIES', 'UTF-8' ) );
  139. libxml_use_internal_errors( false );
  140. $link_elements = $dom_doc->getElementsByTagName( 'a' );
  141. if ( $link_elements->length == 0 ) {
  142. continue;
  143. }
  144. $link_element = $link_elements->item( 0 );
  145. if ( $link_element->hasAttribute( 'href' ) && $link_element->nodeValue ) {
  146. $link_url = trim( $link_element->getAttribute( 'href' ) );
  147. // Add the full admin path to the url if the plugin did not provide it
  148. $link_url_scheme = wp_parse_url( $link_url, PHP_URL_SCHEME );
  149. if ( empty( $link_url_scheme ) ) {
  150. $link_url = admin_url( $link_url );
  151. }
  152. $formatted_action_links[ $link_element->nodeValue ] = $link_url;
  153. }
  154. }
  155. }
  156. if ( $formatted_action_links ) {
  157. $plugins_action_links[ $plugin_file ] = $formatted_action_links;
  158. }
  159. }
  160. // Cache things for a long time
  161. set_transient( 'jetpack_plugin_api_action_links_refresh', time(), DAY_IN_SECONDS );
  162. update_option( 'jetpack_plugin_api_action_links', $plugins_action_links );
  163. }
  164. public function should_send_callable( $callable_checksums, $name, $checksum ) {
  165. $idc_override_callables = array(
  166. 'main_network_site',
  167. 'home_url',
  168. 'site_url',
  169. );
  170. if ( in_array( $name, $idc_override_callables ) && Jetpack_Options::get_option( 'migrate_for_idc' ) ) {
  171. return true;
  172. }
  173. return ! $this->still_valid_checksum( $callable_checksums, $name, $checksum );
  174. }
  175. public function maybe_sync_callables() {
  176. if ( ! apply_filters( 'jetpack_check_and_send_callables', false ) ) {
  177. if ( ! is_admin() || Jetpack_Sync_Settings::is_doing_cron() ) {
  178. return;
  179. }
  180. if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) {
  181. return;
  182. }
  183. }
  184. set_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_callables_wait_time );
  185. $callables = $this->get_all_callables();
  186. if ( empty( $callables ) ) {
  187. return;
  188. }
  189. $callable_checksums = (array) Jetpack_Options::get_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() );
  190. // only send the callables that have changed
  191. foreach ( $callables as $name => $value ) {
  192. $checksum = $this->get_check_sum( $value );
  193. // explicitly not using Identical comparison as get_option returns a string
  194. if ( ! is_null( $value ) && $this->should_send_callable( $callable_checksums, $name, $checksum ) ) {
  195. /**
  196. * Tells the client to sync a callable (aka function) to the server
  197. *
  198. * @since 4.2.0
  199. *
  200. * @param string The name of the callable
  201. * @param mixed The value of the callable
  202. */
  203. do_action( 'jetpack_sync_callable', $name, $value );
  204. $callable_checksums[ $name ] = $checksum;
  205. } else {
  206. $callable_checksums[ $name ] = $checksum;
  207. }
  208. }
  209. Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums );
  210. }
  211. public function expand_callables( $args ) {
  212. if ( $args[0] ) {
  213. $callables = $this->get_all_callables();
  214. $callables_checksums = array();
  215. foreach ( $callables as $name => $value ) {
  216. $callables_checksums[ $name ] = $this->get_check_sum( $value );
  217. }
  218. Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callables_checksums );
  219. return $callables;
  220. }
  221. return $args;
  222. }
  223. }