�PNG  IHDR22?�� pHYs  �� OiCCPPhotoshop ICC profilexڝSgTS�=���BK���KoR RB���&*! J�!��Q�EEȠ�����Q, � ��!���������{�kּ������>���� �H3Q5� �B�������.@� $p�d!s�#�~<<+"��x� �M��0���B�\���t�8K�@z�B�@F���&S�`�cb�P-`'������{[�!�� e�Dh;��V�EX0fK�9�-0IWfH���� � 0Q��){`�##x��F�W<�+��*x��<�$9E�[-qWW.(�I+6aa�@.�y�2�4���������x����6��_-��"bb���ϫp@�t~��,/��;�m��%�h^ �u��f�@����W�p�~<�5�j>{�-�]c�K'Xt����o��(�h���w��?�G�%�fI�q^D$.Tʳ?�D��*�A��,���� �`6�B$��BB d�r`)��B(�Ͱ*`/�@4�Qh��p.�U�=p�a��(�� A�a!ڈb�X#����!�H�$ ɈQ"K�5H1R�T UH�=r9�\F��;�2����G1���Q=� �C��7�F� �dt1�����r�=�6��Ыhڏ>C�0��3�l0.��B�8, �c˱"� ���V����cϱw�E� 6wB aAHXLXN�H� $4� 7 �Q�'"��K�&���b21�XH,#��/{�C�7$�C2'��I��T��F�nR#�,��4H#���dk�9�, +ȅ����3��!�[ �b@q��S�(R�jJ��4�e�2AU��Rݨ�T5�ZB���R�Q��4u�9̓IK�����hh�i��t�ݕN��W���G���w ��Ljg(�gw��L�Ӌ�T071���oUX*�*|�� �J�&�*/T����ު U�U�T��^S}�FU3S� Ԗ�U��P�SSg�;���g�oT?�~Y��Y�L�OC�Q��_�� c�x,!k ��u�5�&���|v*�����=���9C3J3W�R�f?�q��tN �(���~���)�)�4L�1e\k����X�H�Q�G�6������E�Y��A�J'\'Gg����S�Sݧ �M=:��.�k���Dw�n��^��Lo��y���}/�T�m���G X� $� �<�5qo</���QC]�@C�a�a�ᄑ��<��F�F�i�\�$�m�mƣ&&!&KM�M�RM��)�;L;L���͢�֙5�=1�2��כ߷`ZxZ,����eI��Z�Yn�Z9Y�XUZ]�F���%ֻ�����N�N���gð�ɶ�����ۮ�m�}agbg�Ů��}�}��= ���Z~s�r:V:ޚΜ�?}����/gX���3��)�i�S��Ggg�s�󈋉K��.�>.���Ƚ�Jt�q]�z���������ۯ�6�i�ܟ�4�)�Y3s���C�Q��? ��0k߬~OCO�g��#/c/�W�װ��w��a�>�>r��>�<7�2�Y_�7��ȷ�O�o�_��C#�d�z����%g��A�[��z|!��?:�e����A���AA�������!h�쐭!��Α�i�P~���a�a��~ '���W�?�p�X�1�5w��Cs�D�D�Dޛg1O9�-J5*>�.j<�7�4�?�.fY��X�XIlK9.*�6nl������� �{�/�]py�����.,:�@L�N8��A*��%�w%� y��g"/�6ш�C\*N�H*Mz�쑼5y$�3�,幄'���L Lݛ:��v m2=:�1����qB�!M��g�g�fvˬe����n��/��k���Y- �B��TZ(�*�geWf�͉�9���+��̳�ې7�����ᒶ��KW-X潬j9�������(�x��oʿ�ܔ���Ĺd�f�f���-�[����n �ڴ �V����E�/��(ۻ��C���<��e����;?T�T�T�T6��ݵa��n��{��4���[���>ɾ�UUM�f�e�I���?�������m]�Nmq����#�׹���=TR��+�G�����w- 6 U����#pDy��� �� :�v�{���vg/jB��F�S��[b[�O�>����z�G��499�?r����C�d�&����ˮ/~�����јѡ�򗓿m|������������x31^�V���w�w��O�| (�h���SЧ��������c3-� cHRMz%������u0�`:�o�_�F5IDATx��ytUս�?�;���fN$$��@B�!�k��� -*�gE���j�O�kQ�ʠ`U�B_ �AÐ0%�Ȕ�@r3ޛ;߳��$�@��v����^�s����������$��ߢ !�W��5�̱'-٘������4�>�Rƫ$�G��0U�H}�_ _� #��k�MJ/=�9Du *ڲ�k�PQ;0��j|�*�$�^컂脛y���>�z×� B��� ��(�:$�ʔ_�6��C!B����MVx��a��ζyY����j��ƋY2�9�F�{r��r�딟F��Q��hY6��� x��[l��RC��������nt@��;P��3��� F�w��7��:�P���ɋ'���mj���Bƪ�$�Q�L��y��߷g��\���z����v5�p�v$�v櫻�`6~K �Y�X����,�:��"�o�e0II"(��,������l_�C���/4�+f�E�!q��:����u����c��樼.UE�@� �]K�W���$-�h]�JG���Dz)�`��.�A/ ���mᏣc�3jK� �!�>���$�Q�!�_�? \�C2(E �M$Z I{�U���s�ǣ���=��@Բe ޻��W�����@ �w����}�9v,)��.}*|"�s��di���,���G�]W�AC�������|<����[��ߕ������K�*�G nߴ!��‹��J�!��6(L�a)��Am��������;adf��:�xm��nh�;֦I��(���o�C~ډa?�=��s����h��E������xp����)���_���t��W�����´;8���`dz=(��@@�+,�;�`�fhl��XȌ�3,uI�~z(J����� �ZX�A����H'�À�㜦�82|��a�n�z2��I���� ���EY�h�ε<�0�q�{��C��v�˅��?"@��χ�x�WS+��N�~�KQ�J�8ل�� _.W��� !TU{�����,��"�� �;�������HI S� ��))�o�1�H�D� ���� (� ;����"Ҕ��,���� �F=���O��&�Ph>�`���y��߲ &S��P�<��'��1s�1cĂ�N����2)����@�u��tv��<�+�-&�����h��[!FSG���' � z z0��<����8d&�����E0�,��<��"!P�b4��� �{S�����X����.�囤���T��hEF��X�q0"-<����h���]�Py*����F⩍��@v�˂�Y08�� .q��a��s����(!<̫1�3�L�H��7sYW�`��qtu�\7��?�@�Y�� %Ka�x�GZ�*W�P��y�����N0r9��Z�c�%���q,��k�����!!,��>���IB��� �x1�Xu�Y���v-���ֺ��N������JP��a�2`�e5��w��u;�6�i���̃��(K���ťa��(l51��JL��O'%���]���ٖ뿉!��{*�p+f ���i8��D�ȑ���ܩ�M��V��ӟz��x�7Z�1i}�^͜���6e����x��)u�d�ˏ�V�eG�\�X1�����=n�U'�~Y����L�\,�(d!x ,�:�TW8� M`r���@��r+v�Q0��>^;˴�VbF��('����� �*O��i��j�Ӆ����^�S3&� ���{������.ی�� N;��NU%d0�Ce\r"�C�b��&t7N�7��� �ȪD�[%l�۪�$��'m?�A9tI�9>��NVL ��� 6v~M�����ъ�B���0����9$&=ώm��W�0���L���&��E���f�f��'+�9��hv��7,���M��6�D�&�^� �����8 D֮�����o��k^z��O��hP�8q�5d*��YVq3�{��������#LX4�?��jP=3�~�؅c�ݰ�̱�ұ��8̲��2�G�����j��έ�9��w7���U�X�w�85C��?!I&���o&����L���I#�^�eV�,�^t5�k� �xs˛\��U�8��."���{���ǵ�l4��ݼ�;��r"���G�/~���gJV�N糦��ZZ^k+NN�D��I$�=OE�z����|*�*{��%k���G��>�`9��b;�s���8,�qs��c��.b��q�%�>r��n�R�Ƨ�%�,�A��le#�8���}; 21�0�b�&�QX�f���q�00����$>�h�N�'�a3��A���/�r ��L5�8�GԲ'�����w;��c#�q1���h$*��V^�k��QĽ�b:i������� ����H F#�_���,��]g����Ը����`RUU���� ��"�qD���>-��ϤI�~��3�̴�����NZ[[555�eeek7�m����YF#[��Đ��R��i��L�H�r ��@��J-�Pŝ��_|�-��6y��ks�V�U~��6���w.����(��0A� 4��f��� /���C�xŊ�ٳ����z=6�����rg���mKo��o�/����a�g���?'9%9��P��斓��~���L{�� �H��{AW��B��$����v�1b�&Mb�ر��ر#�0`�`xMBp! )Z�Nmo�mZ�g�}�eذaWTVV ���'99��C5��&�&+5�bT>|8��PUUETT����&���t��~TE%��'������b�tEGG;RSS�S�N�v��ѯ��t����u1@�ޚ"f��͈���r'L�Ph��m��ƌ�74o��`�x�q�P���NRSS�X,�����l&&&��MFFA!-��`��ҷ�n��Yuu�����x<I�LB%����KԒ50�!�3�bz�����ʸKL���;---�l6[��`��������Ȏ;�����egg4��!D�T��jWujyZ��������K����%����p�^���X,6��sV=�ܖ����6�3�X ���4ҿ��A2�&����_k�;d�� �VfOIEND�B`� 0x0x
PHP 8.1.34
Preview: class-facebookwordpresssettingsrecorder.php Size: 25.71 KB
/home2/astrociv/public_html/miyakamalkhan.com/wp-content/plugins/official-facebook-pixel/core/class-facebookwordpresssettingsrecorder.php
<?php
/**
 * Copyright (C) 2017-present, Meta, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * @package FacebookPixelPlugin
 */

namespace FacebookPixelPlugin\Core;

/**
 * Class FacebookWordpressSettingsRecorder
 */
class FacebookWordpressSettingsRecorder {

    /**
     * Registers ajax actions for saving FBE,
     * CAPI integration status, CAPI events filter
     * and CAPI PII caching status.
     */
    public function init() {
        add_action(
            'wp_ajax_save_fbe_settings',
            array( $this, 'save_fbe_settings' )
        );
        add_action(
            'wp_ajax_delete_fbe_settings',
            array(
                $this,
                'delete_fbe_settings',
            )
        );
        add_action(
            'wp_ajax_save_capi_integration_status',
            array(
                $this,
                'save_capi_integration_status',
            )
        );
        add_action(
            'wp_ajax_save_capi_integration_events_filter',
            array(
                $this,
                'save_capi_integration_events_filter',
            )
        );
        add_action(
            'wp_ajax_save_capi_pii_caching_status',
            array(
                $this,
                'save_capi_pii_caching_status',
            )
        );
        add_action(
            'wp_ajax_save_capig',
            array(
                $this,
                'save_capig',
            )
        );
        // FBL4B AJAX actions.
        add_action(
            'wp_ajax_save_fbl4b_settings',
            array( $this, 'save_fbl4b_settings' )
        );
        add_action(
            'wp_ajax_delete_fbl4b_settings',
            array( $this, 'delete_fbl4b_settings' )
        );
        add_action(
            'wp_ajax_fbl4b_validate_token',
            array( $this, 'fbl4b_validate_token' )
        );
        add_action(
            'wp_ajax_fbl4b_fetch_business_id',
            array( $this, 'fbl4b_fetch_business_id' )
        );
        add_action(
            'wp_ajax_fbl4b_fetch_pixels',
            array( $this, 'fbl4b_fetch_pixels' )
        );
        add_action(
            'wp_ajax_fbl4b_clear_pixel',
            array( $this, 'fbl4b_clear_pixel' )
        );
    }

    /**
     * Sends a successful response to the AJAX request.
     *
     * @param string $body A message to be sent in the response body.
     *
     * @return array Response data.
     */
    private function handle_success_request( $body ) {
        $res = array(
            'success' => true,
            'msg'     => $body,
        );
        wp_send_json( $res );
        return $res;
    }

    /**
     * Handles unauthorized request by sending a 403 response
     * with 'success' => false and 'msg' => 'Unauthorized user'.
     *
     * @return array response data
     */
    private function handle_unauthorized_request() {
        $res = array(
            'success' => false,
            'msg'     => 'Unauthorized user',
        );
        wp_send_json( $res, 403 );
        return $res;
    }

    /**
     * Handles invalid request by sending a 400 response
     * with 'success' => false and 'msg' => 'Invalid values'.
     *
     * @return array response data
     */
    private function handle_invalid_request() {
        $res = array(
            'success' => false,
            'msg'     => 'Invalid values',
        );
        wp_send_json( $res, 400 );
        return $res;
    }

    /**
     * Handles saving Facebook Business Extension settings.
     *
     * This function handles saving the Facebook Business Extension settings,
     * such as the pixel ID, access token, and external business ID.
     * It checks if the current user is an administrator, and if not,
     * it will return an unauthorized request response.
     * If the request is valid, it will save the settings to the WordPress
     * options table and return a success response.
     *
     * @return array response data
     */
    public function save_fbe_settings() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return $this->handle_unauthorized_request();
        }
        check_admin_referer(
            FacebookPluginConfig::SAVE_FBE_SETTINGS_ACTION_NAME
        );
        $pixel_id             = sanitize_text_field(
            isset( $_POST['pixelId'] ) ?
            wp_unslash( $_POST['pixelId'] ) : ''
        );
        $access_token         = sanitize_text_field(
            isset( $_POST['accessToken'] ) ?
            wp_unslash( $_POST['accessToken'] ) : ''
        );
        $external_business_id = sanitize_text_field(
            isset( $_POST['externalBusinessId'] ) ?
            wp_unslash( $_POST['externalBusinessId'] ) : ''
        );
        if ( empty( $pixel_id )
                || empty( $access_token )
                || empty( $external_business_id ) ) {
            return $this->handle_invalid_request();
        }
        $settings = array(
            FacebookPluginConfig::PIXEL_ID_KEY             => $pixel_id,
            FacebookPluginConfig::ACCESS_TOKEN_KEY         => $access_token,
            FacebookPluginConfig::EXTERNAL_BUSINESS_ID_KEY =>
            $external_business_id,
            FacebookPluginConfig::IS_FBE_INSTALLED_KEY     => '1',
        );
        \update_option(
            FacebookPluginConfig::SETTINGS_KEY,
            $settings
        );
        \delete_transient(
            FacebookPluginConfig::CONNECTION_INVALID_TRANSIENT
        );
        delete_metadata(
            'user',
            0,
            FacebookPluginConfig::ADMIN_IGNORE_CONNECTION_INVALID_NOTICE,
            '',
            true
        );
        return $this->handle_success_request( $settings );
    }

    /**
     * Handles saving the CAPI integration status.
     *
     * This function handles saving the CAPI integration status, which indicates
     * whether the CAPI integration is enabled or disabled.
     * It checks if the current user is an administrator, and if not,
     * it will return an unauthorized request response.
     * If the request is valid, it will save the status to the WordPress
     * options table and return a success response.
     *
     * @return array response data
     */
    public function save_capi_integration_status() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return $this->handle_unauthorized_request();
        }

        if ( empty( FacebookWordpressOptions::get_active_pixel_id() ) ) {
            \update_option(
                FacebookPluginConfig::CAPI_INTEGRATION_STATUS,
                FacebookPluginConfig::CAPI_INTEGRATION_STATUS_DEFAULT
            );
            return $this->handle_invalid_request();
        }

        check_admin_referer(
            FacebookPluginConfig::SAVE_CAPI_INTEGRATION_STATUS_ACTION_NAME
        );
        $val = sanitize_text_field(
            isset( $_POST['val'] ) ?
            wp_unslash( $_POST['val'] ) : ''
        );

        if ( ! ( '0' === $val || '1' === $val ) ) {
            return $this->handle_invalid_request();
        }

        \update_option( FacebookPluginConfig::CAPI_INTEGRATION_STATUS, $val );
        return $this->handle_success_request( $val );
    }

    /**
     * Handles saving the CAPI integration events filter.
     *
     * This function handles saving the CAPI integration events filter, which
     * determines which events are sent to the CAPI integration. It checks if
     * the current user is an administrator, and if not, it will return an
     * unauthorized request response.
     * If the request is valid, it will save the filter to the WordPress
     * options table and return a success response.
     *
     * @return array response data
     */
    public function save_capi_integration_events_filter() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return $this->handle_unauthorized_request();
        }

        if ( empty( FacebookWordpressOptions::get_active_pixel_id() ) ) {
            \update_option(
                FacebookPluginConfig::CAPI_INTEGRATION_EVENTS_FILTER,
                FacebookPluginConfig::CAPI_INTEGRATION_EVENTS_FILTER_DEFAULT
            );
            return $this->handle_invalid_request();
        }

        check_admin_referer(
            FacebookPluginConfig::SAVE_CAPI_INTEGRATION_EVENTS_FILTER_ACTION_NAME
        );
        $val                    = sanitize_text_field(
            isset( $_POST['val'] ) ?
            wp_unslash( $_POST['val'] ) : ''
        );
        $const_filter_page_view =
        FacebookPluginConfig::CAPI_INTEGRATION_FILTER_PAGE_VIEW_EVENT;
        $const_keep_page_view   =
        FacebookPluginConfig::CAPI_INTEGRATION_KEEP_PAGE_VIEW_EVENT;

        if ( ! ( $val === $const_filter_page_view
        || $val === $const_keep_page_view ) ) {
            return $this->handle_invalid_request();
        }

        $page_view_filtered =
        FacebookWordpressOptions::get_capi_integration_page_view_filtered();

        if ( $val === $const_keep_page_view && $page_view_filtered ) {
            \update_option(
                FacebookPluginConfig::CAPI_INTEGRATION_EVENTS_FILTER,
                FacebookPluginConfig::CAPI_INTEGRATION_EVENTS_FILTER_DEFAULT
            );
        } elseif ( $val === $const_filter_page_view && ! $page_view_filtered ) {
            \update_option(
                FacebookPluginConfig::CAPI_INTEGRATION_EVENTS_FILTER,
                FacebookPluginConfig::CAPI_INTEGRATION_EVENTS_FILTER_DEFAULT .
                    ',PageView'
            );
        }

        return $this->handle_success_request( $val );
    }

    /**
     * Handles saving the CAPI PII caching status.
     *
     * This function handles saving the CAPI PII caching status, which indicates
     * whether the CAPI PII caching is enabled or disabled.
     * It checks if the current user is an administrator, and if not,
     * it will return an unauthorized request response.
     * If the request is valid, it will save the status to the WordPress
     * options table and return a success response.
     *
     * @return array response data
     */
    public function save_capi_pii_caching_status() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return $this->handle_unauthorized_request();
        }

        if ( empty( FacebookWordpressOptions::get_active_pixel_id() ) ) {
            \update_option(
                FacebookPluginConfig::CAPI_PII_CACHING_STATUS,
                FacebookPluginConfig::CAPI_PII_CACHING_STATUS_DEFAULT
            );
            return $this->handle_invalid_request();
        }

        check_admin_referer(
            FacebookPluginConfig::SAVE_CAPI_PII_CACHING_STATUS_ACTION_NAME
        );
        $val = sanitize_text_field(
            isset( $_POST['val'] ) ?
            wp_unslash( $_POST['val'] ) : ''
        );

        if ( ! ( '0' === $val || '1' === $val ) ) {
            return $this->handle_invalid_request();
        }

        \update_option( FacebookPluginConfig::CAPI_PII_CACHING_STATUS, $val );
        return $this->handle_success_request( $val );
    }

    /**
     * Persists the Conversions API Gateway (CAPIG) admin toggle.
     *
     * Accepts '1' (default — fbq optinMetaEnabledCapi command is emitted) or
     * '0' (suppress the command). Admin-only.
     *
     * @return array response data
     */
    public function save_capig() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return $this->handle_unauthorized_request();
        }

        if ( empty( FacebookWordpressOptions::get_active_pixel_id() ) ) {
            \update_option(
                FacebookPluginConfig::CAPIG,
                FacebookPluginConfig::CAPIG_DEFAULT
            );
            return $this->handle_invalid_request();
        }

        check_admin_referer(
            FacebookPluginConfig::SAVE_CAPIG_ACTION_NAME
        );
        $val = sanitize_text_field(
            isset( $_POST['val'] ) ?
            wp_unslash( $_POST['val'] ) : ''
        );

        if ( ! ( '0' === $val || '1' === $val ) ) {
            return $this->handle_invalid_request();
        }

        \update_option( FacebookPluginConfig::CAPIG, $val );
        return $this->handle_success_request( $val );
    }

    /**
     * Deletes the Facebook Business Extension settings.
     *
     * This function deletes the Facebook Business
     * Extension settings from the WordPress
     * options table. It checks if the current user
     * is an administrator, and if not,
     * it will return an unauthorized request response.
     * If the request is valid, it will delete the
     * settings and return a success response.
     *
     * @return array response data
     */
    public function delete_fbe_settings() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return $this->handle_unauthorized_request();
        }
        check_admin_referer(
            FacebookPluginConfig::DELETE_FBE_SETTINGS_ACTION_NAME
        );
        \delete_option( FacebookPluginConfig::SETTINGS_KEY );
        \delete_transient( FacebookPluginConfig::AAM_SETTINGS_KEY );

        return $this->handle_success_request( 'Done' );
    }

    /**
     * Returns the Graph API base URL.
     *
     * @return string The Graph API base URL.
     */
    private function get_graph_api_base_url() {
        return 'https://graph.facebook.com/' . FacebookPluginConfig::GRAPH_API_VERSION;
    }

    /**
     * Saves FBL4B settings. On initial save, encrypts and stores the
     * access token. On partial update (pixel selection), merges with
     * existing settings.
     *
     * @return array Response data.
     */
    public function save_fbl4b_settings() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return $this->handle_unauthorized_request();
        }
        check_admin_referer( 'save_fbl4b_settings' );

        $access_token = '';
        // Read access token from Authorization: Bearer header.
        $auth_header = isset( $_SERVER['HTTP_AUTHORIZATION'] )
            ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_AUTHORIZATION'] ) )
            : '';
        if ( 0 === strpos( $auth_header, 'Bearer ' ) ) {
            $access_token = substr( $auth_header, 7 );
        }
        $pixel_id    = sanitize_text_field(
            isset( $_POST['pixelId'] ) ?
            wp_unslash( $_POST['pixelId'] ) : ''
        );
        $pixel_name  = sanitize_text_field(
            isset( $_POST['pixelName'] ) ?
            wp_unslash( $_POST['pixelName'] ) : ''
        );
        $business_id = sanitize_text_field(
            isset( $_POST['businessId'] ) ?
            wp_unslash( $_POST['businessId'] ) : ''
        );

        if ( empty( $access_token ) ) {
            // Partial update (e.g., pixel selection after initial save).
            $existing = \get_option(
                FacebookPluginConfig::FBL4B_SETTINGS_KEY,
                array()
            );
            if ( empty( $existing ) ) {
                return $this->handle_invalid_request();
            }
            if ( ! empty( $pixel_id ) ) {
                $existing[ FacebookPluginConfig::FBL4B_PIXEL_ID_KEY ] = $pixel_id;
            }
            if ( ! empty( $pixel_name ) ) {
                $existing[ FacebookPluginConfig::FBL4B_PIXEL_NAME_KEY ] =
                    $pixel_name;
            }
            if ( ! empty( $business_id ) ) {
                $existing[ FacebookPluginConfig::FBL4B_BUSINESS_ID_KEY ] =
                    $business_id;
            }
            \update_option(
                FacebookPluginConfig::FBL4B_SETTINGS_KEY,
                $existing
            );
            // If FBL4B now has a pixel, mark MBE as not installed so
            // Connection doesn't fall back to MBE after FBL4B disconnect.
            if ( ! empty( $pixel_id ) ) {
                $mbe_settings = \get_option(
                    FacebookPluginConfig::SETTINGS_KEY,
                    array()
                );
                if ( ! empty( $mbe_settings ) ) {
                    $mbe_settings[ FacebookPluginConfig::IS_FBE_INSTALLED_KEY ] = '0';
                    \update_option(
                        FacebookPluginConfig::SETTINGS_KEY,
                        $mbe_settings
                    );
                }
            }
            \delete_transient(
                FacebookPluginConfig::CONNECTION_INVALID_TRANSIENT
            );
            delete_metadata(
                'user',
                0,
                FacebookPluginConfig::ADMIN_IGNORE_CONNECTION_INVALID_NOTICE,
                '',
                true
            );
            return $this->handle_success_request( $existing );
        }

        // Initial save — encrypt and store the access token.
        $fbl4b_settings = array(
            FacebookPluginConfig::FBL4B_ACCESS_TOKEN_KEY =>
                FacebookWordpressOptions::encrypt_token( $access_token ),
            FacebookPluginConfig::FBL4B_PIXEL_ID_KEY     => $pixel_id,
            FacebookPluginConfig::FBL4B_PIXEL_NAME_KEY   => $pixel_name,
            FacebookPluginConfig::FBL4B_BUSINESS_ID_KEY  => $business_id,
        );
        \update_option(
            FacebookPluginConfig::FBL4B_SETTINGS_KEY,
            $fbl4b_settings
        );
        \delete_transient(
            FacebookPluginConfig::CONNECTION_INVALID_TRANSIENT
        );
        delete_metadata(
            'user',
            0,
            FacebookPluginConfig::ADMIN_IGNORE_CONNECTION_INVALID_NOTICE,
            '',
            true
        );

        return $this->handle_success_request( 'FBL4B settings saved' );
    }

    /**
     * Deletes all FBL4B settings.
     *
     * @return array Response data.
     */
    public function delete_fbl4b_settings() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return $this->handle_unauthorized_request();
        }
        check_admin_referer( 'delete_fbl4b_settings' );

        \delete_option( FacebookPluginConfig::FBL4B_SETTINGS_KEY );
        \delete_transient( FacebookPluginConfig::AAM_SETTINGS_KEY );
        \delete_user_meta(
            get_current_user_id(),
            FacebookPluginConfig::ADMIN_IGNORE_FBL4B_UPGRADE_NOTICE
        );

        return $this->handle_success_request( 'FBL4B settings deleted' );
    }

    /**
     * Clears the stored pixel ID and pixel name from FBL4B settings.
     * Keeps the access token and business ID intact so the user can
     * re-select a different pixel without re-authenticating.
     *
     * @return array Response data.
     */
    public function fbl4b_clear_pixel() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return $this->handle_unauthorized_request();
        }
        check_admin_referer( 'fbl4b_clear_pixel' );

        $existing = \get_option(
            FacebookPluginConfig::FBL4B_SETTINGS_KEY,
            array()
        );
        if ( empty( $existing ) ) {
            return $this->handle_invalid_request();
        }

        unset( $existing[ FacebookPluginConfig::FBL4B_PIXEL_ID_KEY ] );
        unset( $existing[ FacebookPluginConfig::FBL4B_PIXEL_NAME_KEY ] );
        \update_option(
            FacebookPluginConfig::FBL4B_SETTINGS_KEY,
            $existing
        );
        \delete_transient( FacebookPluginConfig::AAM_SETTINGS_KEY );

        return $this->handle_success_request( 'Pixel cleared' );
    }

    /**
     * Proxies a Graph API call to validate the FBL4B access token.
     * Returns whether the token is valid and the client_business_id.
     */
    public function fbl4b_validate_token() {
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( 'Unauthorized', 403 );
            return;
        }
        check_admin_referer( 'fbl4b_validate_token' );

        $access_token = FacebookWordpressOptions::get_fbl4b_access_token();
        if ( empty( $access_token ) ) {
            wp_send_json_success(
                array(
                    'valid'              => false,
                    'client_business_id' => null,
                )
            );
            return;
        }

        $response = wp_remote_get(
            $this->get_graph_api_base_url() . '/me?fields=client_business_id',
            array(
                'headers' => array(
                    'Authorization' => 'Bearer ' . $access_token,
                ),
                'timeout' => 15,
            )
        );

        if ( is_wp_error( $response ) ) {
            wp_send_json_success(
                array(
                    'valid'              => false,
                    'client_business_id' => null,
                )
            );
            return;
        }

        $status_code = wp_remote_retrieve_response_code( $response );
        $body        = json_decode( wp_remote_retrieve_body( $response ), true );

        if ( $status_code >= 400 ) {
            wp_send_json_success(
                array(
                    'valid'              => false,
                    'client_business_id' => null,
                )
            );
            return;
        }

        wp_send_json_success(
            array(
                'valid'              => ! empty( $body['client_business_id'] ),
                'client_business_id' => isset( $body['client_business_id'] )
                    ? $body['client_business_id'] : null,
            )
        );
    }

    /**
     * Proxies a Graph API call to fetch the client_business_id.
     * Token is read server-side from the database.
     */
    public function fbl4b_fetch_business_id() {
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( 'Unauthorized', 403 );
            return;
        }
        check_admin_referer( 'fbl4b_fetch_business_id' );

        $access_token = FacebookWordpressOptions::get_fbl4b_access_token();
        if ( empty( $access_token ) ) {
            wp_send_json_error( 'No access token stored', 400 );
            return;
        }

        $response = wp_remote_get(
            $this->get_graph_api_base_url() . '/me?fields=client_business_id',
            array(
                'headers' => array(
                    'Authorization' => 'Bearer ' . $access_token,
                ),
                'timeout' => 15,
            )
        );

        if ( is_wp_error( $response ) ) {
            wp_send_json_error( $response->get_error_message() );
            return;
        }

        $status_code = wp_remote_retrieve_response_code( $response );
        $body        = json_decode( wp_remote_retrieve_body( $response ), true );

        if ( $status_code >= 400 ) {
            $error_msg = isset( $body['error']['message'] )
                ? $body['error']['message'] : 'Graph API error';
            wp_send_json_error( $error_msg, $status_code );
            return;
        }

        wp_send_json_success( $body );
    }

    /**
     * Proxies Graph API calls to fetch owned + client pixels for a business.
     * Deduplicates by pixel ID, owned pixels first.
     */
    public function fbl4b_fetch_pixels() {
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( 'Unauthorized', 403 );
            return;
        }
        check_admin_referer( 'fbl4b_fetch_pixels' );

        $access_token = FacebookWordpressOptions::get_fbl4b_access_token();
        $business_id  = sanitize_text_field(
            isset( $_POST['businessId'] ) ?
            wp_unslash( $_POST['businessId'] ) : ''
        );

        if ( empty( $access_token ) || empty( $business_id ) ) {
            wp_send_json_error( 'Missing parameters', 400 );
            return;
        }

        $base_url = $this->get_graph_api_base_url();
        $headers  = array( 'Authorization' => 'Bearer ' . $access_token );

        $owned_url  = $base_url . '/' . $business_id
            . '/owned_pixels?fields=id,name&limit=100';
        $client_url = $base_url . '/' . $business_id
            . '/client_pixels?fields=id,name&limit=100';

        $owned_response  = wp_remote_get(
            $owned_url,
            array(
            'headers' => $headers,
            'timeout' => 15,
            )
        );
        $client_response = wp_remote_get(
            $client_url,
            array(
            'headers' => $headers,
            'timeout' => 15,
            )
        );

        $pixels   = array();
        $seen_ids = array();

        if ( ! is_wp_error( $owned_response )
            && 200 === wp_remote_retrieve_response_code( $owned_response ) ) {
            $owned_data = json_decode(
                wp_remote_retrieve_body( $owned_response ),
                true
            );
            if ( isset( $owned_data['data'] ) ) {
                foreach ( $owned_data['data'] as $pixel ) {
                    $pixels[]   = $pixel;
                    $seen_ids[] = $pixel['id'];
                }
            }
        }

        if ( ! is_wp_error( $client_response )
            && 200 === wp_remote_retrieve_response_code( $client_response ) ) {
            $client_data = json_decode(
                wp_remote_retrieve_body( $client_response ),
                true
            );
            if ( isset( $client_data['data'] ) ) {
                foreach ( $client_data['data'] as $pixel ) {
                    if ( ! in_array( $pixel['id'], $seen_ids, true ) ) {
                        $pixels[] = $pixel;
                    }
                }
            }
        }

        if ( empty( $pixels ) ) {
            wp_send_json_error(
                array(
                    'message' => 'No pixels found for this business.',
                    'code'    => 'no_pixels',
                )
            );
            return;
        }

        wp_send_json_success( array( 'data' => $pixels ) );
    }
}

Directory Contents

Dirs: 0 × Files: 20
Name Size Perms Modified Actions
3.24 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
1.69 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
1.26 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
3.26 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
14.29 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
7.02 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
13.72 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
7.19 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
10.52 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
5.24 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
17.90 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
30.96 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
7.65 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
47.14 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
25.71 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
5.43 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
19.97 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
6.20 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
21.53 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
3.86 KB lrw-r--r-- 2026-07-25 19:17:51
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).
© 2026 0xNothings — Secure File Manager. All rights reserved. Built with ❤️ & Tailwind x Dark UI