ns that will be enqueued. * * @access public * * @param array $config Full sync configuration for this sync module. * @return array Number of items yet to be enqueued. */ public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable return 1; } /** * Retrieve the actions that will be sent for this module during a full sync. * * @access public * * @return array Full sync actions of this module. */ public function get_full_sync_actions() { return array( 'jetpack_full_sync_callables' ); } /** * Unlock callables so they would be available for syncing again. * * @access public */ public function unlock_sync_callable() { delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); } /** * Unlock callables on the next tick. * Sometime the true callable values are only present on the next tick. * When switching themes for example. * * @access public */ public function unlock_sync_callable_next_tick() { $this->force_send_callables_on_next_tick = true; } /** * Unlock callables and plugin action links. * * @access public */ public function unlock_plugin_action_link_and_callables() { delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); delete_transient( 'jetpack_plugin_api_action_links_refresh' ); add_filter( 'jetpack_check_and_send_callables', '__return_true' ); } /** * Parse and store the plugin action links if on the plugins page. * * @uses \DOMDocument * @uses libxml_use_internal_errors * @uses mb_convert_encoding * * @access public */ public function set_plugin_action_links() { if ( ! class_exists( '\DOMDocument' ) || ! function_exists( 'libxml_use_internal_errors' ) || ! function_exists( 'mb_convert_encoding' ) ) { return; } $current_screeen = get_current_screen(); $plugins_action_links = array(); // Is the transient lock in place? $plugins_lock = get_transient( 'jetpack_plugin_api_action_links_refresh', false ); if ( ! empty( $plugins_lock ) && ( isset( $current_screeen->id ) && 'plugins' !== $current_screeen->id ) ) { return; } $plugins = array_keys( Functions::get_plugins() ); foreach ( $plugins as $plugin_file ) { /** * Plugins often like to unset things but things break if they are not able to. */ $action_links = array( 'deactivate' => '', 'activate' => '', 'details' => '', 'delete' => '', 'edit' => '', ); /** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */ $action_links = apply_filters( 'plugin_action_links', $action_links, $plugin_file, null, 'all' ); /** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */ $action_links = apply_filters( "plugin_action_links_{$plugin_file}", $action_links, $plugin_file, null, 'all' ); // Verify $action_links is still an array to resolve warnings from filters not returning an array. if ( is_array( $action_links ) ) { $action_links = array_filter( $action_links ); } else { $action_links = array(); } $formatted_action_links = null; if ( ! empty( $action_links ) && count( $action_links ) > 0 ) { $dom_doc = new \DOMDocument(); foreach ( $action_links as $action_link ) { // The @ is not enough to suppress errors when dealing with libxml, // we have to tell it directly how we want to handle errors. libxml_use_internal_errors( true ); $dom_doc->loadHTML( mb_convert_encoding( $action_link, 'HTML-ENTITIES', 'UTF-8' ) ); libxml_use_internal_errors( false ); $link_elements = $dom_doc->getElementsByTagName( 'a' ); if ( 0 === $link_elements->length ) { continue; } $link_element = $link_elements->item( 0 ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase if ( $link_element->hasAttribute( 'href' ) && $link_element->nodeValue ) { $link_url = trim( $link_element->getAttribute( 'href' ) ); // Add the full admin path to the url if the plugin did not provide it. $link_url_scheme = wp_parse_url( $link_url, PHP_URL_SCHEME ); if ( empty( $link_url_scheme ) ) { $link_url = admin_url( $link_url ); } // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase $formatted_action_links[ $link_element->nodeValue ] = $link_url; } } } if ( $formatted_action_links ) { $plugins_action_links[ $plugin_file ] = $formatted_action_links; } } // Cache things for a long time. set_transient( 'jetpack_plugin_api_action_links_refresh', time(), DAY_IN_SECONDS ); update_option( 'jetpack_plugin_api_action_links', $plugins_action_links ); } /** * Whether a certain callable should be sent. * * @access public * * @param array $callable_checksums Callable checksums. * @param string $name Name of the callable. * @param string $checksum A checksum of the callable. * @return boolean Whether to send the callable. */ public function should_send_callable( $callable_checksums, $name, $checksum ) { $idc_override_callables = array( 'main_network_site', 'home_url', 'site_url', ); if ( in_array( $name, $idc_override_callables, true ) && \Jetpack_Options::get_option( 'migrate_for_idc' ) ) { return true; } return ! $this->still_valid_checksum( $callable_checksums, $name, $checksum ); } /** * Sync the callables if we're supposed to. * * @access public */ public function maybe_sync_callables() { $callables = $this->get_all_callables(); if ( ! apply_filters( 'jetpack_check_and_send_callables', false ) ) { if ( ! is_admin() ) { // If we're not an admin and we're not doing cron and this isn't WP_CLI, don't sync anything. if ( ! Settings::is_doing_cron() && ! Jetpack_Constants::get_constant( 'WP_CLI' ) ) { return; } // If we're not an admin and we are doing cron, sync the Callables that are always supposed to sync ( See https://github.com/Automattic/jetpack/issues/12924 ). $callables = $this->get_always_sent_callables(); } if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) { if ( $this->force_send_callables_on_next_tick ) { $this->unlock_sync_callable(); } return; } } if ( empty( $callables ) ) { return; } // No need to set the transiant we are trying to remove it anyways. if ( ! $this->force_send_callables_on_next_tick ) { set_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME, microtime( true ), Defaults::$default_sync_callables_wait_time ); } $callable_checksums = (array) \Jetpack_Options::get_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() ); $has_changed = false; // Only send the callables that have changed. foreach ( $callables as $name => $value ) { $checksum = $this->get_check_sum( $value ); // Explicitly not using Identical comparison as get_option returns a string. if ( ! is_null( $value ) && $this->should_send_callable( $callable_checksums, $name, $checksum ) ) { // Only send callable if the non sorted checksum also does not match. if ( $this->should_send_callable( $callable_checksums, $name, $this->get_check_sum( $value, false ) ) ) { /** * Tells the client to sync a callable (aka function) to the server * * @param string The name of the callable * @param mixed The value of the callable * * @since 4.2.0 */ do_action( 'jetpack_sync_callable', $name, $value ); } $callable_checksums[ $name ] = $checksum; $has_changed = true; } else { $callable_checksums[ $name ] = $checksum; } } if ( $has_changed ) { \Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums ); } if ( $this->force_send_callables_on_next_tick ) { $this->unlock_sync_callable(); } } /** * Get the callables that should always be sent, e.g. on cron. * * @return array Callables that should always be sent */ protected function get_always_sent_callables() { $callables = $this->get_all_callables(); $cron_callables = array(); foreach ( self::ALWAYS_SEND_UPDATES_TO_THESE_OPTIONS as $option_name ) { if ( array_key_exists( $option_name, $callables ) ) { $cron_callables[ $option_name ] = $callables[ $option_name ]; continue; } // Check for the Callable name/key for the option, if different from option name. if ( array_key_exists( $option_name, self::OPTION_NAMES_TO_CALLABLE_NAMES ) ) { $callable_name = self::OPTION_NAMES_TO_CALLABLE_NAMES[ $option_name ]; if ( array_key_exists( $callable_name, $callables ) ) { $cron_callables[ $callable_name ] = $callables[ $callable_name ]; } } } return $cron_callables; } /** * Expand the callables within a hook before they are serialized and sent to the server. * * @access public * * @param array $args The hook parameters. * @return array $args The hook parameters. */ public function expand_callables( $args ) { if ( $args[0] ) { $callables = $this->get_all_callables(); $callables_checksums = array(); foreach ( $callables as $name => $value ) { $callables_checksums[ $name ] = $this->get_check_sum( $value ); } \Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callables_checksums ); return $callables; } return $args; } /** * Return Total number of objects. * * @param array $config Full Sync config. * * @return int total */ public function total( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable return count( $this->get_callable_whitelist() ); } }