' . esc_html__( 'Settings', 'adsmurai-one-tag' ) . ''; array_unshift( $settings, $settings_link ); return $settings; } /** * Load all necessary dependencies. * * @since 1.0.0 */ public function register_scripts( $hook ) { if ( $hook !== 'settings_page_one-tag' ) { return; } $script_path = '/build/index.js'; $script_asset_path = dirname( ADSMURAI_MAIN_PLUGIN_FILE ) . '/build/index.asset.php'; $script_asset = file_exists( $script_asset_path ) ? require $script_asset_path : array( 'dependencies' => array(), 'version' => filemtime( $script_path ), ); $script_url = plugins_url( $script_path, ADSMURAI_MAIN_PLUGIN_FILE ); wp_register_script( 'one-tag', $script_url, $script_asset['dependencies'], $script_asset['version'], true ); wp_enqueue_style( 'wp-components-style', // Handle name includes_url( 'css/dist/components/style.css' ), // WordPress components styles path array(), 1 ); wp_register_style( 'one-tag', plugins_url( '/build/index.css', ADSMURAI_MAIN_PLUGIN_FILE ), // Add any dependencies styles may have, such as wp-components. array( 'wp-components-style' ), filemtime( dirname( ADSMURAI_MAIN_PLUGIN_FILE ) . '/build/index.css' ) ); /* Expose wp token to client side */ wp_localize_script( 'one-tag', 'wpData', array( 'oneTagWpToken' => wp_create_nonce( 'wp_rest' ) ) ); wp_enqueue_script( 'one-tag' ); wp_enqueue_style( 'one-tag' ); /* Expose wp token to client side */ wp_register_script( 'adsmurai-ot-token-script', plugins_url( '/build/wp-token.js', ADSMURAI_MAIN_PLUGIN_FILE ), array(), 1, true ); wp_localize_script( 'adsmurai-ot-token-script', 'wpData', array( 'oneTagWpToken' => wp_create_nonce( 'wp_rest' ) ) ); wp_enqueue_script( 'adsmurai-ot-token-script' ); } /** * Register page in WordPress admin * * @since 1.0.0 */ public function register_page() { if ( ! function_exists( 'add_options_page' ) ) { return; } add_options_page( __( 'Adsmurai One Tag', 'adsmurai-one-tag' ), // Page title __( 'OneTag', 'adsmurai-one-tag' ), // Menu title 'manage_options', // Capability (only admins can access) 'one-tag', function () { echo '
'; } ); } public function sanitize_one_tag_settings( $input ) { if ( ! is_array( $input ) && ! is_object( $input ) ) { return array(); // Return an empty array if not valid } $input = (array) $input; // Convert object to array for easier processing // Define schema structure $schema = array( 'token' => 'string', 'apiToken' => 'string', 'organizationId' => 'string', 'gdprCookie' => 'string', 'pixels' => 'array', 'forms' => 'array', ); // Validate and sanitize properties $sanitized = array(); foreach ( $schema as $key => $type ) { if ( isset( $input[ $key ] ) ) { if ( $type === 'string' && is_string( $input[ $key ] ) ) { $sanitized[ $key ] = sanitize_text_field( $input[ $key ] ); } elseif ( $type === 'array' && is_array( $input[ $key ] ) ) { if ( $key === 'pixels' ) { $sanitized[ $key ] = $this->sanitize_pixels( $input[ $key ] ); } elseif ( $key === 'forms' ) { $sanitized[ $key ] = $this->sanitize_forms( $input[ $key ] ); } else { $sanitized[ $key ] = array_values( $input[ $key ] ); } } } } return (object) $sanitized; } private function sanitize_pixels( $pixels ) { $sanitized_pixels = array(); foreach ( $pixels as $pixel ) { if ( ! is_array( $pixel ) ) { continue; } $sanitized_pixel = array(); $sanitized_pixel['id'] = isset( $pixel['id'] ) ? sanitize_text_field( $pixel['id'] ) : ''; $sanitized_pixel['name'] = isset( $pixel['name'] ) ? sanitize_text_field( $pixel['name'] ) : ''; $sanitized_pixel['type'] = isset( $pixel['type'] ) ? sanitize_text_field( $pixel['type'] ) : ''; $sanitized_pixel['access_token'] = isset( $pixel['access_token'] ) ? sanitize_text_field( $pixel['access_token'] ) : ''; if ( isset( $pixel['events'] ) && is_array( $pixel['events'] ) ) { $sanitized_pixel['events'] = array_map( 'sanitize_text_field', $pixel['events'] ); } else { $sanitized_pixel['events'] = array(); } $sanitized_pixels[] = $sanitized_pixel; } return $sanitized_pixels; } private function sanitize_forms( $forms ) { $sanitized_forms = array(); foreach ( $forms as $form ) { if ( ! is_array( $form ) ) { continue; } $sanitized_form = array(); $sanitized_form['formId'] = isset( $form['formId'] ) ? sanitize_text_field( $form['formId'] ) : ''; $sanitized_form['plugin'] = isset( $form['plugin'] ) ? sanitize_text_field( $form['plugin'] ) : ''; if ( isset( $form['matches'] ) && is_array( $form['matches'] ) ) { $sanitized_form['matches'] = array(); foreach ( $form['matches'] as $match ) { if ( ! is_array( $match ) ) { continue; } $sanitized_match = array(); $sanitized_match['formField'] = isset( $match['formField'] ) ? sanitize_text_field( $match['formField'] ) : ''; $sanitized_match['otField'] = isset( $match['otField'] ) ? sanitize_text_field( $match['otField'] ) : ''; $sanitized_form['matches'][] = $sanitized_match; } } else { $sanitized_form['matches'] = array(); } $sanitized_forms[] = $sanitized_form; } return $sanitized_forms; } public function register_one_tag_settings() { $default = array( 'token' => '', 'pixels' => array(), ); $schema = array( 'type' => 'object', 'properties' => array( 'token' => array( 'type' => 'string', ), 'apiToken' => array( 'type' => 'string', ), 'organizationId' => array( 'type' => 'string', ), 'gdprCookie' => array( 'type' => 'string', ), 'pixels' => array( 'type' => 'array', 'items' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'string', 'required' => true, ), 'name' => array( 'type' => 'string', 'required' => false, ), 'type' => array( 'type' => 'string', 'required' => true, ), 'access_token' => array( 'type' => 'string', 'required' => false, ), 'events' => array( 'type' => 'array', 'items' => array( 'type' => 'string', 'required' => true, ), 'required' => true, ), ), ), ), 'forms' => array( 'type' => 'array', 'items' => array( 'type' => 'object', 'properties' => array( 'formId' => array( 'type' => 'string', 'required' => true, ), 'plugin' => array( 'type' => 'string', 'required' => true, ), 'matches' => array( 'type' => 'array', 'items' => array( 'type' => 'object', 'required' => true, 'properties' => array( 'formField' => array( 'type' => 'string', 'required' => true, ), 'otField' => array( 'type' => 'string' ), ), ), ), ), ), ), ), ); $args = array( 'type' => 'object', 'default' => $default, 'show_in_rest' => array( 'schema' => $schema, ), 'sanitize_callback' => array( $this, 'sanitize_one_tag_settings' ), ); register_setting( 'options', 'one_tag_settings', $args ); // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic -- It's not dynamic... } } }