diff --git a/echodash.php b/echodash.php index f646b80..f92d256 100755 --- a/echodash.php +++ b/echodash.php @@ -275,6 +275,7 @@ private function integrations_includes() { 'learndash' => 'SFWD_LMS', 'lifterlms' => 'LLMS', 'pretty-links' => 'PrliLink', + 'searchwp' => 'SearchWP', 'woocommerce' => 'WooCommerce', 'woo-subscriptions' => 'WC_Subscriptions_Product', ); diff --git a/includes/integrations/searchwp/class-echodash-searchwp.php b/includes/integrations/searchwp/class-echodash-searchwp.php new file mode 100644 index 0000000..13f63bf --- /dev/null +++ b/includes/integrations/searchwp/class-echodash-searchwp.php @@ -0,0 +1,190 @@ + array( + 'name' => __( 'Search Performed', 'echodash' ), + 'description' => __( 'Triggered when a user performs a search using SearchWP.', 'echodash' ), + 'has_global' => true, + 'option_types' => array( 'search' ), + 'enabled_by_default' => false, + 'default_event' => array( + 'name' => __( 'Search Query', 'echodash' ), + 'mappings' => array( + 'search_terms' => '{search:search_terms}', + 'results_count' => '{search:results_count}', + 'engine_name' => '{search:engine_name}', + ), + ), + ), + 'search_no_results' => array( + 'name' => __( 'Search No Results', 'echodash' ), + 'description' => __( 'Triggered when a user performs a search that returns zero results.', 'echodash' ), + 'has_global' => true, + 'option_types' => array( 'search' ), + 'enabled_by_default' => true, + 'default_event' => array( + 'name' => __( 'Search No Results', 'echodash' ), + 'mappings' => array( + 'search_terms' => '{search:search_terms}', + 'engine_name' => '{search:engine_name}', + ), + ), + ), + ); + + return $triggers; + } + + /** + * Gets the search options. + * + * @since 2.0.0 + * + * @return array The search options. + */ + public function get_search_options() { + return array( + 'name' => __( 'SearchWP Query', 'echodash' ), + 'type' => 'search', + 'options' => array( + array( + 'meta' => 'search_terms', + 'preview' => __( 'wordpress tutorial', 'echodash' ), + 'placeholder' => __( 'The search terms entered by the user', 'echodash' ), + ), + array( + 'meta' => 'results_count', + 'preview' => '42', + 'placeholder' => __( 'The number of results found', 'echodash' ), + ), + array( + 'meta' => 'query_time', + 'preview' => '0.125', + 'placeholder' => __( 'Search execution time in seconds', 'echodash' ), + ), + array( + 'meta' => 'engine_name', + 'preview' => __( 'default', 'echodash' ), + 'placeholder' => __( 'The SearchWP engine used', 'echodash' ), + ), + array( + 'meta' => 'timestamp', + 'preview' => gmdate( 'Y-m-d H:i:s' ), + 'placeholder' => __( 'When the search was performed', 'echodash' ), + ), + ), + ); + } + + /** + * Handle search query performed event. + * + * @since 2.0.0 + * + * @param object $query The SearchWP Query object. + */ + public function search_query_performed( $query ) { + if ( empty( $query ) || ! method_exists( $query, 'get_keywords' ) ) { + return; + } + + // Get search data. + $search_terms = $query->get_keywords(); + $results_count = isset( $query->found_results ) ? (int) $query->found_results : 0; + $query_time = isset( $query->query_time ) ? (float) $query->query_time : 0; + $engine_name = method_exists( $query, 'get_engine' ) && method_exists( $query->get_engine(), 'get_name' ) ? $query->get_engine()->get_name() : 'default'; + + // Create search data array. + $search_data = array( + 'search_terms' => $search_terms, + 'results_count' => $results_count, + 'query_time' => $query_time, + 'engine_name' => $engine_name, + 'timestamp' => current_time( 'mysql' ), + ); + + // Determine which trigger to fire. + if ( $results_count > 0 ) { + // Track successful search. + $this->track_event( + 'search_performed', + array(), + array( + 'search' => $search_data, + ) + ); + } else { + // Track zero results search. + $this->track_event( + 'search_no_results', + array(), + array( + 'search' => $search_data, + ) + ); + } + } +} + +new EchoDash_SearchWP(); diff --git a/includes/integrations/searchwp/searchwp-icon.png b/includes/integrations/searchwp/searchwp-icon.png new file mode 100644 index 0000000..0ad61fb Binary files /dev/null and b/includes/integrations/searchwp/searchwp-icon.png differ