is->enqueue_ces_survey_for_edited_shop_order(); } } /** * Maybe enqueue the CES survey, if product is being added or edited. * * @param string $new_status The new status. * @param string $old_status The old status. */ private function maybe_enqueue_ces_survey_for_product( $new_status, $old_status ) { if ( 'publish' !== $new_status ) { return; } if ( 'publish' !== $old_status ) { $this->enqueue_ces_survey_for_new_product(); } else { $this->enqueue_ces_survey_for_edited_product(); } } /** * Enqueue the CES survey trigger for a new product. */ private function enqueue_ces_survey_for_new_product() { if ( $this->has_been_shown( self::PRODUCT_ADD_PUBLISH_ACTION_NAME ) ) { return; } $this->enqueue_to_ces_tracks( array( 'action' => self::PRODUCT_ADD_PUBLISH_ACTION_NAME, 'label' => __( 'How easy was it to add a product?', 'woocommerce' ), 'onsubmit_label' => $this->onsubmit_label, 'pagenow' => 'product', 'adminpage' => 'post-php', 'props' => array( 'product_count' => $this->get_product_count(), ), ) ); } /** * Enqueue the CES survey trigger for an existing product. */ private function enqueue_ces_survey_for_edited_product() { if ( $this->has_been_shown( self::PRODUCT_UPDATE_ACTION_NAME ) ) { return; } $this->enqueue_to_ces_tracks( array( 'action' => self::PRODUCT_UPDATE_ACTION_NAME, 'label' => __( 'How easy was it to edit your product?', 'woocommerce' ), 'onsubmit_label' => $this->onsubmit_label, 'pagenow' => 'product', 'adminpage' => 'post-php', 'props' => array( 'product_count' => $this->get_product_count(), ), ) ); } /** * Enqueue the CES survey trigger for an existing shop order. */ private function enqueue_ces_survey_for_edited_shop_order() { if ( $this->has_been_shown( self::SHOP_ORDER_UPDATE_ACTION_NAME ) ) { return; } $this->enqueue_to_ces_tracks( array( 'action' => self::SHOP_ORDER_UPDATE_ACTION_NAME, 'label' => __( 'How easy was it to update an order?', 'woocommerce' ), 'onsubmit_label' => $this->onsubmit_label, 'pagenow' => 'shop_order', 'adminpage' => 'post-php', 'props' => array( 'order_count' => $this->get_shop_order_count(), ), ) ); } /** * Maybe clear the CES tracks queue, executed on every page load. If the * clear option is set it clears the queue. In practice, this executes a * page load after the queued CES tracks are displayed on the client, which * sets the clear option. */ public function maybe_clear_ces_tracks_queue() { $clear_ces_tracks_queue_for_page = get_option( self::CLEAR_CES_TRACKS_QUEUE_FOR_PAGE_OPTION_NAME, false ); if ( ! $clear_ces_tracks_queue_for_page ) { return; } $queue = get_option( self::CES_TRACKS_QUEUE_OPTION_NAME, array() ); $remaining_items = array_filter( $queue, function ( $item ) use ( $clear_ces_tracks_queue_for_page ) { return $clear_ces_tracks_queue_for_page['pagenow'] !== $item['pagenow'] || $clear_ces_tracks_queue_for_page['adminpage'] !== $item['adminpage']; } ); update_option( self::CES_TRACKS_QUEUE_OPTION_NAME, array_values( $remaining_items ) ); update_option( self::CLEAR_CES_TRACKS_QUEUE_FOR_PAGE_OPTION_NAME, false ); } /** * Appends a script to footer to trigger CES on adding product categories. */ public function add_script_track_product_categories() { if ( $this->has_been_shown( self::ADD_PRODUCT_CATEGORIES_ACTION_NAME ) ) { return; } wc_enqueue_js( $this->get_script_track_edit_php( self::ADD_PRODUCT_CATEGORIES_ACTION_NAME, __( 'How easy was it to add product category?', 'woocommerce' ) ) ); } /** * Appends a script to footer to trigger CES on adding product tags. */ public function add_script_track_product_tags() { if ( $this->has_been_shown( self::ADD_PRODUCT_TAGS_ACTION_NAME ) ) { return; } wc_enqueue_js( $this->get_script_track_edit_php( self::ADD_PRODUCT_TAGS_ACTION_NAME, __( 'How easy was it to add a product tag?', 'woocommerce' ) ) ); } /** * Maybe enqueue the CES survey on product import, if step is done. */ public function run_on_product_import() { // We're only interested in when the importer completes. if ( empty( $_GET['step'] ) || 'done' !== $_GET['step'] ) { // phpcs:ignore CSRF ok. return; } if ( $this->has_been_shown( self::IMPORT_PRODUCTS_ACTION_NAME ) ) { return; } $this->enqueue_to_ces_tracks( array( 'action' => self::IMPORT_PRODUCTS_ACTION_NAME, 'label' => __( 'How easy was it to import products?', 'woocommerce' ), 'onsubmit_label' => $this->onsubmit_label, 'pagenow' => 'product_page_product_importer', 'adminpage' => 'product_page_product_importer', 'props' => (object) array(), ) ); } /** * Enqueue the CES survey trigger for setting changes. */ public function run_on_update_options() { // $current_tab is set when WC_Admin_Settings::save_settings is called. global $current_tab; global $current_section; if ( $this->has_been_shown( self::SETTINGS_CHANGE_ACTION_NAME ) ) { return; } $props = array( 'settings_area' => $current_tab, ); if ( $current_section ) { $props['settings_section'] = $current_section; } $this->enqueue_to_ces_tracks( array( 'action' => self::SETTINGS_CHANGE_ACTION_NAME, 'label' => __( 'How easy was it to update your settings?', 'woocommerce' ), 'onsubmit_label' => $this->onsubmit_label, 'pagenow' => 'woocommerce_page_wc-settings', 'adminpage' => 'woocommerce_page_wc-settings', 'props' => (object) $props, ) ); } /** * Enqueue the CES survey on adding new product attributes. */ public function run_on_add_product_attributes() { if ( $this->has_been_shown( self::ADD_PRODUCT_ATTRIBUTES_ACTION_NAME ) ) { return; } $this->enqueue_to_ces_tracks( array( 'action' => self::ADD_PRODUCT_ATTRIBUTES_ACTION_NAME, 'label' => __( 'How easy was it to add a product attribute?', 'woocommerce' ), 'onsubmit_label' => $this->onsubmit_label, 'pagenow' => 'product_page_product_attributes', 'adminpage' => 'product_page_product_attributes', 'props' => (object) array(), ) ); } /** * Determine on initiating CES survey on searching for product or orders. */ public function run_on_load_edit_php() { $allowed_types = array( 'product', 'shop_order' ); $post_type = get_current_screen()->post_type; // We're only interested for certain post types. if ( ! in_array( $post_type, $allowed_types, true ) ) { return; } // Determine whether request is search by "s" GET parameter. if ( empty( $_GET['s'] ) ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended return; } $page_now = 'edit-' . $post_type; $this->enqueue_ces_survey_for_search( $post_type, $page_now, 'edit-php' ); } }