Plugin Directory

Changeset 2529282


Ignore:
Timestamp:
05/10/2021 07:28:25 PM (4 years ago)
Author:
lukasznowicki
Message:

Lot of fixes thanks to WP team

Location:
postpage-specific-custom-css/tags/0.2.2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • postpage-specific-custom-css/tags/0.2.2/post-page-specific-custom-css.php

    r2528049 r2529282  
    1010 * Requires PHP: 7.0
    1111 * Tested up to: 5.7
    12  * Text Domain: phylaxppsccss
     12 * Text Domain: postpage-specific-custom-css
    1313 * Domain Path: /languages
    1414 */
     
    1616namespace Phylax\WPPlugin\PPCustomCSS;
    1717
    18 if ( ! defined( 'ABSPATH' ) ) {
    19     die;
    20 } # famous cheatin', huh?
    21 
    22 const TEXT_DOMAIN = 'phylaxppsccss';
     18use const DOING_AUTOSAVE;
     19
     20defined( 'ABSPATH' ) or exit;
    2321
    2422class Plugin {
    2523
    26     public $menu_slug = 'post-page-custom-css';
    27     public $menu_parent_slug = 'options-general.php';
    28     public $option_group = 'ppcs_settings_group';
    29     public $option_name = 'ppcs_settings_name';
     24    const USER_META_STRING = 'ppscc_birthday_message';
     25    const MENU_SLUG = 'post-page-custom-css';
     26    const PARENT_MENU_SLUG = 'options-general.php';
     27    const OPTION_GROUP = 'ppcs_settings_group';
     28    const OPTION_NAME = 'ppcs_settings_name';
     29
     30    const POST_META_CSS = '_phylax_ppsccss_css';
     31    const POST_META_SINGLE = '_phylax_ppsccss_single_only';
     32    const POST_META_VALID = '_phylax_ppsccss_valid';
     33
     34    const NONCE_HIDE_BIRTHDAY = 'ppscc_hide_birthday';
     35    const NONCE_VALIDATE_CSS = 'ppscc_validate_css';
    3036
    3137    private $isBirthday = false;
    3238    private $isDayBefore = false;
    33     private $isWeekAfter = false;
    34 
    35     private $newPlugin = true;
    36     private $flagUrl;
    37     private $myTransient;
     39    private $isDayAfter = false;
     40
     41    private $flagsURLs;
    3842
    3943    public function __construct() {
     
    4549            $this,
    4650            'the_content',
    47         ] );
     51        ], 999 );
    4852        if ( is_admin() ) {
    49             add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), [
    50                 $this,
    51                 'page_settings_link_filter',
    52             ] );
    53             add_action( 'add_meta_boxes', [
    54                 $this,
    55                 'add_meta_boxes',
    56             ] );
    57             add_action( 'save_post', [
    58                 $this,
    59                 'save_post',
    60             ] );
    61             add_action( 'admin_menu', [
    62                 $this,
    63                 'add_options_page',
    64             ] );
    65             add_action( 'admin_init', [
    66                 $this,
    67                 'register_settings',
    68             ] );
    69             add_action( 'admin_init', [
    70                 $this,
    71                 'pluginInfo'
    72             ] );
    73             add_action( 'admin_enqueue_scripts', [
    74                 $this,
    75                 'admin_enqueue_scripts',
    76             ] );
    77         }
    78         add_action( 'admin_notices', [ $this, 'adminNotices' ] );
    79 
    80         add_action( 'after_setup_theme', function () {
    81             if ( is_admin() ) {
    82                 $current_user      = wp_get_current_user();
    83                 $this->myTransient = 'ppsc_lastview_' . $current_user->ID . '_' . date( 'Ymd' );
    84                 $dayBefore         = 'ppsc_lastview_' . $current_user->ID . '_' . date( 'Ymd', time() - DAY_IN_SECONDS );
    85                 delete_transient( $dayBefore );
    86             }
    87             if ( isset( $_GET['ppscTransient'] ) && ( $_GET['ppscTransient'] === 'true' ) ) {
    88                 delete_transient( $this->myTransient );
    89             }
    90         } );
    91 
    92         $today   = date( 'md' );
    93         $birtday = '0502';
    94         if ( $birtday === $today ) {
    95             $this->isBirthday = true;
    96         }
    97         $daybefore = '0501';
    98         if ( $daybefore === $today ) {
    99             $this->isDayBefore = true;
    100         }
    101         $today   = (int) $today;
    102         $daynext = (int) '0503';
    103         $daylast = (int) '0510';
    104         if ( ( $today >= $daynext ) && ( $today <= $daylast ) ) {
    105             $this->isWeekAfter = true;
     53            $this->startInAdmin();
     54        }
     55    }
     56
     57    public function startInAdmin() {
     58        add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), [ $this, 'page_settings_link_filter' ] );
     59        add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );
     60        add_action( 'save_post', [ $this, 'save_post' ] );
     61        add_action( 'admin_menu', [ $this, 'add_options_page' ] );
     62        add_action( 'admin_init', [ $this, 'register_settings' ] );
     63        add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
     64        $today = date( 'md' );
     65        switch ( $today ) {
     66            case '0501':
     67                $this->isDayBefore = true;
     68                break;
     69            case '0502':
     70                $this->isBirthday = true;
     71                break;
     72            case '0503':
     73                $this->isDayAfter = true;
     74                break;
     75        }
     76        if ( $this->isDayBefore || $this->isBirthday || $this->isDayAfter ) {
     77            add_action( 'admin_notices', [ $this, 'adminNotices' ] );
    10678        }
    10779    }
     
    11183    }
    11284
    113     public function admin_enqueue_scripts( $hook ) {
     85    public function admin_enqueue_scripts() {
    11486        $screen = get_current_screen();
    11587        if ( false === is_a( $screen, 'WP_Screen' ) ) {
     
    129101            return;
    130102        }
    131         $settings = (array) get_option( $this->option_name );
     103        $settings = (array) get_option( self::OPTION_NAME );
    132104        $value    = (int) ( $settings[ $field ] ?? 0 );
    133105        if ( 1 === $value ) {
     
    142114
    143115    public function register_settings() {
    144         register_setting( $this->option_group, $this->option_name );
    145         add_settings_section( 'plugin-behavior', __( 'Options', TEXT_DOMAIN ), [
     116        register_setting( self::OPTION_GROUP, self::OPTION_NAME );
     117        add_settings_section( 'plugin-behavior', __( 'Options', 'postpage-specific-custom-css' ), [
    146118            $this,
    147119            'section_plugin_behavior',
    148         ], $this->menu_slug );
    149         add_settings_section( 'default-values', __( 'Default values', TEXT_DOMAIN ), [
     120        ], self::MENU_SLUG );
     121        add_settings_field( 'control-user', __( 'User control', 'postpage-specific-custom-css' ), [
     122            $this,
     123            'control_user_editor'
     124        ], self::MENU_SLUG, 'plugin-behavior' );
     125        add_settings_section( 'default-values', __( 'Default values', 'postpage-specific-custom-css' ), [
    150126            $this,
    151127            'section_default_values',
    152         ], $this->menu_slug );
    153         add_settings_field( 'default_post_css', __( 'Default stylesheet for new posts', TEXT_DOMAIN ), [
     128        ], self::MENU_SLUG );
     129        add_settings_field( 'default_post_css', __( 'Default stylesheet for new posts', 'postpage-specific-custom-css' ), [
    154130            $this,
    155131            'default_post_css',
    156         ], $this->menu_slug, 'default-values' );
    157         add_settings_field( 'default_page_css', __( 'Default stylesheet for new pages', TEXT_DOMAIN ), [
     132        ], self::MENU_SLUG, 'default-values' );
     133        add_settings_field( 'default_page_css', __( 'Default stylesheet for new pages', 'postpage-specific-custom-css' ), [
    158134            $this,
    159135            'default_page_css',
    160         ], $this->menu_slug, 'default-values' );
    161         add_settings_field( 'enable_highlighting_in', __( 'Code highlight', TEXT_DOMAIN ), [
     136        ], self::MENU_SLUG, 'default-values' );
     137        add_settings_field( 'enable_highlighting_in', __( 'Code highlight', 'postpage-specific-custom-css' ), [
    162138            $this,
    163139            'enable_highlighting_in',
    164         ], $this->menu_slug, 'plugin-behavior' );
    165         add_settings_field( 'bigger_textarea', __( 'Bigger input field', TEXT_DOMAIN ), [
     140        ], self::MENU_SLUG, 'plugin-behavior' );
     141        add_settings_field( 'bigger_textarea', __( 'Bigger input field', 'postpage-specific-custom-css' ), [
    166142            $this,
    167143            'bigger_textarea',
    168         ], $this->menu_slug, 'plugin-behavior' );
     144        ], self::MENU_SLUG, 'plugin-behavior' );
    169145    }
    170146
    171147    public function bigger_textarea() {
    172         $settings = (array) get_option( $this->option_name );
     148        $settings = (array) get_option( self::OPTION_NAME );
    173149        $field    = 'bigger_textarea';
    174150        $value    = (int) ( $settings[ $field ] ?? 0 );
     
    176152        <fieldset>
    177153            <legend class="screen-reader-text">
    178                 <span><?php echo __( 'Make input boxes bigger', TEXT_DOMAIN ); ?></span>
     154                <span><?php echo __( 'Make input boxes bigger', 'postpage-specific-custom-css' ); ?></span>
    179155            </legend>
    180             <input type="hidden" name="<?php echo $this->option_name; ?>[<?php echo $field; ?>]" value="0">
     156            <input type="hidden" name="<?php echo self::OPTION_NAME; ?>[<?php echo $field; ?>]" value="0">
    181157            <label for="item_<?php echo $field; ?>">
    182158                <input id="item_<?php echo $field; ?>" type="checkbox"
    183                        name="<?php echo $this->option_name; ?>[<?php echo $field; ?>]"
    184                        value="1"<?php echo( ( $value === 1 ) ? ' checked="checked"' : '' ); ?>> <?php echo __( 'Make input boxes on Posts and Pages bigger', TEXT_DOMAIN ); ?>
     159                       name="<?php echo self::OPTION_NAME; ?>[<?php echo $field; ?>]"
     160                       value="1"<?php echo( ( $value === 1 ) ? ' checked="checked"' : '' ); ?>> <?php echo __( 'Make input boxes on Posts and Pages bigger', 'postpage-specific-custom-css' ); ?>
    185161            </label>
    186162        </fieldset>
     
    189165
    190166    public function enable_highlighting_in() {
    191         $settings = (array) get_option( $this->option_name );
     167        $settings = (array) get_option( self::OPTION_NAME );
    192168        $field    = 'enable_highlighting_in_settings';
    193169        $value    = (int) ( $settings[ $field ] ?? 0 );
     
    195171        <fieldset>
    196172            <legend class="screen-reader-text">
    197                 <span><?php echo __( 'Enable code highlighting', TEXT_DOMAIN ); ?></span>
     173                <span><?php echo __( 'Enable code highlighting', 'postpage-specific-custom-css' ); ?></span>
    198174            </legend>
    199             <input type="hidden" name="<?php echo $this->option_name; ?>[<?php echo $field; ?>]" value="0">
     175            <input type="hidden" name="<?php echo self::OPTION_NAME; ?>[<?php echo $field; ?>]" value="0">
    200176            <label for="item_<?php echo $field; ?>">
    201177                <input id="item_<?php echo $field; ?>" type="checkbox"
    202                        name="<?php echo $this->option_name; ?>[<?php echo $field; ?>]"
    203                        value="1"<?php echo( ( $value === 1 ) ? ' checked="checked"' : '' ); ?>> <?php echo __( 'Enable code highlighting for fields on settings page', TEXT_DOMAIN ); ?>
     178                       name="<?php echo self::OPTION_NAME; ?>[<?php echo $field; ?>]"
     179                       value="1"<?php echo( ( $value === 1 ) ? ' checked="checked"' : '' ); ?>> <?php echo __( 'Enable code highlighting for fields on settings page', 'postpage-specific-custom-css' ); ?>
    204180            </label>
    205181            <br>
     
    208184            $value = (int) ( $settings[ $field ] ?? 0 );
    209185            ?>
    210             <input type="hidden" name="<?php echo $this->option_name; ?>[<?php echo $field; ?>]" value="0">
     186            <input type="hidden" name="<?php echo self::OPTION_NAME; ?>[<?php echo $field; ?>]" value="0">
    211187            <label for="item_<?php echo $field; ?>">
    212188                <input id="item_<?php echo $field; ?>" type="checkbox"
    213                        name="<?php echo $this->option_name; ?>[<?php echo $field; ?>]"
    214                        value="1"<?php echo( ( $value === 1 ) ? ' checked="checked"' : '' ); ?>> <?php echo __( 'Enable code highlighting for Posts fields', TEXT_DOMAIN ); ?>
     189                       name="<?php echo self::OPTION_NAME; ?>[<?php echo $field; ?>]"
     190                       value="1"<?php echo( ( $value === 1 ) ? ' checked="checked"' : '' ); ?>> <?php echo __( 'Enable code highlighting for Posts fields', 'postpage-specific-custom-css' ); ?>
    215191            </label>
    216192            <br>
     
    219195            $value = (int) ( $settings[ $field ] ?? 0 );
    220196            ?>
    221             <input type="hidden" name="<?php echo $this->option_name; ?>[<?php echo $field; ?>]" value="0">
     197            <input type="hidden" name="<?php echo self::OPTION_NAME; ?>[<?php echo $field; ?>]" value="0">
    222198            <label for="item_<?php echo $field; ?>">
    223199                <input id="item_<?php echo $field; ?>" type="checkbox"
    224                        name="<?php echo $this->option_name; ?>[<?php echo $field; ?>]"
    225                        value="1"<?php echo( ( $value === 1 ) ? ' checked="checked"' : '' ); ?>> <?php echo __( 'Enable code highlighting for Pages fields', TEXT_DOMAIN ); ?>
     200                       name="<?php echo self::OPTION_NAME; ?>[<?php echo $field; ?>]"
     201                       value="1"<?php echo( ( $value === 1 ) ? ' checked="checked"' : '' ); ?>> <?php echo __( 'Enable code highlighting for Pages fields', 'postpage-specific-custom-css' ); ?>
    226202            </label>
    227203        </fieldset>
    228         <p class="description"><?php echo __( '<strong>Warning</strong> Please consider that on weaker computers, enabling CSS highlighting may slow you down.', TEXT_DOMAIN ); ?></p>
     204        <p class="description"><?php echo __( '<strong>Warning</strong> Please consider that on weaker computers, enabling CSS highlighting may slow you down.', 'postpage-specific-custom-css' ); ?></p>
     205        <?php
     206    }
     207
     208    public function control_user_editor() {
     209        $settings = (array) get_option( self::OPTION_NAME );
     210        $field    = 'control_user_editor';
     211        $value    = (int) ( $settings[ $field ] ?? 0 );
     212        ?>
     213        <fieldset>
     214            <input type="hidden" name="<?php echo self::OPTION_NAME; ?>[<?php echo $field; ?>]" value="0">
     215            <label for="item_<?php echo $field; ?>">
     216                <input id="item_<?php echo $field; ?>" type="checkbox"
     217                       name="<?php echo self::OPTION_NAME; ?>[<?php echo $field; ?>]"
     218                       value="1"<?php echo( ( $value === 1 ) ? ' checked="checked"' : '' ); ?>> <?php echo __( 'Allow Editors to edit CSS code for posts and pages.', 'postpage-specific-custom-css' ); ?>
     219            </label>
     220        </fieldset>
    229221        <?php
    230222    }
    231223
    232224    public function default_post_css() {
    233         $settings = (array) get_option( $this->option_name );
    234         $field    = 'default_post_css';
    235         $value    = wp_unslash( $settings[ $field ] ?? '' );
    236         ?>
    237         <fieldset>
    238             <legend class="screen-reader-text">
    239                 <span><?php echo __( 'Default stylesheet for new posts', TEXT_DOMAIN ); ?></span>
     225        $settings    = (array) get_option( self::OPTION_NAME );
     226        $field       = 'default_post_css';
     227        $value       = wp_unslash( $settings[ $field ] ?? '' );
     228        $errors      = $this->validateCSS( $value );
     229        $error_count = count( $errors );
     230        ?>
     231        <fieldset class="<?php echo( ( 0 !== $error_count ) ? 'ppsc_validate_errors' : '' ); ?>">
     232            <legend class="screen - reader - text">
     233                <span><?php echo __( 'Default stylesheet for new posts', 'postpage-specific-custom-css' ); ?></span>
    240234            </legend>
    241235            <p>
    242                 <textarea id="defaultPostCSS" name="<?php echo $this->option_name; ?>[<?php echo $field; ?>]"
    243                           class="large-text code" rows="10" cols="50"><?php echo $value; ?></textarea>
     236                <label class="ppsc_screen_wide"><textarea id="defaultPostCSS"
     237                                                          name=" <?php echo self::OPTION_NAME; ?>[<?php echo $field; ?>]"
     238                                                          class="ppsc_css_source large-text code" rows="10"
     239                                                          cols="50"><?php echo $value; ?></textarea></label>
    244240            </p>
     241            <?php $this->view_errors( $errors ); ?>
    245242        </fieldset>
    246243        <?php
    247244    }
    248245
     246    public function validateCSS( string $css ): array {
     247        $errors     = [];
     248        $imbalanced = false;
     249        if ( preg_match( '#</?\w+#', $css ) ) {
     250            $errors[] = __( 'Markup is not allowed in CSS.', 'postpage-specific-custom-css' );
     251        }
     252        if ( ! $this->validate_balanced_characters( '{', '}', $css ) ) {
     253            $errors[]   = sprintf(
     254            /* translators: 1: {}, 2: }, 3: { */
     255                __( 'Your curly brackets %1$s are imbalanced. Make sure there is a closing %2$s for every opening %3$s.', 'postpage-specific-custom-css' ),
     256                '<code>{}</code>',
     257                '<code>}</code>',
     258                '<code>{</code>'
     259            );
     260            $imbalanced = true;
     261        }
     262        if ( ! $this->validate_balanced_characters( '[', ']', $css ) ) {
     263            $errors[]   = sprintf(
     264            /* translators: 1: {}, 2: }, 3: { */
     265                __( 'Your brackets %1$s are imbalanced. Make sure there is a closing %2$s for every opening %3$s.', 'postpage-specific-custom-css' ),
     266                '<code>[]</code>',
     267                '<code>]</code>',
     268                '<code>[</code>'
     269            );
     270            $imbalanced = true;
     271        }
     272        if ( ! $this->validate_balanced_characters( '(', ')', $css ) ) {
     273            $errors[]   = sprintf(
     274            /* translators: 1: {}, 2: }, 3: { */
     275                __( 'Your parentheses %1$s are imbalanced. Make sure there is a closing %2$s for every opening %3$s.', 'postpage-specific-custom-css' ),
     276                '<code>()</code>',
     277                '<code>)</code>',
     278                '<code>(</code>'
     279            );
     280            $imbalanced = true;
     281        }
     282        if ( ! $this->validate_equal_characters( '"', $css ) ) {
     283            $errors[]   = sprintf(
     284            /* translators: 1: " (double quote) */
     285                __( 'Your double quotes %1$s are uneven. Make sure there is a closing %1$s for every opening %1$s.', 'postpage-specific-custom-css' ),
     286                '<code>"</code>'
     287            );
     288            $imbalanced = true;
     289        }
     290        $unclosed_comment_count = $this->validate_count_unclosed_comments( $css );
     291        if ( 0 < $unclosed_comment_count ) {
     292            $errors[]   = sprintf(
     293            /* translators: 1: number of unclosed comments, 2: *​/ */
     294                _n(
     295                    'There is %1$s unclosed code comment. Close each comment with %2$s.',
     296                    'There are %1$s unclosed code comments. Close each comment with %2$s.',
     297                    $unclosed_comment_count,
     298                    'postpage-specific-custom-css'
     299                ),
     300                $unclosed_comment_count,
     301                '<code>*/</code>'
     302            );
     303            $imbalanced = true;
     304        } elseif ( ! $this->validate_balanced_characters( '/*', '*/', $css ) ) {
     305            $errors[]   = sprintf(
     306            /* translators: 1: *​/, 2: /​* */
     307                __( 'There is an extra %1$s, indicating an end to a comment. Be sure that there is an opening %2$s for every closing %1$s.', 'postpage-specific-custom-css' ),
     308                '<code>*/</code>',
     309                '<code>/*</code>'
     310            );
     311            $imbalanced = true;
     312        }
     313        if ( $imbalanced && $this->is_possible_content_error( $css ) ) {
     314            $errors[] = sprintf(
     315            /* translators: %s: content: ""; */
     316                __( 'Imbalanced/unclosed character errors can be caused by %s declarations. You may need to remove this or add it to a custom CSS file.', 'postpage-specific-custom-css' ),
     317                '<code>content: "";</code>'
     318            );
     319        }
     320
     321        return $errors;
     322    }
     323
     324    private function validate_balanced_characters( string $opening_char, string $closing_char, string $css ): bool {
     325        return substr_count( $css, $opening_char ) === substr_count( $css, $closing_char );
     326    }
     327
     328    private function validate_equal_characters( string $char, string $css ): bool {
     329        $char_count = substr_count( $css, $char );
     330
     331        return ( 0 === $char_count % 2 );
     332    }
     333
     334    private function validate_count_unclosed_comments( string $css ): int {
     335        $count    = 0;
     336        $comments = explode( '/*', $css );
     337
     338        if ( ! is_array( $comments ) || ( 1 >= count( $comments ) ) ) {
     339            return $count;
     340        }
     341
     342        unset( $comments[0] ); // The first item is before the first comment.
     343        foreach ( $comments as $comment ) {
     344            if ( false === strpos( $comment, '*/' ) ) {
     345                $count ++;
     346            }
     347        }
     348
     349        return $count;
     350    }
     351
     352    private function is_possible_content_error( string $css ): bool {
     353        $found = preg_match( '/\bcontent\s*:/', $css );
     354        if ( ! empty( $found ) ) {
     355            return true;
     356        }
     357
     358        return false;
     359    }
     360
     361    public function view_errors( array $errors ) {
     362        $error_count = count( $errors );
     363        if ( 0 === $error_count ) {
     364            return;
     365        }
     366        ?>
     367        <ul class="errors">
     368            <?php foreach ( $errors as $error ) : ?>
     369                <li><?php echo $error; ?></li>
     370            <?php endforeach; ?>
     371        </ul>
     372        <?php
     373    }
     374
    249375    public function default_page_css() {
    250         $settings = (array) get_option( $this->option_name );
    251         $field    = 'default_page_css';
    252         $value    = wp_unslash( $settings[ $field ] ?? '' );
    253         ?>
    254         <fieldset>
     376        $settings    = (array) get_option( self::OPTION_NAME );
     377        $field       = 'default_page_css';
     378        $value       = wp_unslash( $settings[ $field ] ?? '' );
     379        $errors      = $this->validateCSS( $value );
     380        $error_count = count( $errors );
     381        ?>
     382        <fieldset class="<?php echo( ( 0 !== $error_count ) ? 'ppsc_validate_errors' : '' ); ?>">
    255383            <legend class="screen-reader-text">
    256                 <span><?php echo __( 'Default stylesheet for new pages', TEXT_DOMAIN ); ?></span>
     384                <span><?php echo __( 'Default stylesheet for new pages', 'postpage-specific-custom-css' ); ?></span>
    257385            </legend>
    258386            <p>
    259                 <textarea id="defaultPageCSS" name="<?php echo $this->option_name; ?>[<?php echo $field; ?>]"
    260                           class="large-text code" rows="10" cols="50"><?php echo $value; ?></textarea>
     387                <label class="ppsc_screen_wide"><textarea id="defaultPageCSS"
     388                                                          name="<?php echo self::OPTION_NAME; ?>[<?php echo $field; ?>]"
     389                                                          class="large-text code" rows="10"
     390                                                          cols="50"><?php echo $value; ?></textarea></label>
    261391            </p>
     392            <?php $this->view_errors( $errors ); ?>
    262393        </fieldset>
    263394        <?php
     
    266397    public function section_default_values() {
    267398        ?>
     399        <style>
     400            fieldset label.ppsc_screen_wide {
     401                width: 100%;
     402            }
     403
     404            fieldset.ppsc_validate_errors ul {
     405                color: #a00;
     406            }
     407        </style>
    268408        <p>
    269             <?php echo __( 'You can set the pre-filled content for your newly created posts or pages.', TEXT_DOMAIN ); ?>
     409            <?php echo __( 'You can set the pre-filled content for your newly created posts or pages. Warning: improper CSS will be stored but not attached.', 'postpage-specific-custom-css' ); ?>
    270410        </p>
    271411        <?php
     
    273413
    274414    public function section_plugin_behavior() {
    275         if ( $this->isBirthday || $this->isDayBefore || $this->isWeekAfter ) {
    276             $this->adminNotices( true );
    277         }
    278     }
    279 
    280     public function adminNotices( $forceShow = false ) {
    281         if ( ! current_user_can( 'manage_options' ) ) {
    282             return;
    283         }
    284         $ppscc_birthday = (string) ( $_COOKIE['ppscc_birthday'] ?? '' );
    285         if ( $ppscc_birthday === 'hide' ) {
    286             return;
    287         }
    288         if ( ! $forceShow ) {
    289             if ( ! $this->isBirthday && ! $this->isDayBefore && ! $this->isWeekAfter ) {
    290                 return;
    291             }
    292         }
    293         $current_user = wp_get_current_user();
    294         $transient    = 'ppsc_lastview_' . $current_user->ID . '_' . date( 'Ymd' );
    295         $tValue       = get_transient( $transient );
    296         if ( ( false !== $tValue ) && ! $forceShow ) {
    297             return;
    298         }
    299         //set_transient( $transient, '1', 6 * HOUR_IN_SECONDS );
     415        ?>
     416        <p><?php echo __( 'Be careful. Allowing editors to edit your CSS may crash your site layout if in wrong hands.', 'postpage-specific-custom-css' ); ?></p>
     417        <?php
     418    }
     419
     420    public function adminNotices( $show_on_demand = false ) {
     421        if ( ! $show_on_demand && $this->current_user_already_seen_message() ) {
     422            return;
     423        }
    300424        add_action( 'admin_print_footer_scripts', function () {
    301             $expires = date( 'D, j M H:i:s e', time() + MONTH_IN_SECONDS );
    302425            ?>
    303426            <script>
    304427                jQuery(function ($) {
    305                     $(document).on('click', '#ppscc_birthday_notice > button.notice-dismiss', function (event) {
    306                         document.cookie = "ppscc_birthday=hide; SameSite=Lax; expires=<?php echo $expires; ?>";
    307                         console.log('Cookie set');
     428                    $(document).on('click', '#ppscc_birthday_notice > button.notice-dismiss', function () {
     429                        $.post(ajaxurl, {
     430                            url: ajaxurl,
     431                            action: "ppscc_hide",
     432                            ppscc_nonce: "<?php echo wp_create_nonce( self::NONCE_HIDE_BIRTHDAY ); ?>",
     433                        }, function (response) {
     434                        });
    308435                    });
    309436                    $(document).on('click', '.ppsc_show', function (event) {
    310437                        event.preventDefault();
    311                         var id = '#acinfofor_' + $(this).data('ppsc');
     438                        const id = '#acinfofor_' + $(this).data('ppsc');
    312439                        $('.ppsc_hide_all').hide(0);
    313440                        $(id).show(0);
    314441                    });
    315                     console.clear();
    316                     console.log('DOM is READY');
    317442                });
    318443            </script>
    319444            <style>
     445                .ppsc_screen_wide {
     446                    width: 100%;
     447                }
     448
    320449                .ppsc_hide_all {
    321450                    margin: 1rem 0;
     
    343472                    text-align: center;
    344473                    font-weight: bold;
    345                     letter-spacing: .5px;
     474                    letter-spacing: 1px;
    346475                }
    347476            </style>
    348477            <?php
    349478        } );
    350         $this->flagUrl = plugins_url( '/assets/flags/', __FILE__ );
    351         $flagList      = [ 'us', 'gb', 'eu', 'ch', 'pl' ];
     479        $this->flagsURLs = plugins_url( '/assets/flags/', __FILE__ );
     480        $flagList        = [ 'us', 'gb', 'eu', 'ch', 'pl' ];
     481        $current_user    = wp_get_current_user();
    352482        ?>
    353483        <div id="ppscc_birthday_notice" class="notice notice-success is-dismissible">
    354484            <p><span class="dashicons dashicons-buddicons-groups"
    355485                     style="font-size:120px;width: 120px;height: 120px;float:left;color:#0a0;"></span>
    356                 <strong><?php echo sprintf( __( 'Hello %s!', TEXT_DOMAIN ), $current_user->display_name ); ?></strong>
     486                <strong><?php echo sprintf( __( 'Hello %s!', 'postpage-specific-custom-css' ), $current_user->display_name ); ?></strong>
    357487            </p>
    358488            <p>
    359489                <?php
    360490                if ( $this->isBirthday ) {
    361                     echo __( 'Today is my birthday.', TEXT_DOMAIN ) . ' ';
    362                     echo sprintf( __( 'I hope I just turned <strong>%d</strong>.', TEXT_DOMAIN ), ( (int) date( 'Y' ) ) - 1977 ) . ' ';
     491                    echo __( 'Today is my birthday.', 'postpage-specific-custom-css' ) . ' ';
     492                    echo sprintf( __( 'I hope I just turned <strong>%d</strong>.', 'postpage-specific-custom-css' ), ( (int) date( 'Y' ) ) - 1977 ) . ' ';
    363493                }
    364494                if ( $this->isDayBefore ) {
    365                     echo __( 'Tomorrow will be my birthday.', TEXT_DOMAIN ) . ' ';
    366                     echo sprintf( __( 'I hope I will turn <strong>%d</strong> tomorrow.', TEXT_DOMAIN ), ( (int) date( 'Y' ) ) - 1977 ) . ' ';
     495                    echo __( 'Tomorrow will be my birthday.', 'postpage-specific-custom-css' ) . ' ';
     496                    echo sprintf( __( 'I hope I will turn <strong>%d</strong> tomorrow.', 'postpage-specific-custom-css' ), ( (int) date( 'Y' ) ) - 1977 ) . ' ';
    367497                }
    368                 if ( $this->isWeekAfter ) {
    369                     echo __( 'This week were my birthday.', TEXT_DOMAIN ) . ' ';
    370                     echo sprintf( __( 'I hope I turned <strong>%d</strong>...', TEXT_DOMAIN ), ( (int) date( 'Y' ) ) - 1977 ) . ' ';
     498                if ( $this->isDayAfter ) {
     499                    echo __( 'Yesterday were my birthday.', 'postpage-specific-custom-css' ) . ' ';
     500                    echo sprintf( __( 'I hope I turned <strong>%d</strong>...', 'postpage-specific-custom-css' ), ( (int) date( 'Y' ) ) - 1977 ) . ' ';
    371501                }
    372                 echo sprintf( __( 'I just think, maybe you want to <a href="%s">give me a review</a> for my plugin?', TEXT_DOMAIN ), 'https://wordpress.org/support/plugin/postpage-specific-custom-css/reviews/#new-post' ) . ' ';
    373                 echo __( 'Or maybe you have such a good situation that you would like to consider a small donation? Click on the currency flag and the account number will show if you would like to repay my work. <strong>I do not insist!</strong> It would be just nice to get a birthday present.', TEXT_DOMAIN ) . '<br>';
     502                echo sprintf( __( 'I just think, maybe you want to <a href="%s">give me a review</a> for my plugin?', 'postpage-specific-custom-css' ), 'https://wordpress.org/support/plugin/postpage-specific-custom-css/reviews/#new-post' ) . ' ';
     503                echo __( 'Or maybe you have such a good situation that you would like to consider a small donation? Click on the currency flag and the account number will show if you would like to repay my work. <strong>I do not insist!</strong> It would be just nice to get a birthday present.', 'postpage-specific-custom-css' ) . '<br>';
    374504                ?>
    375505            </p>
    376506            <p class="ppsc_story"><?php
    377                 echo __( 'Or maybe you would like to know my story? I weighed 230.6 kg (508.5lb), I couldn\'t move, I almost became a cripple. Thanks to my fiancee I started to lose weight. I\'m halfway there. Currently, I weigh about 165kg (363lb). Or maybe less?', TEXT_DOMAIN ) . ' ';
    378                 echo __( '<a href="%s">Follow me on Instagram</a> and <a href="%s">follow the Facebook page</a>. You can also send me wishes there if you like, thank you :)', TEXT_DOMAIN ) . ' ';
     507                echo __( 'Or maybe you would like to know my story? I weighed 230.6 kg (508.5lb), I couldn\'t move, I almost became a cripple. Thanks to my fiancee I started to lose weight. I\'m halfway there. Currently, I weigh about 165kg (363lb). Or maybe less?', 'postpage-specific-custom-css' ) . ' ';
     508                echo __( '<a href="%s">Follow me on Instagram</a> and <a href="%s">follow the Facebook page</a>. You can also send me wishes there if you like, thank you :)', 'postpage-specific-custom-css' ) . ' ';
    379509                ?></p>
    380             <p style="text-align: center"><?php echo __( 'Click on the flag to see account details. For every, even the smallest payment - thank you a lot!', TEXT_DOMAIN ); ?></p>
     510            <p style="text-align: center"><?php echo __( 'Click on the flag to see account details. For every, even the smallest payment - thank you a lot!', 'postpage-specific-custom-css' ); ?></p>
    381511            <p style="text-align: center">
    382512                <?php foreach ( $flagList as $code ) : ?>
     
    394524            ];
    395525            $currency = [
    396                 'us' => __( 'United States dollar - USD', TEXT_DOMAIN ),
    397                 'gb' => __( 'Pound sterling - GBP', TEXT_DOMAIN ),
    398                 'eu' => __( 'Euro - EUR', TEXT_DOMAIN ),
    399                 'ch' => __( 'Swiss franc - CHF', TEXT_DOMAIN ),
    400                 'pl' => __( 'Polish złoty - PLN', TEXT_DOMAIN ),
     526                'us' => __( 'United States dollar - USD', 'postpage-specific-custom-css' ),
     527                'gb' => __( 'Pound sterling - GBP', 'postpage-specific-custom-css' ),
     528                'eu' => __( 'Euro - EUR', 'postpage-specific-custom-css' ),
     529                'ch' => __( 'Swiss franc - CHF', 'postpage-specific-custom-css' ),
     530                'pl' => __( 'Polish złoty - PLN', 'postpage-specific-custom-css' ),
    401531            ];
    402532            foreach ( $flagList as $code ) {
     
    408538    }
    409539
    410     public function flag( $code ) {
     540    public function current_user_already_seen_message(): bool {
     541        if ( ! current_user_can( 'manage_options' ) ) {
     542            // Always return true for non-administrators
     543            return true;
     544        }
     545        $value = (array) ( get_user_meta( get_current_user_id(), self::USER_META_STRING, true ) ?? [] );
     546
     547        return (bool) ( $value[ date( 'Y' ) ] ?? false );
     548    }
     549
     550    public function flag(
     551        $code
     552    ): string {
    411553        $alt = '';
    412554        switch ( $code ) {
    413555            case 'us':
    414                 $alt = __( 'United States dollar - USD', TEXT_DOMAIN );
     556                $alt = __( 'United States dollar - USD', 'postpage-specific-custom-css' );
    415557                break;
    416558            case 'gb':
    417                 $alt = __( 'Pound sterling - GBP', TEXT_DOMAIN );
     559                $alt = __( 'Pound sterling - GBP', 'postpage-specific-custom-css' );
    418560                break;
    419561            case 'eu':
    420                 $alt = __( 'Euro - EUR', TEXT_DOMAIN );
     562                $alt = __( 'Euro - EUR', 'postpage-specific-custom-css' );
    421563                break;
    422564            case 'ch':
    423                 $alt = __( 'Swiss franc - CHF', TEXT_DOMAIN );
     565                $alt = __( 'Swiss franc - CHF', 'postpage-specific-custom-css' );
    424566                break;
    425567            case 'pl':
    426                 $alt = __( 'Polish złoty - PLN', TEXT_DOMAIN );
     568                $alt = __( 'Polish złoty - PLN', 'postpage-specific-custom-css' );
    427569                break;
    428570        }
    429571
    430         return '<img src="' . $this->flagUrl . $code . '.png' . '" alt="' . $alt . '" title="' . $alt . '" style="width:32px;height:22px;">';
    431     }
    432 
    433     public function accountLine( $code, $account, $currency ) {
     572        return '<img src="' . $this->flagsURLs . $code . '.png' . '" alt="' . $alt . '" title="' . $alt . '" style="width:32px;height:22px;">';
     573    }
     574
     575    public function accountLine(
     576        $code, $account, $currency
     577    ) {
    434578        ?>
    435579        <div class="ppsc_hide_all" id="acinfofor_<?php echo $code; ?>">
    436             <div class="ppsc_info_line"><?php echo __( 'BIC/SWIFT:', TEXT_DOMAIN ); ?>
     580            <div class="ppsc_info_line"><?php echo __( 'BIC/SWIFT:', 'postpage-specific-custom-css' ); ?>
    437581                <span>ALBPPLPW</span>
    438                 <?php echo __( 'Currency:', TEXT_DOMAIN ) . ' <span> ' . $currency; ?></span></div>
    439             <div class="ppsc_info_line"><?php echo __( 'Account number:', TEXT_DOMAIN ); ?>
     582                <?php echo __( 'Currency:', 'postpage-specific-custom-css' ) . ' <span> ' . $currency; ?></span></div>
     583            <div class="ppsc_info_line"><?php echo __( 'Account number:', 'postpage-specific-custom-css' ); ?>
    440584                <span><?php echo $account; ?></span></div>
    441585        </div>
     
    443587    }
    444588
    445     public function page_settings_link_filter( $links ) {
     589    public function page_settings_link_filter(
     590        $links
     591    ) {
    446592        if ( ! is_array( $links ) ) {
    447593            $links = [];
    448594        }
    449         $links[] = '<a href="' . $this->build_settings_link() . '">' . __( 'Settings', TEXT_DOMAIN ) . '</a>';
    450         if ( $this->isBirthday || $this->isDayBefore || $this->isWeekAfter ) {
    451             $links[] = '<a href="' . get_admin_url() . 'options-general.php?page=post-page-custom-css">' . __( 'My birthday!', TEXT_DOMAIN ) . '</a>';
     595        $links[] = '<a href="' . $this->build_settings_link() . '">' . __( 'Settings', 'postpage-specific-custom-css' ) . '</a>';
     596        if ( $this->isBirthday || $this->isDayBefore || $this->isDayAfter ) {
     597            $links[] = '<a href="' . get_admin_url() . 'options-general.php?page=post-page-custom-css">' . __( 'My birthday!', 'postpage-specific-custom-css' ) . '</a>';
    452598        }
    453599
     
    456602
    457603    private function build_settings_link() {
    458         return admin_url( $this->menu_parent_slug . '?page=' . $this->menu_slug );
     604        return admin_url( self::PARENT_MENU_SLUG . '?page=' . self::MENU_SLUG );
    459605    }
    460606
    461607    public function add_options_page() {
    462         $sub_menu_suffix = add_submenu_page( $this->menu_parent_slug, __( 'Post/Page specific custom CSS', TEXT_DOMAIN ), __( 'Post/Page CSS', TEXT_DOMAIN ), 'manage_options', $this->menu_slug, [
     608        $sub_menu_suffix = add_submenu_page( self::PARENT_MENU_SLUG, __( 'Post/Page specific custom CSS', 'postpage-specific-custom-css' ), __( 'Post/Page CSS', 'postpage-specific-custom-css' ), 'manage_options', self::MENU_SLUG, [
    463609            $this,
    464610            'options_page_view',
    465611        ] );
    466         $settings        = (array) get_option( $this->option_name );
     612        $settings        = (array) get_option( self::OPTION_NAME );
    467613        $field           = 'enable_highlighting_in_settings';
    468614        $value           = (int) ( $settings[ $field ] ?? 0 );
     
    476622
    477623    public function options_page_view() {
    478 
    479624        ?>
    480625        <div class="wrap">
    481             <h1><?php echo __( 'Post/Page Custom CSS', TEXT_DOMAIN ); ?></h1>
     626            <h1><?php echo __( 'Post/Page Custom CSS', 'postpage-specific-custom-css' ); ?></h1>
    482627            <form action="options.php" method="POST">
    483                 <?php settings_fields( $this->option_group ); ?>
     628                <?php settings_fields( self::OPTION_GROUP ); ?>
    484629                <div>
    485                     <?php do_settings_sections( $this->menu_slug ); ?>
     630                    <?php do_settings_sections( self::MENU_SLUG ); ?>
    486631                </div>
    487632                <?php submit_button(); ?>
     
    489634        </div>
    490635        <?php
    491         $settings = (array) get_option( $this->option_name );
     636        $settings = (array) get_option( self::OPTION_NAME );
    492637        $field    = 'enable_highlighting_in_settings';
    493638        $value    = (int) ( $settings[ $field ] ?? 0 );
     
    496641            <script>
    497642                jQuery(function ($) {
    498                     var defaultPageCSS = $('#defaultPageCSS');
    499                     var defaultPostCSS = $('#defaultPostCSS');
    500                     var editorSettings;
    501                     var editor;
     643                    const defaultPageCSS = $('#defaultPageCSS');
     644                    const defaultPostCSS = $('#defaultPostCSS');
     645                    let editorSettings;
    502646                    if (defaultPageCSS.length === 1) {
    503647                        editorSettings = wp.codeEditor.defaultSettings ? _.clone(wp.codeEditor.defaultSettings) : {};
     
    505649                            indentUnit: 2, tabSize: 2, mode: 'css',
    506650                        });
    507                         editor = wp.codeEditor.initialize(defaultPageCSS, editorSettings);
     651                        wp.codeEditor.initialize(defaultPageCSS, editorSettings);
    508652                    }
    509653                    if (defaultPostCSS.length === 1) {
     
    512656                            indentUnit: 2, tabSize: 2, mode: 'css',
    513657                        });
    514                         editor = wp.codeEditor.initialize(defaultPostCSS, editorSettings);
     658                        wp.codeEditor.initialize(defaultPostCSS, editorSettings);
    515659                    }
    516660                });
     
    520664    }
    521665
    522     public function the_content( $content ) {
    523         if ( isset( $GLOBALS['post'] ) ) {
    524             $post_id                    = $GLOBALS['post']->ID;
    525             $phylax_ppsccss_single_only = get_post_meta( $post_id, '_phylax_ppsccss_single_only', true );
    526             $phylax_ppsccss_css         = get_post_meta( $post_id, '_phylax_ppsccss_css', true );
    527             if ( '' != $phylax_ppsccss_css ) {
    528                 # mamy css!
    529                 if ( is_single() || is_page() ) {
    530                     $content = $this->join( $content, $phylax_ppsccss_css );
    531                 } elseif ( '0' == $phylax_ppsccss_single_only ) {
    532                     $content = $this->join( $content, $phylax_ppsccss_css );
     666    public function the_content(
     667        string $content
     668    ): string {
     669        global $post;
     670        if ( ! isset( $post ) || ! is_a( $post, 'WP_Post' ) ) {
     671            return $content;
     672        }
     673        /** @var \WP_Post $post */
     674        $phylax_ppsccss_single_only = get_post_meta( $post->ID, self::POST_META_SINGLE, true );
     675        $phylax_ppsccss_css         = get_post_meta( $post->ID, self::POST_META_CSS, true );
     676        if ( '' != $phylax_ppsccss_css ) {
     677
     678            $phylax_valid_css = (string) ( get_post_meta( $post->ID, self::POST_META_VALID, true ) ?? '' );
     679            if ( '' === $phylax_valid_css ) {
     680                $errors = $this->validateCSS( $phylax_ppsccss_css );
     681                if ( 0 === count( $errors ) ) {
     682                    $phylax_valid_css = '1';
     683                } else {
     684                    $phylax_valid_css = '0';
    533685                }
     686                update_post_meta( $post->ID, self::POST_META_VALID, $phylax_valid_css );
    534687            }
     688            if ( '1' !== $phylax_valid_css ) {
     689                return $content;
     690            }
     691
     692            if ( is_single() || is_page() ) {
     693                $content = $this->join( $content, $phylax_ppsccss_css );
     694            } elseif ( '0' == $phylax_ppsccss_single_only ) {
     695                $content = $this->join( $content, $phylax_ppsccss_css );
     696            }
    535697        }
    536698
     
    538700    }
    539701
    540     public function join( $content, $css ) {
    541         return '<!-- ' . __( 'Added by Post/Page specific custom CSS plugin, thank you for using!', TEXT_DOMAIN ) . ' -->' . PHP_EOL . '<style type="text/css">' . $css . '</style>' . PHP_EOL . $content;
     702    public function join(
     703        $content, $css
     704    ): string {
     705        return '<!-- ' . __( 'Added by Post/Page specific custom CSS plugin, thank you for using!', 'postpage-specific-custom-css' ) . ' -->' . PHP_EOL . '<style>' . $css . '</style>' . PHP_EOL . $content;
    542706    }
    543707
    544708    public function add_meta_boxes() {
    545         if ( current_user_can( 'manage_options' ) ) {
    546             add_meta_box( 'phylax_ppsccss', __( 'Custom CSS', TEXT_DOMAIN ), [
     709        if ( $this->allowedToView() ) {
     710            add_meta_box( 'phylax_ppsccss', __( 'Custom CSS', 'postpage-specific-custom-css' ), [
    547711                $this,
    548712                'render_phylax_ppsccss',
     
    554718    }
    555719
    556     public function save_post( $post_id ) {
    557         $test_id = (int) $post_id;
    558         if ( $test_id < 1 ) {
    559             return $post_id;
    560         }
    561         if ( ! isset( $_POST['phylax_ppsccss_nonce'] ) ) {
    562             return $post_id;
    563         }
    564         $nonce = $_POST['phylax_ppsccss_nonce'];
    565         if ( ! wp_verify_nonce( $nonce, 'phylax_ppsccss' ) ) {
    566             return $post_id;
     720    public function allowedToView() {
     721        $settings      = (array) get_option( self::OPTION_NAME );
     722        $allow_editors = (bool) ( $settings['control_user_editor'] ?? 0 );
     723        if (
     724            current_user_can( 'manage_options' ) ||
     725            (
     726                $allow_editors &&
     727                current_user_can( 'edit_others_pages' )
     728            )
     729        ) {
     730            return true;
     731        }
     732
     733        return false;
     734    }
     735
     736    public function save_post(
     737        int $post_id
     738    ) {
     739        $post_id = abs( $post_id );
     740        if ( 0 === $post_id ) {
     741            return;
     742        }
     743        $nonce_value = (string) ( $_POST['phylax_ppsccss_nonce'] ?? '' );
     744        if ( '' === $nonce_value ) {
     745            return;
     746        }
     747        if ( ! wp_verify_nonce( $nonce_value, 'phylax_ppsccss' ) ) {
     748            return;
    567749        }
    568750        if ( ( 'page' != $_POST['post_type'] ) && ( 'post' != $_POST['post_type'] ) ) {
    569             return $post_id;
    570         }
    571         if ( ( 'post' == $_POST['post_type'] ) && ! current_user_can( 'edit_post', $post_id ) ) {
    572             return $post_id;
    573         }
    574         if ( ( 'page' == $_POST['post_type'] ) && ! current_user_can( 'edit_page', $post_id ) ) {
    575             return $post_id;
    576         }
    577         if ( defined( 'DOING_AUTOSAVE' ) && \DOING_AUTOSAVE ) {
    578             return $post_id;
     751            return;
     752        }
     753        if ( ! $this->allowedToView() ) {
     754            return;
     755        }
     756        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
     757            return;
    579758        }
    580759        $phylax_ppsccss_css         = trim( strip_tags( $_POST['phylax_ppsccss_css'] ) );
     
    583762            $phylax_ppsccss_single_only = 0;
    584763        }
    585         update_post_meta( $post_id, '_phylax_ppsccss_css', $phylax_ppsccss_css );
    586         update_post_meta( $post_id, '_phylax_ppsccss_single_only', $phylax_ppsccss_single_only );
    587     }
    588 
    589     public function render_phylax_ppsccss( $post ) {
     764        $errors           = $this->validateCSS( $phylax_ppsccss_css );
     765        $phylax_valid_css = '0';
     766        if ( 0 === count( $errors ) ) {
     767            $phylax_valid_css = '1';
     768        }
     769        update_post_meta( $post_id, self::POST_META_CSS, $phylax_ppsccss_css );
     770        update_post_meta( $post_id, self::POST_META_SINGLE, $phylax_ppsccss_single_only );
     771        update_post_meta( $post_id, self::POST_META_VALID, $phylax_valid_css );
     772    }
     773
     774    public function render_phylax_ppsccss(
     775        $post
     776    ) {
    590777        wp_nonce_field( 'phylax_ppsccss', 'phylax_ppsccss_nonce' );
    591         $screen   = '';
    592         $settings = (array) get_option( $this->option_name );
     778        $screen   = $field = $dField = '';
     779        $settings = (array) get_option( self::OPTION_NAME );
    593780        switch ( $post->post_type ) {
    594781            case 'post':
    595782                $field  = 'enable_highlighting_in_posts';
    596783                $dField = 'default_page_css';
    597                 $screen = __( 'Post custom CSS', TEXT_DOMAIN );
     784                $screen = __( 'Post custom CSS', 'postpage-specific-custom-css' );
    598785                break;
    599786            case 'page':
    600787                $field  = 'enable_highlighting_in_pages';
    601788                $dField = 'default_post_css';
    602                 $screen = __( 'Page custom CSS', TEXT_DOMAIN );
     789                $screen = __( 'Page custom CSS', 'postpage-specific-custom-css' );
    603790                break;
    604791        }
    605         if ( '' == $screen ) {
     792        if ( '' === $field ) {
    606793            return;
    607794        }
     
    609796        $post_meta           = get_post_meta( $post->ID );
    610797        $brand_new           = false;
    611         if ( false === isset( $post_meta['_phylax_ppsccss_css'] ) ) {
     798        if ( false === isset( $post_meta[ self::POST_META_CSS ] ) ) {
    612799            $brand_new = true;
    613800        }
    614         $phylax_ppsccss_css = get_post_meta( $post->ID, '_phylax_ppsccss_css', true );
     801        $phylax_ppsccss_css = get_post_meta( $post->ID, self::POST_META_CSS, true );
    615802        if ( ( '' === $phylax_ppsccss_css ) && ( true === $brand_new ) ) {
    616             $phylax_ppsccss_css .= (string) ( $settings[ $dField ] );
    617         }
    618         $phylax_ppsccss_single_only = get_post_meta( $post->ID, '_phylax_ppsccss_single_only', true );
     803            $phylax_ppsccss_css .= $settings[ $dField ];
     804        }
     805        $phylax_ppsccss_single_only = get_post_meta( $post->ID, self::POST_META_SINGLE, true );
    619806        if ( '' == $phylax_ppsccss_single_only ) {
    620807            $phylax_ppsccss_single_only = 0;
     
    626813        }
    627814        $biggerBox = (int) ( $settings['bigger_textarea'] ?? 0 );
     815        $errors    = $this->validateCSS( $phylax_ppsccss_css );
    628816        ?>
    629817        <p class="post-attributes-label-wrapper">
    630818            <label for="phylax_ppsccss_css"><?php echo $screen; ?></label>
    631819        </p>
     820        <style>
     821            ul.errors {
     822                color: #a00;
     823            }
     824        </style>
     825        <?php $this->view_errors( $errors ); ?>
    632826        <div id="phylax_ppsccss_css_outer">
    633827            <textarea name="phylax_ppsccss_css" id="phylax_ppsccss_css" class="widefat textarea"
     
    638832                                                           value="0"><input type="checkbox"
    639833                                                                            name="phylax_ppsccss_single_only" value="1"
    640                                                                             id="phylax_ppsccss_single_only"<?php echo $checked; ?>> <?php echo __( 'Attach this CSS code only on single page view', TEXT_DOMAIN ); ?>
     834                                                                            id="phylax_ppsccss_single_only"<?php echo $checked; ?>> <?php echo __( 'Attach this CSS code only on single page view', 'postpage-specific-custom-css' ); ?>
    641835            </label>
    642836        </p>
    643837        <p>
    644838            <?php
    645             echo __( 'Please add only valid CSS code, it will be placed between &lt;style&gt; tags.', TEXT_DOMAIN ); ?>
     839            echo __( 'Please add only valid CSS code, it will be placed between &lt;style&gt; tags.', 'postpage-specific-custom-css' ); ?>
    646840        </p>
    647841        <?php
     
    650844            <script>
    651845                jQuery(function ($) {
    652                     var phylaxCSSEditorDOM = $('#phylax_ppsccss_css');
    653                     var phylaxCSSEditorSettings;
    654                     var phylaxCSSEditorInstance;
     846                    const phylaxCSSEditorDOM = $('#phylax_ppsccss_css');
     847                    let phylaxCSSEditorSettings;
     848                    let phylaxCSSEditorInstance;
    655849                    if (phylaxCSSEditorDOM.length === 1) {
    656850                        phylaxCSSEditorSettings = wp.codeEditor.defaultSettings ? _.clone(wp.codeEditor.defaultSettings) : {};
     
    681875    }
    682876
    683     public function pluginInfo() {
    684         /** TODO: see if there is a new plugin, now there is not. */
    685     }
    686 
    687877    public function init() {
    688         load_plugin_textdomain( TEXT_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
    689     }
    690 
    691     private function text() {
    692         __( 'Post/Page specific custom CSS will allow you to add cascade stylesheet to specific posts/pages. It will give you special area in the post/page edit field to attach your CSS. It will also let you decide if this CSS has to be added in multi-page/post view (like archive posts) or only in a single view.', TEXT_DOMAIN );
     878        load_plugin_textdomain( 'postpage-specific-custom-css', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
     879        if ( current_user_can( 'manage_options' ) ) {
     880            $ppscc_birthday = (string) ( get_user_meta( get_current_user_id(), 'ppscc_birthday', true ) ?? '' );
     881            if ( 'hide' === $ppscc_birthday ) {
     882                return;
     883            }
     884            add_action( 'wp_ajax_ppscc_hide', function () {
     885                $ppscc_nonce = (string) ( $_POST['ppscc_nonce'] ?? '' );
     886                if ( '' === $ppscc_nonce ) {
     887                    exit;
     888                }
     889                if ( false === wp_verify_nonce( $ppscc_nonce, self::NONCE_HIDE_BIRTHDAY ) ) {
     890                    exit;
     891                }
     892                if ( ! current_user_can( 'manage_options' ) ) {
     893                    exit;
     894                }
     895                $this->update_user_meta();
     896                echo json_encode( [ 'error' => 0 ] );
     897                exit;
     898            } );
     899        }
     900    }
     901
     902    public function update_user_meta() {
     903        if ( ! current_user_can( 'manage_options' ) ) {
     904            return;
     905        }
     906        $current_user_id      = get_current_user_id();
     907        $value                = (array) ( get_user_meta( $current_user_id, self::USER_META_STRING, true ) ?? [] );
     908        $value[ date( 'Y' ) ] = 1;
     909        update_user_meta( $current_user_id, self::USER_META_STRING, $value );
     910    }
     911
     912    /**
     913     * This text is used for readme and header translation
     914     */
     915    private function _text() {
     916        __( 'Post/Page specific custom CSS will allow you to add cascade stylesheet to specific posts/pages. It will give you special area in the post/page edit field to attach your CSS. It will also let you decide if this CSS has to be added in multi-page/post view (like archive posts) or only in a single view.', 'postpage-specific-custom-css' );
    693917    }
    694918
  • postpage-specific-custom-css/tags/0.2.2/readme.txt

    r2528049 r2529282  
    4848
    4949= 0.2.2 =
    50 * Release date: 2020-05-07
     50* Release date: 2020-05-10
     51* Lot of fixes, to stay up to date with WordPress code rules
    5152* Birthday banner visible only for administrators
    5253* Birthday banner now can be hidden for the next year
     54+ Now it's possible to let editors edit CSS
    5355
    5456= 0.2.1 =
Note: See TracChangeset for help on using the changeset viewer.