Plugin Directory

Changeset 3036964


Ignore:
Timestamp:
02/16/2024 07:21:44 PM (2 years ago)
Author:
brandondove
Message:

PHP 8 compatibility, WordPress 6.4 compatibility, Improves documentation, Updates site scan allow line for robots.txt, Adds new Site Scan setting for modifying the robots.txt file, Changes from automatic robots.txt file writing to opt-in.

Location:
uk-cookie-consent/trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • uk-cookie-consent/trunk/changelog.txt

    r3009736 r3036964  
     1= 3.2 =
     2* New: There is a new setting on the site scan page to add a specific allow line to an existing or virtual (provided by WordPress core) robots.txt file.
     3* Update: Changed the text of the "allow" line in the robots.txt to accurately reflect the new scraper's name.
     4* Removed: The plugin will no longer automatically save the "allow" line to the robots.txt file when regenerating rewrite rules.
     5
    16= 3.1.1 =
    27* Update: Updates the embed script on the consent banner to include some additional information needed for European privacy framework.
  • uk-cookie-consent/trunk/includes/controllers/class-account-api-controller.php

    r2567699 r3036964  
    11<?php
     2/**
     3 * Account API Controller
     4 *
     5 * @package UKCookieConsent
     6 */
    27
    38namespace termly;
    49
     10/**
     11 * Account API Controller class.
     12 */
    513class Account_API_Controller {
    614
     15    /**
     16     * Hooks into WordPress for this class.
     17     *
     18     * @return void
     19     */
    720    public static function hooks() {
    821
     
    1629    }
    1730
     31    /**
     32     * Adds the rewrite rule for the account status endpoint.
     33     *
     34     * @return void
     35     */
    1836    public static function add_rewrite_rule() {
    1937
     
    3048    }
    3149
     50    /**
     51     * Maybe schedule the cron job.
     52     *
     53     * @return void
     54     */
    3255    public static function maybe_schedule_cron() {
    3356
     
    4063    }
    4164
     65    /**
     66     * Update the account status.
     67     *
     68     * @return WP_REST_Response
     69     */
    4270    public static function update_account_status() {
    4371
     
    76104        } else {
    77105
    78             error_log( print_r( [ $response ], true ) );
    79             wp_send_json_error();
     106            return rest_ensure_response( [ 'message' => __( 'Failed to update account status.', 'uk-cookie-consent' ) ] );
    80107
    81108        }
    82109
    83         wp_send_json_success();
     110        return rest_ensure_response( [ 'message' => __( 'Account status updated.', 'uk-cookie-consent' ) ] );
    84111
    85112    }
    86113
     114    /**
     115     * Check if the plugin is using a free account to communicate with the API.
     116     *
     117     * @return bool
     118     */
    87119    public static function is_free() {
    88120
  • uk-cookie-consent/trunk/includes/controllers/class-banner-settings-controller.php

    r3006375 r3036964  
    172172            '/consent-toggle',
    173173            [
    174                 'methods'  => 'POST',
    175                 'callback' => [ __CLASS__, 'handle_consent_toggle' ],
     174                'methods'             => 'POST',
     175                'callback'            => [ __CLASS__, 'handle_consent_toggle' ],
     176                'permission_callback' => '__return_true',
    176177            ]
    177178        );
  • uk-cookie-consent/trunk/includes/controllers/class-edit-cookie.php

    r3006375 r3036964  
    88namespace termly;
    99
    10 // If the Termly API Controller has not been included
     10// If the Termly API Controller has not been included.
    1111if ( ! class_exists( 'Termly_API_Controller' ) ) {
    1212    require_once TERMLY_CONTROLLERS . 'class-termly-api-controller.php';
     
    1818class Edit_Cookie {
    1919
     20    /**
     21     * The name prefix for ids.
     22     *
     23     * @var string
     24     */
    2025    public static $name_prefix = 'termly-edit-cookie-';
    2126
    2227    /**
    23      * hooks    Hooks into WordPress for this class
     28     * Hooks into WordPress for this class.
    2429     *
    2530     * @return void
     
    3338
    3439    /**
    35      * edit_page    Adds submenu page for edit page with no parent
     40     * Adds submenu page for edit page with no parent.
    3641     *
    3742     * @return void
     
    4045
    4146        add_submenu_page(
    42             null,
     47            'admin.php',
    4348            __( 'Edit Cookie', 'uk-cookie-consent' ),
    4449            '',
     
    5156
    5257    /**
    53      * edit_page_view    The view for the edit page.
     58     * The view for the edit page.
    5459     * Also triggers edit/add functionality if action is set
    5560     *
     
    5863    public static function edit_page_view() {
    5964
    60         // Handle editing or adding a cookie if there is an action set in the request
     65        // Handle editing or adding a cookie if there is an action set in the request.
    6166        if ( isset( $_REQUEST['action'] ) ) {
    6267            $status = self::handle_crud();
     
    6469
    6570        // Whether this is editing or adding new
    66         // Cookie ID is only set if we are editing
     71        // Cookie ID is only set if we are editing.
    6772        $editing = isset( $_GET['cookie_id'] );
    6873
    69         // Name prefix for ids
     74        // Name prefix for ids.
    7075        $name_prefix = self::$name_prefix;
    7176
    7277        // If a cookie has been added, an additional
    73         // array value is added which is the cookie id
     78        // array value is added which is the cookie id.
    7479        if ( isset( $status ) && 3 === count( $status ) ) {
    7580            $cookie_id = $status[2];
     
    7782        }
    7883
    79         // By default, cookie is set to false
     84        // By default, cookie is set to false.
    8085        $cookie = false;
    8186
     
    117122
    118123    /**
    119      * handle_crud    Handle editing and adding a cookie
     124     * Handle editing and adding a cookie
    120125     *
    121126     * @return array [0] is success/error [1] is message [2] (optional) is cookie id
     
    123128    public static function handle_crud() {
    124129
    125         if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'termly_cookie_nonce' ) ) {
     130        if ( ! isset( $_REQUEST['_wpnonce'], $_REQUEST['action'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'termly_cookie_nonce' ) ) {
    126131            die();
    127132        }
    128133
    129         $action = sanitize_text_field( $_REQUEST['action'] );
    130 
    131         // Check for required fields
    132         $required_fields = [
    133             'name', 'category', 'domain',
    134         ];
     134        $action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) );
     135
     136        // Check for required fields.
     137        $required_fields = [ 'name', 'category', 'domain' ];
    135138
    136139        foreach ( $required_fields as $field ) {
     
    143146        }
    144147
    145         // Delete the transient which stores cookie list
     148        // Delete the transient which stores cookie list.
    146149        delete_transient( 'termly-site-scan-results' );
    147150
    148         // Arguments for edit/add cookie
    149         $arg_keys = [
    150             'name', 'category', 'expire', 'tracker_type',
    151             'country', 'domain', 'service', 'service_policy_link',
    152             'source', 'value', 'en_us',
    153         ];
     151        // Arguments for edit/add cookie.
     152        $arg_keys = [ 'name', 'category', 'expire', 'tracker_type', 'country', 'domain', 'service', 'service_policy_link', 'source', 'value', 'en_us' ];
    154153
    155154        $args = [];
    156155
    157         // Add arguments of they are set
     156        // Add arguments of they are set.
    158157        foreach ( $arg_keys as $key ) {
    159158            if ( ! isset( $_REQUEST[ $key ] ) ) {
    160159                continue;
    161160            }
    162             $args[ $key ] = sanitize_text_field( $_REQUEST[ $key ] );
    163         }
    164 
    165         // Unslash the args
    166         $args = wp_unslash( $args );
    167 
    168         // Edit a cookie
     161            $args[ $key ] = sanitize_text_field( wp_unslash( $_REQUEST[ $key ] ) );
     162        }
     163
     164        // Edit a cookie.
    169165        if ( 'edit' === $action ) {
    170166            $status = self::edit_cookie( $args );
     
    172168        }
    173169
    174         // Add a cookie
     170        // Add a cookie.
    175171        if ( 'add' === $action ) {
    176172            $status = self::add_cookie( $args );
     
    183179
    184180    /**
    185      * edit_cookie
    186      *
    187      * @param  array $args Post arguments
     181     * Store the cookie and return the status.
     182     *
     183     * @param  array $args Post arguments All posted arguments.
    188184     *
    189185     * @return array [0] is success/error [1] is message [2] is cookie id
     
    191187    public static function edit_cookie( $args ) {
    192188
    193         // These fields cannot be edited
    194         $non_editable_fields = [ 'name', 'expire', 'tracker_type', 'domain', ];
    195 
    196         // Loop through and remove non editable fields if they are set
     189        // These fields cannot be edited.
     190        $non_editable_fields = [ 'name', 'expire', 'tracker_type', 'domain' ];
     191
     192        // Loop through and remove non editable fields if they are set.
    197193        foreach ( $non_editable_fields as $field ) {
    198194            if ( ! isset( $args[ $field ] ) ) {
     
    202198        }
    203199
    204         // Get the cookie ID
     200        if ( ! isset( $_REQUEST['cookie_id'] ) ) {
     201            return [
     202                'error',
     203                __( 'Cookie not found', 'uk-cookie-consent' ),
     204            ];
     205        }
     206
     207        // Get the cookie ID.
    205208        $cookie_id = intval( $_REQUEST['cookie_id'] );
    206209
    207         // PUT request to the API
     210        // PUT request to the API.
    208211        $response = Termly_API_Controller::call( 'PUT', 'cookies/' . $cookie_id, $args );
    209212
    210213        if ( 200 === wp_remote_retrieve_response_code( $response ) && ! is_wp_error( $response ) ) {
    211             // Return success
     214
     215            // Return success.
    212216            return [
    213217                'success',
     
    215219                $cookie_id,
    216220            ];
    217         }
    218 
    219         // Return failure
     221
     222        }
     223
     224        // Return failure.
    220225        return [
    221226            'error',
     
    250255
    251256            // If not adding another, set cookie id to go to edit screen.
    252             if ( 'add_another' !== $_REQUEST['submit'] ) {
     257            if ( isset( $_REQUEST['submit'] ) && 'add_another' !== sanitize_text_field( wp_unslash( $_REQUEST['submit'] ) ) ) {
    253258                $success[] = intval( $cookie_id );
    254259            }
     
    268273     * Highlight the "Cookie Management" submenu page when on the edit cookie page
    269274     *
    270      * @param  string $parent
     275     * @param  string $parent_page The slug of the parent page.
    271276     *
    272277     * @return string
    273278     */
    274     public static function highlight( $parent ) {
     279    public static function highlight( $parent_page ) {
     280
    275281        global $plugin_page;
    276282        if ( 'termly-edit-cookie' === $plugin_page ) {
    277             $plugin_page = 'cookie-management';
    278         }
    279         return $parent;
     283
     284            $plugin_page = 'cookie-management'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
     285
     286        }
     287        return $parent_page;
     288
    280289    }
    281290
  • uk-cookie-consent/trunk/includes/controllers/class-robots-txt.php

    r2570327 r3036964  
    2121
    2222        \add_filter( 'robots_txt', [ __CLASS__, 'virtual' ], 10, 1 );
    23         \add_action( 'generate_rewrite_rules', [ __CLASS__, 'check_file' ] );
    2423
    2524    }
     
    3130     * @return string $file
    3231     */
    33     public static function virtual( $file ) {
     32    public static function virtual( $file = '' ) {
    3433
    35         return 'User-agent: Scrapy
     34        return '# Termly scanner
     35User-agent: TermlyBot
    3636Allow: /
    3737
     
    4141
    4242    /**
    43      * Check for an actual robots.txt file.
    44      * Fired after rewrite rules are flushed.
    45      * We needed a place where users could trigger this.
    46      *
    47      * @param WP_Rewrite $rules The WP_Rewrite object.
     43     * Check for an actual robots.txt file and add the allow line.
    4844     *
    4945     * @return void
    5046     */
    51     public static function check_file( $rules ) {
     47    public static function add_allow_line() {
    5248
    5349        // Include filesystem functionality.
     
    8783        // Check to see if the robots file already has the rule.
    8884        $robots_content = $wp_filesystem->get_contents( $robots_path );
    89         $robots_rule = 'User-agent: Scrapy
     85        $scrapy_rule    = '/User-agent: Scrapy\nAllow: \//';
     86        $robots_rule    = '# Termly scanner
     87User-agent: TermlyBot
    9088Allow: /';
    9189
     90        // Remove the Scrapy rule if it exists.
     91        if ( 1 === preg_match( $scrapy_rule, $robots_content ) ) {
     92            $robots_content = preg_replace( $scrapy_rule, '', $robots_content );
     93        }
     94
     95        // Check if the termly bot rule already exists.
    9296        if ( false !== strpos( $robots_content, $robots_rule ) ) {
    9397            return;
     
    103107    }
    104108
     109    /**
     110     * Check for an actual robots.txt file with the allo wline and remove it.
     111     *
     112     * @return void
     113     */
     114    public static function remove_allow_line() {
     115
     116        // Include filesystem functionality.
     117        require_once ABSPATH . 'wp-admin/includes/file.php';
     118
     119        // Check that the robots file exists.
     120        $robots_path = ABSPATH . '/robots.txt';
     121        if ( ! file_exists( $robots_path ) || ! is_file( $robots_path ) ) {
     122            return;
     123        }
     124
     125        // Initialize the filesystem API.
     126        global $wp_filesystem;
     127
     128        $url = \wp_nonce_url(
     129            \add_query_arg(
     130                [
     131                    'page' => 'termly',
     132                ],
     133                \admin_url( 'admin.php' )
     134            ),
     135            'termly-robots-nonce'
     136        );
     137
     138        // Create and test creds.
     139        $creds = \request_filesystem_credentials( $url, '', false, false, null );
     140        if ( false === $creds ) {
     141            return;
     142        }
     143
     144        if ( ! \WP_Filesystem( $creds ) ) {
     145            // Prompt user to enter credentials.
     146            \request_filesystem_credentials( $url, '', true, false, null );
     147            return;
     148        }
     149
     150        // Check to see if the robots file already has the rule.
     151        $robots_content = $wp_filesystem->get_contents( $robots_path );
     152        $rules          = '/(User-agent: Scrapy\nAllow: \/|# Termly scanner\nUser-agent: TermlyBot\nAllow: \/)/';
     153
     154        // Remove the Scrapy rule if it exists.
     155        if ( 1 === preg_match( $rules, $robots_content ) ) {
     156            $robots_content = preg_replace( $rules, '', $robots_content );
     157        }
     158
     159        // Prepend the rule. Robots file is read top to bottom.
     160        $wp_filesystem->put_contents( $robots_path, $robots_content, FS_CHMOD_FILE );
     161
     162    }
     163
    105164}
    106165
  • uk-cookie-consent/trunk/includes/controllers/class-site-scan-controller.php

    r2567699 r3036964  
    1313class Site_Scan_Controller extends Menu_Controller {
    1414
     15    /**
     16     * The last request made to the API.
     17     *
     18     * @var \WP_Error
     19     */
    1520    private static $last_request = null;
    1621
     22    /**
     23     * Register the hooks for the class.
     24     */
    1725    public static function hooks() {
    1826
     
    3543    }
    3644
     45    /**
     46     * Register the settings and fields for the Site Scan.
     47     */
    3748    public static function register_settings() {
    3849
    3950        // Register the API Key Setting.
    40         register_setting( 'termly_site_scan', 'termly_site_scan', [ 'sanitize_callback' => [ new Site_Scan_Model, 'sanitize_site_scan' ] ] );
     51        register_setting(
     52            'termly_site_scan',
     53            'termly_site_scan',
     54            [
     55                'sanitize_callback' => [ new Site_Scan_Model, 'sanitize_site_scan' ],
     56            ]
     57        );
    4158
    4259        // Add a section to the Settings API.
     
    5673        );
    5774
    58     }
    59 
     75        add_settings_field(
     76            'termly_robots_txt',
     77            __( 'Robots.txt', 'uk-cookie-consent' ),
     78            [ __CLASS__, 'robots_txt_field' ],
     79            'termly_site_scan',
     80            'termly_site_scan_section'
     81        );
     82
     83    }
     84
     85    /**
     86     * Output the section header.
     87     *
     88     * @param array $args The arguments for the section.
     89     */
    6090    public static function section_header( $args = [] ) {
    6191        // Don't output a heading.
    6292    }
    6393
    64     public static function site_scan_field( $args = [] ) {
     94    /**
     95     * Output the Site Scan field.
     96     */
     97    public static function site_scan_field() {
    6598
    6699        $feature_set_cache_key = 'termly-feature-set';
     
    107140    }
    108141
     142    /**
     143     * Output the Robots.txt field.
     144     */
     145    public static function robots_txt_field() {
     146
     147        $site_scan = get_option(
     148            'termly_site_scan',
     149            [
     150                'robots_txt' => 0,
     151            ]
     152        );
     153        $site_scan = wp_parse_args( $site_scan, [ 'robots_txt' => 0 ] );
     154        ?>
     155        <p><label class="checkbox-container" for="termly-site-scan-robots-txt">
     156            <input type="checkbox" name="termly_site_scan[robots_txt]" id="termly-site-scan-robots-txt" value="1" <?php checked( 1, $site_scan['robots_txt'] ); ?>>
     157            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
     158                <path class="border" d="M3.5 6C3.5 4.61929 4.61929 3.5 6 3.5H18C19.3807 3.5 20.5 4.61929 20.5 6V18C20.5 19.3807 19.3807 20.5 18 20.5H6C4.61929 20.5 3.5 19.3807 3.5 18V6Z" fill="white" stroke="#CED4DA"/>
     159                <path class="checkmark" fill-rule="evenodd" clip-rule="evenodd" d="M15.4937 9.25628C15.8383 8.91457 16.397 8.91457 16.7416 9.25628C17.0861 9.59799 17.0861 10.152 16.7416 10.4937L11.4474 15.7437C11.1029 16.0854 10.5442 16.0854 10.1996 15.7437L7.25844 12.8271C6.91385 12.4853 6.91385 11.9313 7.25844 11.5896C7.60302 11.2479 8.16169 11.2479 8.50627 11.5896L10.8235 13.8876L15.4937 9.25628Z" fill="#4672FF"/>
     160            </svg>
     161            <span><?php esc_html_e( 'Add Termly Scanner to robots.txt Allow list', 'uk-cookie-consent' ); ?></span>
     162        </label></p>
     163        <?php
     164    }
     165
     166    /**
     167     * Add the Site Scan submenu item.
     168     */
    109169    public static function menu() {
    110170
     
    120180    }
    121181
     182    /**
     183     * Output the Site Scan page.
     184     */
    122185    public static function menu_page() {
    123186
     
    126189    }
    127190
     191    /**
     192     * Make a call out to the Termly API to initiate a new scan.
     193     */
    128194    public static function handle_new_scan_request() {
    129195
     
    133199    }
    134200
     201    /**
     202     * Output the new scan notice.
     203     */
    135204    public static function new_scan_notice() {
    136205
     
    168237    }
    169238
     239    /**
     240     * Output the update notice.
     241     */
    170242    public static function maybe_update_notice() {
    171243
     
    175247            'termly_site_scan',
    176248            [
    177                 'enabled'   => 0,
     249                'enabled' => 0,
    178250            ]
    179251        );
     
    207279    }
    208280
     281    /**
     282     * Get the last scanned date.
     283     *
     284     * @return string
     285     */
    209286    public static function get_last_scanned() {
    210287
  • uk-cookie-consent/trunk/includes/models/class-site-scan-model.php

    r2567699 r3036964  
    1313class Site_Scan_Model {
    1414
     15    /**
     16     * Sanitize the site scan settings.
     17     *
     18     * @param array $value The value to sanitize.
     19     *
     20     * @return array
     21     */
    1522    public static function sanitize_site_scan( $value ) {
    1623
     
    2229            $value,
    2330            [
    24                 'enabled'   => 0,
    25                 'frequency' => 'trimonthly',
     31                'enabled'    => 0,
     32                'frequency'  => 'trimonthly',
     33                'robots_txt' => 0,
    2634            ]
    2735        );
     
    3139            if ( '' !== $value ) {
    3240
    33                 $response = Termly_API_Controller::call( 'PUT', 'website/scan_settings', [ 'scan_enabled' => boolval( $value['enabled'] ), 'scan_period' => $value['frequency'], ] );
     41                // Save the settings to the API.
     42                $response = Termly_API_Controller::call(
     43                    'PUT',
     44                    'website/scan_settings',
     45                    [
     46                        'scan_enabled' => boolval( $value['enabled'] ),
     47                        'scan_period'  => $value['frequency'],
     48                    ]
     49                );
    3450                if ( 200 === wp_remote_retrieve_response_code( $response ) && ! is_wp_error( $response ) ) {
     51
     52                    // If the robots.txt setting is enabled and the robots.txt file exists.
     53                    if ( 1 === intval( $value['robots_txt'] ) ) {
     54
     55                        \termly\Robots_Txt::add_allow_line();
     56
     57                    } else {
     58
     59                        \termly\Robots_Txt::remove_allow_line();
     60
     61                    }
    3562
    3663                    $type = 'updated';
  • uk-cookie-consent/trunk/includes/models/class-termly-api-model.php

    r2567699 r3036964  
    8383
    8484            }
    85 
    86         } else {
    87 
    88             error_log( print_r( [ $response ], true ) );
    8985
    9086        }
  • uk-cookie-consent/trunk/readme.txt

    r3009736 r3036964  
    55Requires PHP: 5.6
    66Tested up to: 6.2
    7 Stable tag: 3.1.1
     7Stable tag: 3.2
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    126126== Changelog ==
    127127
     128= 3.2 =
     129* New: There is a new setting on the site scan page to add a specific allow line to an existing or virtual (provided by WordPress core) robots.txt file.
     130* Update: Changed the text of the "allow" line in the robots.txt to accurately reflect the new scraper's name.
     131* Removed: The plugin will no longer automatically save the "allow" line to the robots.txt file when regenerating rewrite rules.
     132
    128133= 3.1.1 =
    129134* Update: Updates the embed script on the consent banner to include some additional information needed for European privacy framework.
  • uk-cookie-consent/trunk/uk-cookie-consent.php

    r3009737 r3036964  
    44 * Plugin URI: https://termly.io/products/
    55 * Description: Our easy to use cookie consent plugin can assist in your GDPR and ePrivacy Directive compliance efforts.
    6  * Version: 3.1.1
     6 * Version: 3.2
    77 * Author: Termly
    88 * Author URI: https://termly.io/
     
    7878define( 'TERMLY_BASENAME', plugin_basename( __FILE__ ) );
    7979define( 'TERMLY_API_BASE', 'https://app.termly.io/api' );
    80 define( 'TERMLY_VERSION', '3.1' );
     80define( 'TERMLY_VERSION', '3.2' );
    8181define( 'TERMLY_URL', plugin_dir_url( __FILE__ ) );
    8282define( 'TERMLY_PATH', plugin_dir_path( __FILE__ ) );
Note: See TracChangeset for help on using the changeset viewer.