self::get_default_external_business_id(), FacebookPluginConfig::IS_FBE_INSTALLED_KEY => self::get_default_is_fbe_installed(), ); if ( isset( $old_options[ FacebookPluginConfig::OLD_ACCESS_TOKEN_KEY ] ) && ! empty( $old_options[ FacebookPluginConfig::OLD_ACCESS_TOKEN_KEY ] ) ) { self::$options[ FacebookPluginConfig::ACCESS_TOKEN_KEY ] = $old_options[ FacebookPluginConfig::OLD_ACCESS_TOKEN_KEY ]; } else { self::$options[ FacebookPluginConfig::ACCESS_TOKEN_KEY ] = self::get_default_access_token(); } if ( isset( $old_options[ FacebookPluginConfig::OLD_PIXEL_ID_KEY ] ) && ! empty( $old_options[ FacebookPluginConfig::OLD_PIXEL_ID_KEY ] ) && is_numeric( $old_options[ FacebookPluginConfig::OLD_PIXEL_ID_KEY ] ) ) { self::$options[ FacebookPluginConfig::PIXEL_ID_KEY ] = $old_options[ FacebookPluginConfig::OLD_PIXEL_ID_KEY ]; } else { self::$options[ FacebookPluginConfig::PIXEL_ID_KEY ] = self::get_default_pixel_id(); } } else { self::$options = \get_option( FacebookPluginConfig::SETTINGS_KEY, array( FacebookPluginConfig::PIXEL_ID_KEY => self::get_default_pixel_id(), FacebookPluginConfig::ACCESS_TOKEN_KEY => self::get_default_access_token(), FacebookPluginConfig::EXTERNAL_BUSINESS_ID_KEY => self::get_default_external_business_id(), FacebookPluginConfig::IS_FBE_INSTALLED_KEY => self::get_default_is_fbe_installed(), ) ); } } /** * Retrieves the Facebook pixel ID. * * If the pixel ID is not set in the options, the default pixel * ID is returned. * * @return string The Facebook pixel ID. */ public static function get_pixel_id() { if ( isset( self::$options[ FacebookPluginConfig::PIXEL_ID_KEY ] ) ) { return self::$options[ FacebookPluginConfig::PIXEL_ID_KEY ]; } return self::get_default_pixel_id(); } /** * Retrieves the Facebook pixel ID using the old method name for backwards compatibility. * * @return string The Facebook pixel ID. */ public static function getPixelId() { // phpcs:ignore return self::get_pixel_id(); } /** * Retrieves the external business ID. * * If the external business ID is not set in the options, * the default external * business ID is returned. * * @return string The external business ID. */ public static function get_external_business_id() { if ( isset( self::$options[ FacebookPluginConfig::EXTERNAL_BUSINESS_ID_KEY ] ) ) { return self::$options[ FacebookPluginConfig::EXTERNAL_BUSINESS_ID_KEY ]; } return self::get_default_external_business_id(); } /** * Retrieves the status indicating whether the Facebook Business * Extension is installed. * * If the FBE installed status is not set in the options, the * default FBE installed status is returned. * * @return string The FBE installed status. */ public static function get_is_fbe_installed() { if ( isset( self::$options[ FacebookPluginConfig::IS_FBE_INSTALLED_KEY ] ) ) { return self::$options[ FacebookPluginConfig::IS_FBE_INSTALLED_KEY ]; } return self::get_default_is_fbe_installed(); } /** * Retrieves the Facebook access token. * * If the access token is not set in the options, the * default access token is returned. * * @return string The Facebook access token. */ public static function get_access_token() { if ( isset( self::$options[ FacebookPluginConfig::ACCESS_TOKEN_KEY ] ) ) { return self::$options[ FacebookPluginConfig::ACCESS_TOKEN_KEY ]; } return self::get_default_access_token(); } /** * Retrieves the user information. * * This function returns the user information stored in the class. * The user information is an array that may contain keys like 'email', * 'first_name', and 'last_name', depending on the data available for the * current user. * * @return array The user information. */ public static function get_user_info() { return self::$user_info; } /** * Registers an action hook to set the user information. * * The 'register_user_info' method is called when the 'init' * action is triggered. * The method sets the user information stored in the class. * * @return void */ public static function set_user_info() { add_action( 'init', array( 'FacebookPixelPlugin\\Core\\FacebookWordpressOptions', 'register_user_info', ), 0 ); } /** * Registers the user information. * * This function is called when the 'init' action is triggered, and it * sets the user information stored in the class. The user information * is an array that may contain keys like 'email', 'first_name', and * 'last_name', depending on the data available for the current user. * * @return void */ public static function register_user_info() { $current_user = wp_get_current_user(); if ( 0 === $current_user->ID ) { self::$user_info = array(); } else { $user_info = array_filter( array( AAMSettingsFields::EMAIL => $current_user->user_email, AAMSettingsFields::FIRST_NAME => $current_user->user_firstname, AAMSettingsFields::LAST_NAME => $current_user->user_lastname, ), function ( $value ) { return null !== $value && '' !== $value; } ); self::$user_info = AAMFieldsExtractor::get_normalized_user_data( $user_info ); } } /** * Retrieves the version information. * * The version information is an array that contains the keys * 'pluginVersion' and 'source'. The 'pluginVersion' key contains * the current version of the plugin, and the 'source' key * contains the source of the plugin. * * @return array The version information. */ public static function get_version_info() { return self::$version_info; } /** * Sets the version information. * * The version information is an array that contains the * keys 'pluginVersion', 'source', and 'version'. The * 'pluginVersion' key contains the current version of the * plugin, the 'source' key contains the source of the plugin, * and the 'version' key contains the current version of WordPress. * * @return void */ public static function set_version_info() { global $wp_version; self::$version_info = array( 'pluginVersion' => FacebookPluginConfig::PLUGIN_VERSION, 'source' => FacebookPluginConfig::SOURCE, 'version' => $wp_version, ); } /** * Constructs the agent string from version information. * * This function returns a formatted string that combines the source, * WordPress version, and plugin version from the version information array. * * @return string The constructed agent string. */ public static function get_agent_string() { $source = self::$version_info['source']; if ( function_exists( 'get_option' ) && 1 === get_option( 'is_wordpress_com_hosted' ) ) { $source .= '_1'; } return sprintf( '%s-%s-%s', $source, self::$version_info['version'], self::$version_info['pluginVersion'] ); } /** * Determines whether the current site is hosted on WordPress.com. * * This method makes a request to the WordPress.com REST API to check if the * current site is hosted on WordPress.com. * * @return bool Whether the current site is hosted on WordPress.com. */ public static function is_wordpress_com_hosted() { if ( defined( 'WPCOMSH_VERSION' ) && defined( 'IS_ATOMIC' ) && IS_ATOMIC ) { return 1; } return 0; } /** * Retrieves the AdsPixelSettings object from the AAM settings. * * @return AdsPixelSettings The AdsPixelSettings object * containing the AAM settings. */ public static function get_aam_settings() { return self::$aam_settings; } /** * Retrieves the AdsPixelSettings object from the AAM settings. * * This method first checks if there are any AAM settings cached in the * WordPress database. If there are, they are converted into an * AdsPixelSettings object and returned. If there are no cached * settings, the method fetches the settings from the Facebook * domain and caches them in the WordPress * database if they are not null. * * @return void */ private static function set_fbe_based_aam_settings() { $installed_pixel = self::get_pixel_id(); $settings_as_array = get_transient( FacebookPluginConfig::AAM_SETTINGS_KEY ); if ( false !== $settings_as_array ) { $aam_settings = new AdsPixelSettings(); $aam_settings->setPixelId( $settings_as_array['pixelId'] ); $aam_settings->setEnableAutomaticMatching( $settings_as_array['enableAutomaticMatching'] ); $aam_settings->setEnabledAutomaticMatchingFields( $settings_as_array['enabledAutomaticMatchingFields'] ); if ( $installed_pixel == $aam_settings->getPixelId() ) { // phpcs:ignore Universal.Operators.StrictComparisons self::$aam_settings = $aam_settings; } } if ( ! self::$aam_settings ) { $refresh_interval = self::AAM_SETTINGS_REFRESH_IN_MINUTES * MINUTE_IN_SECONDS; $aam_settings = AdsPixelSettings::buildFromPixelId( $installed_pixel ); if ( $aam_settings ) { $settings_as_array = array( 'pixelId' => $aam_settings->getPixelId(), 'enableAutomaticMatching' => $aam_settings->getEnableAutomaticMatching(), 'enabledAutomaticMatchingFields' => $aam_settings->getEnabledAutomaticMatchingFields(), ); set_transient( FacebookPluginConfig::AAM_SETTINGS_KEY, $settings_as_array, $refresh_interval ); self::$aam_settings = $aam_settings; } } } /** * If the old settings are present and the user has opted-in to use PII, * the AAM settings are set to enable automatic matching and all its fields. * Otherwise, the AAM settings are set to disable automatic matching * and all its fields. */ private static function set_old_aam_settings() { $old_options = \get_option( FacebookPluginConfig::OLD_SETTINGS_KEY ); if ( $old_options && isset( $old_options[ FacebookPluginConfig::OLD_USE_PII ] ) && $old_options[ FacebookPluginConfig::OLD_USE_PII ] ) { self::$aam_settings = new AdsPixelSettings( array( 'enableAutomaticMatching' => true, 'enabledAutomaticMatchingFields' => AAMSettingsFields::get_all_fields(), ) ); } else { self::$aam_settings = new AdsPixelSettings( array( 'enableAutomaticMatching' => false, 'enabledAutomaticMatchingFields' => array(), ) ); } } /** * Sets the AdsPixelSettings based on the installation status. * * This method initializes the AAM settings to null and * checks if a pixel ID is set. * If no pixel ID is found, the method returns early. * If the Facebook Business Extension * (FBE) is installed, the AAM settings are set using * the FBE-based settings. * Otherwise, the old AAM settings are used. * * @return void */ private static function set_aam_settings() { self::$aam_settings = null; if ( empty( self::get_pixel_id() ) ) { return; } if ( self::get_is_fbe_installed() ) { self::set_fbe_based_aam_settings(); } else { self::set_old_aam_settings(); } } }