Plugin Directory

Changeset 1063263


Ignore:
Timestamp:
01/08/2015 05:16:47 PM (11 years ago)
Author:
aercolino
Message:

Extracted capabilities into their own class EnzymesCapabilities.
All green tests.
WIP

Location:
enzymes/trunk/src
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • enzymes/trunk/src/Enzymes3.php

    r1063182 r1063263  
    22require_once dirname(ENZYMES_FILENAME) . '/vendor/Ando/Regex.php';
    33require_once 'EnzymesSequence.php';
     4require_once 'EnzymesCapabilities.php';
     5require_once 'EnzymesOption.php';
    46
    57class Enzymes3
     
    79    const PREFIX = 'enzymes.';
    810
    9     static public
    10     function capabilities()
    11     {
    12 //@Formatter:off
    13         $result = array(
    14                 self::PREFIX . 'inject'                       => 'It allows a user to inject enzymes into her posts.',
    15                 self::PREFIX . 'use_own_attributes'           => 'It allows a user to make her enzymes with her own attributes.',
    16                 self::PREFIX . 'use_others_attributes'        => 'It allows a user to make her enzymes with others\' attributes.',
    17                 self::PREFIX . 'use_own_custom_fields'        => 'It allows a user to make her enzymes with her own custom fields.',
    18                 self::PREFIX . 'use_others_custom_fields'     => 'It allows a user to make her enzymes with others\' custom fields.',
    19                 self::PREFIX . 'create_static_custom_fields'  => 'It allows a user to create enzymes from non-evaluated custom fields.',
    20                 self::PREFIX . 'create_dynamic_custom_fields' => 'It allows a user to create enzymes from evaluated custom fields.',
    21                 self::PREFIX . 'share_static_custom_fields'   => 'It allows a user to share her enzymes from non-evaluated custom fields.',
    22                 self::PREFIX . 'share_dynamic_custom_fields'  => 'It allows a user to share her enzymes from evaluated custom fields.',
    23         );
    24 //@Formatter:on
    25         return $result;
    26     }
     11    /**
     12     * @var EnzymesOption
     13     */
     14    protected $options;
    2715
    2816    /**
     
    291279        $this->init_grammar();
    292280        $this->init_expressions();
     281        $this->options = new EnzymesOption(self::PREFIX);
    293282    }
    294283
     
    517506    function execute_code( $code, $arguments, $post_object )
    518507    {
    519         if ( author_can($post_object, self::PREFIX . 'create_dynamic_custom_fields') &&
     508        if ( author_can($post_object, EnzymesCapabilities::create_dynamic_custom_fields) &&
    520509             ($this->belongs_to_current_author($post_object) ||
    521               author_can($post_object, self::PREFIX . 'share_dynamic_custom_fields') &&
    522               author_can($this->current_post, self::PREFIX . 'use_others_custom_fields'))
     510              author_can($post_object, EnzymesCapabilities::share_dynamic_custom_fields) &&
     511              author_can($this->current_post, EnzymesCapabilities::use_others_custom_fields))
    523512        ) {
    524513            list($result,) = $this->safe_eval($code, $arguments);
     
    635624    function transclude_code( $code, $post_object )
    636625    {
    637         if ( author_can($post_object, self::PREFIX . 'create_dynamic_custom_fields') &&
     626        if ( author_can($post_object, EnzymesCapabilities::create_dynamic_custom_fields) &&
    638627             ($this->belongs_to_current_author($post_object) ||
    639               author_can($post_object, self::PREFIX . 'share_dynamic_custom_fields') &&
    640               author_can($this->current_post, self::PREFIX . 'use_others_custom_fields'))
     628              author_can($post_object, EnzymesCapabilities::share_dynamic_custom_fields) &&
     629              author_can($this->current_post, EnzymesCapabilities::use_others_custom_fields))
    641630        ) {
    642631            list(, $output) = $this->safe_eval(" ?>$code<?php ");
    643         } elseif ( author_can($post_object, self::PREFIX . 'create_static_custom_fields') &&
     632        } elseif ( author_can($post_object, EnzymesCapabilities::create_static_custom_fields) &&
    644633                   ($this->belongs_to_current_author($post_object) ||
    645                     author_can($post_object, self::PREFIX . 'share_static_custom_fields') &&
    646                     author_can($this->current_post, self::PREFIX . 'use_others_custom_fields'))
     634                    author_can($post_object, EnzymesCapabilities::share_static_custom_fields) &&
     635                    author_can($this->current_post, EnzymesCapabilities::use_others_custom_fields))
    647636        ) {
    648637            $output = $code;
     
    710699        $this->debug_print('transcluding post_attr');
    711700        $same_author = $this->belongs_to_current_author($post_object);
    712         if ( $same_author && author_can($post_object, self::PREFIX . 'use_own_attributes') ||
    713              ! $same_author && author_can($this->current_post, self::PREFIX . 'use_others_attributes')
     701        if ( $same_author && author_can($post_object, EnzymesCapabilities::use_own_attributes) ||
     702             ! $same_author && author_can($this->current_post, EnzymesCapabilities::use_others_attributes)
    714703        ) {
    715704            $expression = $this->grammar['post_attr']->wrapper_set('@@')
     
    737726        $this->debug_print('transcluding author_attr');
    738727        $same_author = $this->belongs_to_current_author($post_object);
    739         if ( $same_author && author_can($post_object, self::PREFIX . 'use_own_attributes') ||
    740              ! $same_author && author_can($this->current_post, self::PREFIX . 'use_others_attributes')
     728        if ( $same_author && author_can($post_object, EnzymesCapabilities::use_own_attributes) ||
     729             ! $same_author && author_can($this->current_post, EnzymesCapabilities::use_others_attributes)
    741730        ) {
    742731            $expression = $this->grammar['author_attr']->wrapper_set('@@')
     
    917906            return $content;
    918907        }
    919         if ( ! author_can($this->current_post, self::PREFIX . 'inject') ) {
     908        if ( ! author_can($this->current_post, EnzymesCapabilities::inject) ) {
    920909            return $content;
    921910        }
  • enzymes/trunk/src/EnzymesPlugin.php

    r1063182 r1063263  
    5353    function on_activation()
    5454    {
     55        self::add_plugin_options();
    5556        self::add_roles_and_capabilities();
    5657        return true;
     
    8283    //------------------------------------------------------------------------------------------------------------------
    8384
     85
     86    static protected
     87    function save_activation_time()
     88    {
     89//        self::engine()
     90    }
     91
     92    static protected
     93    function delete_activation_time()
     94    {
     95
     96    }
     97
    8498    static protected
    8599    function add_roles_and_capabilities()
    86100    {
    87         $caps = array_keys(Enzymes3::capabilities());
    88         $no_role_capabilities = array_fill_keys($caps, false);
     101        self::remove_roles_and_capabilities();
    89102//@formatter:off
    90         remove_role(Enzymes3::PREFIX . 'User');
    91         $user_role = add_role(
    92             Enzymes3::PREFIX . 'User', __('Enzymes User'), array_merge($no_role_capabilities, array(
    93                 Enzymes3::PREFIX . 'inject'                       => true,
    94                 Enzymes3::PREFIX . 'use_own_attributes'           => true,
    95                 Enzymes3::PREFIX . 'use_own_custom_fields'        => true,
    96                 Enzymes3::PREFIX . 'create_static_custom_fields'  => true,
    97         )));
    98 
    99         remove_role(Enzymes3::PREFIX . 'PrivilegedUser');
    100         $privileged_user_role = add_role(
    101             Enzymes3::PREFIX . 'PrivilegedUser', __('Enzymes Privileged User'), array_merge($user_role->capabilities, array(
    102                 Enzymes3::PREFIX . 'use_others_custom_fields'     => true,
    103         )));
    104 
    105         remove_role(Enzymes3::PREFIX . 'TrustedUser');
    106         $trusted_user_role = add_role(
    107             Enzymes3::PREFIX . 'TrustedUser', __('Enzymes Trusted User'), array_merge($privileged_user_role->capabilities, array(
    108                 Enzymes3::PREFIX . 'share_static_custom_fields'   => true,
    109         )));
    110 
    111         remove_role(Enzymes3::PREFIX . 'Coder');
    112         $coder_role = add_role(
    113             Enzymes3::PREFIX . 'Coder', __('Enzymes Coder'), array_merge($trusted_user_role->capabilities, array(
    114                 Enzymes3::PREFIX . 'create_dynamic_custom_fields' => true,
    115         )));
    116 
    117         remove_role(Enzymes3::PREFIX . 'TrustedCoder');
    118         $trusted_coder_role = add_role(
    119             Enzymes3::PREFIX . 'TrustedCoder', __('Enzymes Trusted Coder'), array_merge($coder_role->capabilities, array(
    120                 Enzymes3::PREFIX . 'share_dynamic_custom_fields'  => true,
    121         )));
     103        add_role(EnzymesCapabilities::User,           __('Enzymes User'),            EnzymesCapabilities::for_User() );
     104        add_role(EnzymesCapabilities::PrivilegedUser, __('Enzymes Privileged User'), EnzymesCapabilities::for_PrivilegedUser() );
     105        add_role(EnzymesCapabilities::TrustedUser,    __('Enzymes Trusted User'),    EnzymesCapabilities::for_TrustedUser() );
     106        add_role(EnzymesCapabilities::Coder,          __('Enzymes Coder'),           EnzymesCapabilities::for_Coder() );
     107        add_role(EnzymesCapabilities::TrustedCoder,   __('Enzymes Trusted Coder'),   EnzymesCapabilities::for_TrustedCoder() );
    122108//@formatter:on
    123109
    124110        global $wp_roles;
    125111        /* @var $wp_roles WP_Roles */
    126         foreach ($caps as $cap) {
     112
     113        foreach (EnzymesCapabilities::all() as $cap) {
    127114            $wp_roles->add_cap('administrator', $cap);
    128115        }
    129116    }
    130117
     118    /**
     119     * Remove roles and capabilities by prefix.
     120     */
    131121    static protected
    132122    function remove_roles_and_capabilities()
     
    141131        }
    142132
    143         $caps = array_keys(Enzymes3::capabilities());
    144         foreach ($caps as $cap) {
    145             $wp_roles->remove_cap('administrator', $cap);
     133        foreach (EnzymesCapabilities::all() as $cap) {
     134            if ( 0 === strpos($cap, Enzymes3::PREFIX) ) {
     135                $wp_roles->remove_cap('administrator', $cap);
     136            }
    146137        }
    147138    }
Note: See TracChangeset for help on using the changeset viewer.