'; foreach ( $menu as $index => $menu_item ) { // Only add the badge markup if not already present and the menu item is the WooPayments menu item. if ( false === strpos( $menu_item[0], $badge ) && ( 'wc-admin&path=/wc-pay-welcome-page' === $menu_item[2] || 'admin.php?page=wc-admin&path=/wc-pay-welcome-page' === $menu_item[2] ) ) { $menu[ $index ][0] .= $badge; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited // One menu item with a badge is more than enough. break; } } } /** * Adds shared settings for the WooPayments incentive. * * @param array $settings Shared settings. * @return array */ public function shared_settings( $settings ): array { // Return early if not on a wc-admin powered page. if ( ! PageController::is_admin_page() ) { return $settings; } // Return early if the incentive must not be visible. if ( ! $this->must_be_visible() ) { return $settings; } $settings['wcpayWelcomePageIncentive'] = $this->get_incentive(); return $settings; } /** * Adds allowed promo notes from the WooPayments incentive. * * @param array $promo_notes Allowed promo notes. * @return array */ public function allowed_promo_notes( $promo_notes = [] ): array { // Return early if the incentive must not be visible. if ( ! $this->must_be_visible() ) { return $promo_notes; } // Add our incentive ID to the promo notes. $promo_notes[] = $this->get_incentive()['id']; return $promo_notes; } /** * Adds the WooPayments incentive badge to the onboarding task. * * @param string $badge Current badge. * * @return string */ public function onboarding_task_badge( string $badge ): string { // Return early if the incentive must not be visible. if ( ! $this->must_be_visible() ) { return $badge; } return $this->get_incentive()['task_badge'] ?? $badge; } /** * Check if the WooPayments payment gateway is active and set up or was at some point, * or there are orders processed with it, at some moment. * * @return boolean */ private function has_wcpay(): bool { // First, get the stored value, if it exists. // This way we avoid costly DB queries and API calls. // Basically, we only want to know if WooPayments was in use in the past. // Since the past can't be changed, neither can this value. $had_wcpay = get_option( self::HAD_WCPAY_OPTION_NAME ); if ( false !== $had_wcpay ) { return $had_wcpay === 'yes'; } // We need to determine the value. // Start with the assumption that the store didn't have WooPayments in use. $had_wcpay = false; // We consider the store to have WooPayments if there is meaningful account data in the WooPayments account cache. // This implies that WooPayments was active at some point and that it was connected. // If WooPayments is active right now, we will not get to this point since the plugin is active check is done first. if ( $this->has_wcpay_account_data() ) { $had_wcpay = true; } // If there is at least one order processed with WooPayments, we consider the store to have WooPayments. if ( false === $had_wcpay && ! empty( wc_get_orders( [ 'payment_method' => 'woocommerce_payments', 'return' => 'ids', 'limit' => 1, ] ) ) ) { $had_wcpay = true; } // Store the value for future use. update_option( self::HAD_WCPAY_OPTION_NAME, $had_wcpay ? 'yes' : 'no' ); return $had_wcpay; } /** * Check if the WooPayments plugin is active. * * @return boolean */ private function is_wcpay_active(): bool { return class_exists( '\WC_Payments' ); } /** * Check if there is meaningful data in the WooPayments account cache. * * @return boolean */ private function has_wcpay_account_data(): bool { $account_data = get_option( 'wcpay_account_data', [] ); if ( ! empty( $account_data['data']['account_id'] ) ) { return true; } return false; } /** * Check if the store has any paid orders. * * Currently, we look at the past 90 days and only consider orders * with status `wc-completed`, `wc-processing`, or `wc-refunded`. * * @return boolean Whether the store has any paid orders. */ private function has_orders(): bool { // First, get the stored value, if it exists. // This way we avoid costly DB queries and API calls. $has_orders = get_transient( self::HAS_ORDERS_TRANSIENT_NAME ); if ( false !== $has_orders ) { return 'yes' === $has_orders; } // We need to determine the value. // Start with the assumption that the store doesn't have orders in the timeframe we look at. $has_orders = false; // By default, we will check for new orders every 6 hours. $expiration = 6 * HOUR_IN_SECONDS; // Get the latest completed, processing, or refunded order. $latest_order = wc_get_orders( array( 'status' => array( 'wc-completed', 'wc-processing', 'wc-refunded' ), 'limit' => 1, 'orderby' => 'date', 'order' => 'DESC', ) ); if ( ! empty( $latest_order ) ) { $latest_order = reset( $latest_order ); // If the latest order is within the timeframe we look at, we consider the store to have orders. // Otherwise, it clearly doesn't have orders. if ( $latest_order instanceof WC_Abstract_Order && strtotime( $latest_order->get_date_created() ) >= strtotime( '-90 days' ) ) { $has_orders = true; // For ultimate efficiency, we will check again after 90 days from the latest order // because in all that time we will consider the store to have orders regardless of new orders. $expiration = strtotime( $latest_order->get_date_created() ) + 90 * DAY_IN_SECONDS - time(); } } // Store the value for future use. set_transient( self::HAS_ORDERS_TRANSIENT_NAME, $has_orders ? 'yes' : 'no', $expiration ); return $has_orders; } /** * Check if the current incentive has been manually dismissed. * * @return boolean */ private function is_incentive_dismissed(): bool { $dismissed_incentives = get_option( 'wcpay_welcome_page_incentives_dismissed', [] ); // If there are no dismissed incentives, return early. if ( empty( $dismissed_incentives ) ) { return false; } // Return early if there is no eligible incentive. $incentive = $this->get_incentive(); if ( empty( $incentive ) ) { return true; } // Search the incentive ID in the dismissed incentives list. if ( in_array( $incentive['id'], $dismissed_incentives, true ) ) { return true; } return false; } /** * Fetches and caches eligible incentive from the WooPayments API. * * @return array|null Array of eligible incentive or null. */ private function get_incentive(): ?array { // Return in-memory cached incentive if it is set. if ( isset( $this->incentive ) ) { return $this->incentive; } // Get the cached data. $cache = get_transient( self::CACHE_TRANSIENT_NAME ); // If the cached data is not expired and it's a WP_Error, // it means there was an API error previously and we should not retry just yet. if ( is_wp_error( $cache ) ) { // Initialize the in-memory cache and return it. $this->incentive = []; return $this->incentive; } // Gather the store context data. $store_context = [ // Store ISO-2 country code, e.g. `US`. 'country' => WC()->countries->get_base_country(), // Store locale, e.g. `en_US`. 'locale' => get_locale(), // WooCommerce store active for duration in seconds. 'active_for' => WCAdminHelper::get_wcadmin_active_for_in_seconds(), 'has_orders' => $this->has_orders(), // Whether the store has at least one payment gateway enabled. 'has_payments' => ! empty( WC()->payment_gateways()->get_available_payment_gateways() ), 'has_wcpay' => $this->has_wcpay(), ]; // Fingerprint the store context through a hash of certain entries. $store_context_hash = $this->generate_context_hash( $store_context ); // Use the transient cached incentive if it exists, it is not expired, // and the store context hasn't changed since we last requested from the WooPayments API (based on context hash). if ( false !== $cache && ! empty( $cache['context_hash'] ) && is_string( $cache['context_hash'] ) && hash_equals( $store_context_hash, $cache['context_hash'] ) ) { // We have a store context hash and it matches with the current context one. // We can use the cached incentive data. // Store the incentive in the in-memory cache and return it. $this->incentive = $cache['incentive'] ?? []; return $this->incentive; } // By this point, we have an expired transient or the store context has changed. // Query for incentives by calling the WooPayments API. $url = add_query_arg( $store_context, 'https://public-api.wordpress.com/wpcom/v2/wcpay/incentives', ); $response = wp_remote_get( $url, [ 'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ), ] ); // Return early if there is an error, waiting 6 hours before the next attempt. if ( is_wp_error( $response ) ) { // Store a trimmed down, lightweight error. $error = new \WP_Error( $response->get_error_code(), $response->get_error_message(), wp_remote_retrieve_response_code( $response ) ); // Store the error in the transient so we know this is due to an API error. set_transient( self::CACHE_TRANSIENT_NAME, $error, HOUR_IN_SECONDS * 6 ); // Initialize the in-memory cache and return it. $this->incentive = []; return $this->incentive; } $cache_for = wp_remote_retrieve_header( $response, 'cache-for' ); // Initialize the in-memory cache. $this->incentive = []; if ( 200 === wp_remote_retrieve_response_code( $response ) ) { // Decode the results, falling back to an empty array. $results = json_decode( wp_remote_retrieve_body( $response ), true ) ?? []; // Find all `welcome_page` incentives. $incentives = array_filter( $results, function( $incentive ) { return 'welcome_page' === $incentive['type']; } ); // Use the first found matching incentive or empty array if none was found. // Store incentive in the in-memory cache. $this->incentive = empty( $incentives ) ? [] : reset( $incentives ); } // Skip transient cache if `cache-for` header equals zero. if ( '0' === $cache_for ) { // If we have a transient cache that is not expired, delete it so there are no leftovers. if ( false !== $cache ) { delete_transient( self::CACHE_TRANSIENT_NAME ); } return $this->incentive; } // Store incentive in transient cache (together with the context hash) for the given number of seconds // or 1 day in seconds. Also attach a timestamp to the transient data so we know when we last fetched. set_transient( self::CACHE_TRANSIENT_NAME, [ 'incentive' => $this->incentive, 'context_hash' => $store_context_hash, 'timestamp' => time(), ], ! empty( $cache_for ) ? (int) $cache_for : DAY_IN_SECONDS ); return $this->incentive; } /** * Generate a hash from the store context data. * * @param array $context The store context data. * * @return string The context hash. */ private function generate_context_hash( array $context ): string { // Include only certain entries in the context hash. // We need only discrete, user-interaction dependent data. // Entries like `active_for` have no place in the hash generation since they change automatically. return md5( wp_json_encode( [ 'country' => $context['country'] ?? '', 'locale' => $context['locale'] ?? '', 'has_orders' => $context['has_orders'] ?? false, 'has_payments' => $context['has_payments'] ?? false, 'has_wcpay' => $context['has_wcpay'] ?? false, ] ) ); } }
Warning: Class "Automattic\WooCommerce\Internal\Admin\WcPayWelcomePage" not found in /htdocs/wp-content/plugins/woocommerce/src/Admin/Features/Features.php on line 363
'expand_wp_insert_comment', ) ); } } // Listen for meta changes. $this->init_listeners_for_meta_type( 'comment', $callable ); $this->init_meta_whitelist_handler( 'comment', array( $this, 'filter_meta' ) ); } /** * Handler for any comment content updates. * * @access public * * @param array $new_comment The new, processed comment data. * @param array $old_comment The old, unslashed comment data. * @param array $new_comment_with_slashes The new, raw comment data. * @return array The new, processed comment data. */ public function handle_comment_contents_modification( $new_comment, $old_comment, $new_comment_with_slashes ) { $changes = array(); $content_fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', ); foreach ( $content_fields as $field ) { if ( $new_comment_with_slashes[ $field ] !== $old_comment[ $field ] ) { $changes[ $field ] = array( $new_comment[ $field ], $old_comment[ $field ] ); } } if ( ! empty( $changes ) ) { /** * Signals to the sync listener that this comment's contents were modified and a sync action * reflecting the change(s) to the content should be sent * * @since 1.6.3 * @since-jetpack 4.9.0 * * @param int $new_comment['comment_ID'] ID of comment whose content was modified * @param mixed $changes Array of changed comment fields with before and after values */ do_action( 'jetpack_modified_comment_contents', $new_comment['comment_ID'], $changes ); } return $new_comment; } /** * Initialize comments action listeners for full sync. * * @access public * * @param callable $callable Action handler callable. */ public function init_full_sync_listeners( $callable ) { add_action( 'jetpack_full_sync_comments', $callable ); // Also send comments meta. } /** * Gets a filtered list of comment types that sync can hook into. * * @access public * * @return array Defaults to [ '', 'trackback', 'pingback' ]. */ public function get_whitelisted_comment_types() { /** * Comment types present in this list will sync their status changes to WordPress.com. * * @since 1.6.3 * @since-jetpack 7.6.0 * * @param array A list of comment types. */ return apply_filters( 'jetpack_sync_whitelisted_comment_types', array( '', 'comment', 'trackback', 'pingback', 'review' ) ); } /** * Prevents any comment types that are not in the whitelist from being enqueued and sent to WordPress.com. * * @param array $args Arguments passed to wp_insert_comment, deleted_comment, spammed_comment, etc. * * @return bool or array $args Arguments passed to wp_insert_comment, deleted_comment, spammed_comment, etc. */ public function only_allow_white_listed_comment_types( $args ) { $comment = false; if ( isset( $args[1] ) ) { // comment object is available. $comment = $args[1]; } elseif ( is_numeric( $args[0] ) ) { // comment_id is available. $comment = get_comment( $args[0] ); } if ( isset( $comment->comment_type ) && ! in_array( $comment->comment_type, $this->get_whitelisted_comment_types(), true ) ) { return false; } return $args; } /** * Filter all blacklisted post types. * * @param array $args Hook arguments. * @return array|false Hook arguments, or false if the post type is a blacklisted one. */ public function filter_blacklisted_post_types( $args ) { $post_id = $args[0]; $posts_module = Modules::get_module( 'posts' ); '@phan-var Posts $posts_module'; if ( false !== $posts_module && ! $posts_module->is_post_type_allowed( $post_id ) ) { return false; } return $args; } /** * Prevents any comment types that are not in the whitelist from being enqueued and sent to WordPress.com. * * @param array $args Arguments passed to wp_{old_status}_to_{new_status}. * * @return bool or array $args Arguments passed to wp_{old_status}_to_{new_status} */ public function only_allow_white_listed_comment_type_transitions( $args ) { $comment = $args[0]; if ( ! in_array( $comment->comment_type, $this->get_whitelisted_comment_types(), true ) ) { return false; } return $args; } /** * Prevents any comment types that are not in the whitelist from being enqueued and sent to WordPress.com. * Also expands comment data before being enqueued. * * @param array $args Arguments passed to wp_insert_comment. * * @return false or array $args Arguments passed to wp_insert_comment or false if the comment type is a blacklisted one. */ public function filter_jetpack_sync_before_enqueue_wp_insert_comment( $args ) { if ( false === $this->only_allow_white_listed_comment_types( $args ) ) { return false; } return $this->expand_wp_insert_comment( $args ); } /** * Whether a comment type is allowed. * A comment type is allowed if it's present in the comment type whitelist. * * @param int $comment_id ID of the comment. * @return boolean Whether the comment type is allowed. */ public function is_comment_type_allowed( $comment_id ) { $comment = get_comment( $comment_id ); if ( isset( $comment->comment_type ) ) { return in_array( $comment->comment_type, $this->get_whitelisted_comment_types(), true ); } return false; } /** * Initialize the module in the sender. * * @access public */ public function init_before_send() { // Full sync. add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) ); } /** * Enqueue the comments actions for full sync. * * @access public * * @param array $config Full sync configuration for this sync module. * @param int $max_items_to_enqueue Maximum number of items to enqueue. * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. * @return array Number of actions enqueued, and next module state. */ public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { global $wpdb; return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_comments', $wpdb->comments, 'comment_ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); } /** * Retrieve an estimated number of actions that will be enqueued. * * @access public * * @param array $config Full sync configuration for this sync module. * @return int Number of items yet to be enqueued. */ public function estimate_full_sync_actions( $config ) { global $wpdb; $query = "SELECT count(*) FROM $wpdb->comments"; $where_sql = $this->get_where_sql( $config ); if ( $where_sql ) { $query .= ' WHERE ' . $where_sql; } // TODO: Call $wpdb->prepare on the following query. // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching $count = (int) $wpdb->get_var( $query ); return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); } /** * Retrieve the WHERE SQL clause based on the module config. * * @access public * * @param array $config Full sync configuration for this sync module. * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. */ public function get_where_sql( $config ) { if ( is_array( $config ) ) { return 'comment_ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; } return '1=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_comments' ); } /** * Count all the actions that are going to be sent. * * @access public * * @param array $action_names Names of all the actions that will be sent. * @return int Number of actions. */ public function count_full_sync_actions( $action_names ) { return $this->count_actions( $action_names, array( 'jetpack_full_sync_comments' ) ); } /** * Expand the comment status change before the data is serialized and sent to the server. * * @access public * @todo This is not used currently - let's implement it. * * @param array $args The hook parameters. * @return array The expanded hook parameters. */ public function expand_wp_comment_status_change( $args ) { return array( $args[0], $this->filter_comment( $args[1] ) ); } /** * Expand the comment creation before the data is added to the Sync queue. * * @access public * * @param array $args The hook parameters. * @return array The expanded hook parameters. */ public function expand_wp_insert_comment( $args ) { return array( $args[0], $this->filter_comment( $args[1] ) ); } /** * Filter a comment object to the fields we need. * * @access public * * @param \WP_Comment $comment The unfiltered comment object. * @return \WP_Comment Filtered comment object. */ public function filter_comment( $comment ) { /** * Filters whether to prevent sending comment data to .com * * Passing true to the filter will prevent the comment data from being sent * to the WordPress.com. * Instead we pass data that will still enable us to do a checksum against the * Jetpacks data but will prevent us from displaying the data on in the API as well as * other services. * * @since 1.6.3 * @since-jetpack 4.2.0 * * @param boolean false prevent post data from bing synced to WordPress.com * @param mixed $comment WP_COMMENT object */ if ( apply_filters( 'jetpack_sync_prevent_sending_comment_data', false, $comment ) ) { $blocked_comment = new \stdClass(); $blocked_comment->comment_ID = $comment->comment_ID; $blocked_comment->comment_date = $comment->comment_date; $blocked_comment->comment_date_gmt = $comment->comment_date_gmt; $blocked_comment->comment_approved = 'jetpack_sync_blocked'; return $blocked_comment; } return $comment; } /** * Whether a certain comment meta key is whitelisted for sync. * * @access public * * @param string $meta_key Comment meta key. * @return boolean Whether the meta key is whitelisted. */ public function is_whitelisted_comment_meta( $meta_key ) { return in_array( $meta_key, Settings::get_setting( 'comment_meta_whitelist' ), true ); } /** * Handler for filtering out non-whitelisted comment meta. * * @access public * * @param array $args Hook args. * @return array|boolean False if not whitelisted, the original hook args otherwise. */ public function filter_meta( $args ) { if ( $this->is_comment_type_allowed( $args[1] ) && $this->is_whitelisted_comment_meta( $args[2] ) ) { return $args; } return false; } /** * Expand the comment IDs to comment objects and meta before being serialized and sent to the server. * * @access public * * @param array $args The hook parameters. * @return array The expanded hook parameters. */ public function expand_comment_ids( $args ) { list( $comment_ids, $previous_interval_end ) = $args; $comments = get_comments( array( 'include_unapproved' => true, 'comment__in' => $comment_ids, 'orderby' => 'comment_ID', 'order' => 'DESC', ) ); return array( $comments, $this->get_metadata( $comment_ids, 'comment', Settings::get_setting( 'comment_meta_whitelist' ) ), $previous_interval_end, ); } }
Fatal error: Uncaught Error: Class "Automattic\Jetpack\Sync\Modules\Comments" not found in /htdocs/wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-modules.php:142 Stack trace: #0 [internal function]: Automattic\Jetpack\Sync\Modules::load_module('Automattic\\Jetp...') #1 /htdocs/wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-modules.php(126): array_map(Array, Array) #2 /htdocs/wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-modules.php(67): Automattic\Jetpack\Sync\Modules::initialize_modules() #3 /htdocs/wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-listener.php(86): Automattic\Jetpack\Sync\Modules::get_modules() #4 /htdocs/wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-listener.php(76): Automattic\Jetpack\Sync\Listener->init() #5 /htdocs/wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-listener.php(63): Automattic\Jetpack\Sync\Listener->__construct() #6 /htdocs/wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-actions.php(759): Automattic\Jetpack\Sync\Listener::get_instance() #7 /htdocs/wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-actions.php(146): Automattic\Jetpack\Sync\Actions::initialize_listener() #8 /htdocs/wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-main.php(111): Automattic\Jetpack\Sync\Actions::init() #9 /htdocs/wp-includes/class-wp-hook.php(324): Automattic\Jetpack\Sync\Main::on_plugins_loaded_late('') #10 /htdocs/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array) #11 /htdocs/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #12 /htdocs/wp-settings.php(578): do_action('plugins_loaded') #13 /htdocs/wp-config.php(85): require_once('/htdocs/wp-sett...') #14 /htdocs/wp-load.php(50): require_once('/htdocs/wp-conf...') #15 /htdocs/wp-blog-header.php(13): require_once('/htdocs/wp-load...') #16 /htdocs/index.php(17): require('/htdocs/wp-blog...') #17 {main} thrown in /htdocs/wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-modules.php on line 142