formPlugins[] = new \OneTag\Integrations\WPCF7(); } if ( class_exists( 'WPForms' ) ) { require_once plugin_dir_path( __FILE__ ) . 'includes/Integrations/WPForms.php'; $this->formPlugins[] = new \OneTag\Integrations\WPForms(); } if ( class_exists( 'Ninja_Forms' ) ) { require_once plugin_dir_path( __FILE__ ) . 'includes/Integrations/NinjaForms.php'; $this->formPlugins[] = new \OneTag\Integrations\NinjaForms(); } if ( class_exists( 'Forminator_API' ) ) { require_once plugin_dir_path( __FILE__ ) . 'includes/Integrations/Forminator.php'; $this->formPlugins[] = new \OneTag\Integrations\Forminator(); } if ( class_exists( 'FluentForm\App\App' ) ) { require_once plugin_dir_path( __FILE__ ) . 'includes/Integrations/FluentForms.php'; $this->formPlugins[] = new \OneTag\Integrations\FluentForms(); } if ( ! $this->settingsDTO && count( $this->formPlugins ) > 0 ) { $this->settingsDTO = $this->formPlugins[0]->getSettingsDTO(); } if ( class_exists( 'WooCommerce' ) ) { require_once plugin_dir_path( __FILE__ ) . '/woocommerce.php'; $this->commercePlugin = new OneTagWooCommerce(); if ( ! $this->settingsDTO ) { $this->settingsDTO = $this->commercePlugin->getSettingsDTO(); } } require_once plugin_dir_path( __FILE__ ) . 'includes/Integrations/PageView.php'; $pageView = new \OneTag\Integrations\PageView(); if ( ! $this->settingsDTO ) { $this->settingsDTO = $pageView->getSettingsDTO(); } new \OneTag\Admin\Setup(); add_action( 'wp_enqueue_scripts', array( $this, 'setupFrontendScripts' ) ); } function register_query_vars( $qvars ) { $qvars[] = 'refresh_feed'; return $qvars; } public function setupFrontendScripts() { try { if ( empty( $this->settingsDTO ) ) { return; } wp_enqueue_script( 'onetag-product', plugin_dir_url( __FILE__ ) . 'public/js/product.js', array(), 24112021, true ); $sentEvents = $this->getSentEvents(); $simplifiedList = array(); foreach ( $sentEvents as $dto ) { $simplifiedList[] = array( // DO NOT EDIT THE ATTRIBUTE NAME, ITS WHAT ONETAG TAG EXPECTS 'event_id' => $dto->getId(), 'event_name' => $dto->getName(), '_pixels' => $dto->getPixels(), 'value' => $dto->getValue(), 'currency' => $dto->getCurrency(), 'order_id' => $dto->getOrderId(), 'content_type' => $dto->getContentType(), 'content_ids' => $dto->getContentIds(), ); } wp_localize_script( 'onetag-product', 'onetagDetails', array( 'events' => $simplifiedList, ) ); } catch ( \Exception $e ) { } } /** * @return \OneTag\DTO\EventDTO[] */ public function getSentEvents() { $list = array(); if ( ! empty( $this->commercePlugin ) ) { $list = $this->commercePlugin->getSentEvents(); } foreach ( $this->formPlugins as $formPlugin ) { $list = array_merge( $list, $formPlugin->getSentEvents() ); } return $list; } /** * Cloning is forbidden. */ public function __clone() { wc_doing_it_wrong( __FUNCTION__, __( 'Cloning is forbidden.', 'adsmurai-one-tag' ), $this->version ); } /** * Unserializing instances of this class is forbidden. */ public function __wakeup() { wc_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'adsmurai-one-tag' ), $this->version ); } public function showForms() { $forms = array(); foreach ( $this->formPlugins as $formPlugin ) { $forms = array_merge( $formPlugin->getForms(), $forms ); } return rest_ensure_response( $forms ); } public function showFeed() { $response = array( 'url' => '', ); if ( ! $this->commercePlugin ) { return $response; } if (!empty($_GET["refresh_feed"])) { $this->commercePlugin->generateProductFeed(true); } $response['url'] = $this->commercePlugin->getProductFeedUrl(); return rest_ensure_response( $response ); } public function showSettings() { $compatiblePlugins = array(); $availableEvents = array( \OneTag\DTO\EventDTO::EVENT_PAGE_VIEW ); $eventsPerPlugin = array(); if ( class_exists( 'WooCommerce' ) ) { $compatiblePlugins[] = 'WOOCOMMERCE'; $eventsPerPlugin['WOOCOMMERCE'] = array( \OneTag\DTO\EventDTO::EVENT_PURCHASE, \OneTag\DTO\EventDTO::EVENT_ADD_TO_CART, \OneTag\DTO\EventDTO::EVENT_INITIATE_CHECKOUT, \OneTag\DTO\EventDTO::EVENT_VIEW_CONTENT, ); $availableEvents = array_merge( $availableEvents, $eventsPerPlugin['WOOCOMMERCE'] ); } if ( class_exists( 'WPCF7_ContactForm' ) ) { $compatiblePlugins[] = 'WPCF7'; $eventsPerPlugin['WPCF7'] = array( \OneTag\DTO\EventDTO::EVENT_LEAD ); $availableEvents = array_merge( $availableEvents, $eventsPerPlugin['WPCF7'] ); } if ( class_exists( 'WPForms' ) ) { $compatiblePlugins[] = 'WP_FORMS'; $eventsPerPlugin['WP_FORMS'] = array( \OneTag\DTO\EventDTO::EVENT_LEAD ); $availableEvents = array_merge( $availableEvents, $eventsPerPlugin['WP_FORMS'] ); } if ( class_exists( 'Ninja_Forms' ) ) { $compatiblePlugins[] = 'NINJA_FORMS'; $eventsPerPlugin['NINJA_FORMS'] = array( \OneTag\DTO\EventDTO::EVENT_LEAD ); $availableEvents = array_merge( $availableEvents, $eventsPerPlugin['NINJA_FORMS'] ); } if ( class_exists( 'Forminator' ) ) { $compatiblePlugins[] = 'FORMINATOR'; $eventsPerPlugin['FORMINATOR'] = array( \OneTag\DTO\EventDTO::EVENT_LEAD ); $availableEvents = array_merge( $availableEvents, $eventsPerPlugin['FORMINATOR'] ); } if ( class_exists( 'FluentForm\App\App' ) ) { $compatiblePlugins[] = 'FLUENT_FORMS'; $eventsPerPlugin['FLUENT_FORMS'] = array( \OneTag\DTO\EventDTO::EVENT_LEAD ); $availableEvents = array_merge( $availableEvents, $eventsPerPlugin['FORMINATOR'] ); } return rest_ensure_response( array( 'compatible_plugins' => $compatiblePlugins, 'available_events' => $availableEvents, 'events_per_plugin' => $eventsPerPlugin, ) ); } /** * Gets the main instance. * * Ensures only one instance can be loaded. * * @return \OneTag */ public static function instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } public static function setupAPI() { register_rest_route( self::API_NAMESPACE, '/forms', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( self::instance(), 'showForms' ), 'permission_callback' => static function () { if ( current_user_can( 'manage_options' ) ) { return true; } else { return new WP_Error( 'onetag_forbidden', __( 'You are not allowed to access onetag.', 'adsmurai-one-tag' ), array( 'status' => 403 ) ); } }, ), ) ); register_rest_route( self::API_NAMESPACE, '/settings', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( self::instance(), 'showSettings' ), 'permission_callback' => static function () { if ( current_user_can( 'manage_options' ) ) { return true; } else { return new WP_Error( 'onetag_forbidden', __( 'You are not allowed to access onetag.', 'adsmurai-one-tag' ), array( 'status' => 403 ) ); } }, ), ) ); register_rest_route( self::API_NAMESPACE, '/feed', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( self::instance(), 'showFeed' ), 'permission_callback' => static function () { if ( current_user_can( 'manage_options' ) ) { return true; } else { return new WP_Error( 'onetag_forbidden', __( 'You are not allowed to access onetag.', 'adsmurai-one-tag' ), array( 'status' => 403 ) ); } }, ), ) ); } } endif; add_action( 'plugins_loaded', 'one_tag_init', 10 ); add_action( 'rest_api_init', static function () { try { OneTag::setupAPI(); } catch ( \Exception $e ) { } }, 10, 0 ); /** * Initialize the plugin. * * @since 0.1.0 */ function one_tag_init() { load_plugin_textdomain( 'OneTag', false, plugin_basename( __DIR__ ) . '/languages' ); try { OneTag::instance(); } catch ( \Exception $e ) { if ( ( ! empty( $_SERVER['HTTP_HOST'] ) && strpos( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ), 'localhost' ) === 0 ) || ! empty( $_REQUEST['onetag_debug'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Received data isn't used in a way that can be exploited throw $e; } } }