Plugin Directory

Changeset 1062328


Ignore:
Timestamp:
01/07/2015 03:59:07 PM (11 years ago)
Author:
aercolino
Message:

Improved plugin compliance with WordPress best practices (e.g. add roles on activation, remove roles on deactivation).
All green tests.

Location:
enzymes/trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • enzymes/trunk/EnzymesPlugin.php

    r1061280 r1062328  
    11<?php
     2require_once 'lib/Enzymes3.php';
    23
    34class EnzymesPlugin
     
    1819    {
    1920        if ( is_null(self::$enzymes) ) {
    20             require_once 'lib/Enzymes3.php';
    2121            self::$enzymes = new Enzymes3();
    2222        }
     
    2626    public
    2727    function __construct()
     28    {
     29        register_activation_hook(ENZYMES_FILENAME, array('EnzymesPlugin', 'on_activation'));
     30        register_deactivation_hook(ENZYMES_FILENAME, array('EnzymesPlugin', 'on_deactivation'));
     31
     32        add_action('init', array('EnzymesPlugin', 'on_init'), 10, 2);
     33    }
     34
     35    static public
     36    function on_init()
    2837    {
    2938        $enzymes = self::engine();  // pointer to the singleton
     
    3645    }
    3746
     47    /**
     48     * Callback used when the plugin is activated by the user.
     49     *
     50     * @return boolean
     51     */
     52    static public
     53    function on_activation()
     54    {
     55        self::add_roles_and_capabilities();
     56        return true;
     57    }
     58
     59    /**
     60     * Callback used when the plugin is deactivated by the user.
     61     *
     62     * @return boolean
     63     */
     64    static public
     65    function on_deactivation()
     66    {
     67        self::remove_roles_and_capabilities();
     68        return true;
     69    }
     70
     71    /**
     72     * Uninstalls this plugin, cleaning up all data.
     73     * This is called from uninstall.php without instantiating an object of this class.
     74     *
     75     */
     76    static public
     77    function uninstall()
     78    {
     79
     80    }
     81
     82    //------------------------------------------------------------------------------------------------------------------
     83
     84    static protected
     85    function add_roles_and_capabilities()
     86    {
     87        $caps = array_keys(Enzymes3::capabilities());
     88        $no_role_capabilities = array_fill_keys($caps, false);
     89//@formatter:off
     90        remove_role('enzymes.User');
     91        $user_role = add_role(
     92            'enzymes.User', __('Enzymes User'), array_merge($no_role_capabilities, array(
     93                'enzymes.inject'                       => true,
     94                'enzymes.use_own_attributes'           => true,
     95                'enzymes.use_own_custom_fields'        => true,
     96                'enzymes.create_static_custom_fields'  => true,
     97        )));
     98
     99        remove_role('enzymes.PrivilegedUser');
     100        $privileged_user_role = add_role(
     101            'enzymes.PrivilegedUser', __('Enzymes Privileged User'), array_merge($user_role->capabilities, array(
     102                'enzymes.use_others_custom_fields'     => true,
     103        )));
     104
     105        remove_role('enzymes.TrustedUser');
     106        $trusted_user_role = add_role(
     107            'enzymes.TrustedUser', __('Enzymes Trusted User'), array_merge($privileged_user_role->capabilities, array(
     108                'enzymes.share_static_custom_fields'   => true,
     109        )));
     110
     111        remove_role('enzymes.Coder');
     112        $coder_role = add_role(
     113            'enzymes.Coder', __('Enzymes Coder'), array_merge($trusted_user_role->capabilities, array(
     114                'enzymes.create_dynamic_custom_fields' => true,
     115        )));
     116
     117        remove_role('enzymes.TrustedCoder');
     118        $trusted_coder_role = add_role(
     119            'enzymes.TrustedCoder', __('Enzymes Trusted Coder'), array_merge($coder_role->capabilities, array(
     120                'enzymes.share_dynamic_custom_fields'  => true,
     121        )));
     122//@formatter:on
     123
     124        global $wp_roles;
     125        /* @var $wp_roles WP_Roles */
     126        foreach ($caps as $cap) {
     127            $wp_roles->add_cap('administrator', $cap);
     128        }
     129    }
     130
     131    static protected
     132    function remove_roles_and_capabilities()
     133    {
     134        global $wp_roles;
     135        /* @var $wp_roles WP_Roles */
     136
     137        foreach ($wp_roles->roles as $name => $role) {
     138            if ( 0 === strpos($name, 'enzymes.') ) {
     139                remove_role($name);
     140            }
     141        }
     142
     143        $caps = array_keys(Enzymes3::capabilities());
     144        foreach ($caps as $cap) {
     145            $wp_roles->remove_cap('administrator', $cap);
     146        }
     147    }
     148
    38149}
  • enzymes/trunk/enzymes.php

    r1054200 r1062328  
    1313require 'EnzymesPlugin.php';
    1414
    15 //just comment the following line if you need to temporarily disable this plugin
    1615$enzymesPlugin = new EnzymesPlugin();
  • enzymes/trunk/lib/Enzymes3.php

    r1062172 r1062328  
    55class Enzymes3
    66{
     7    static public
     8    function capabilities() {
     9        $result = array(
     10                'enzymes.inject'                       => 'It allows a user to inject enzymes into her posts.',
     11                'enzymes.use_own_attributes'           => 'It allows a user to make her enzymes with her own attributes.',
     12                'enzymes.use_others_attributes'        => 'It allows a user to make her enzymes with others\' attributes.',
     13                'enzymes.use_own_custom_fields'        => 'It allows a user to make her enzymes with her own custom fields.',
     14                'enzymes.use_others_custom_fields'     => 'It allows a user to make her enzymes with others\' custom fields.',
     15                'enzymes.create_static_custom_fields'  => 'It allows a user to create enzymes from non-evaluated custom fields.',
     16                'enzymes.create_dynamic_custom_fields' => 'It allows a user to create enzymes from evaluated custom fields.',
     17                'enzymes.share_static_custom_fields'   => 'It allows a user to share her enzymes from non-evaluated custom fields.',
     18                'enzymes.share_dynamic_custom_fields'  => 'It allows a user to share her enzymes from evaluated custom fields.',
     19        );
     20        return $result;
     21    }
     22
    723    /**
    824     * @var bool
     
    261277        $this->init_grammar();
    262278        $this->init_expressions();
    263         $this->add_roles_and_capabilities();
    264279    }
    265280
     
    912927        return $result;
    913928    }
    914 
    915     /**
    916      * Add capabilities.
    917      */
    918     protected
    919     function add_roles_and_capabilities()
    920     {
    921         $capabilities = array(
    922                 'enzymes.inject'                       => 'It allows a user to inject enzymes into her posts.',
    923                 'enzymes.use_own_attributes'           => 'It allows a user to make her enzymes with her own attributes.',
    924                 'enzymes.use_others_attributes'        => 'It allows a user to make her enzymes with others\' attributes.',
    925                 'enzymes.use_own_custom_fields'        => 'It allows a user to make her enzymes with her own custom fields.',
    926                 'enzymes.use_others_custom_fields'     => 'It allows a user to make her enzymes with others\' custom fields.',
    927                 'enzymes.create_static_custom_fields'  => 'It allows a user to create enzymes from non-evaluated custom fields.',
    928                 'enzymes.create_dynamic_custom_fields' => 'It allows a user to create enzymes from evaluated custom fields.',
    929                 'enzymes.share_static_custom_fields'   => 'It allows a user to share her enzymes from non-evaluated custom fields.',
    930                 'enzymes.share_dynamic_custom_fields'  => 'It allows a user to share her enzymes from evaluated custom fields.',
    931         );
    932 
    933         remove_role('enzymes.User');
    934         $user_role = add_role('enzymes.User', __('Enzymes User'), array(
    935                 'enzymes.inject'                       => true,
    936                 'enzymes.use_own_attributes'           => true,
    937                 'enzymes.use_others_attributes'        => false,
    938                 'enzymes.use_own_custom_fields'        => true,
    939                 'enzymes.use_others_custom_fields'     => false,
    940                 'enzymes.create_static_custom_fields'  => true,
    941                 'enzymes.create_dynamic_custom_fields' => false,
    942                 'enzymes.share_static_custom_fields'   => false,
    943                 'enzymes.share_dynamic_custom_fields'  => false,
    944         ));
    945 
    946         remove_role('enzymes.PrivilegedUser');
    947         $privileged_user_role = add_role('enzymes.PrivilegedUser', __('Enzymes Privileged User'),
    948                                          array_merge($user_role->capabilities, array(
    949                                                  'enzymes.use_others_custom_fields' => true,
    950                                          )));
    951 
    952         remove_role('enzymes.TrustedUser');
    953         $trusted_user_role = add_role('enzymes.TrustedUser', __('Enzymes Trusted User'),
    954                                       array_merge($privileged_user_role->capabilities, array(
    955                                               'enzymes.share_static_custom_fields' => true,
    956                                       )));
    957 
    958         remove_role('enzymes.Coder');
    959         $coder_role = add_role('enzymes.Coder', __('Enzymes Coder'),
    960                                array_merge($trusted_user_role->capabilities, array(
    961                                        'enzymes.create_dynamic_custom_fields' => true,
    962                                )));
    963 
    964         remove_role('enzymes.TrustedCoder');
    965         $trusted_coder_role = add_role('enzymes.TrustedCoder', __('Enzymes Trusted Coder'),
    966                                        array_merge($coder_role->capabilities, array(
    967                                                'enzymes.share_dynamic_custom_fields' => true,
    968                                        )));
    969 
    970         global $wp_roles;
    971         /* @var $wp_roles WP_Roles */
    972         foreach (array_keys($user_role->capabilities) as $cap) {
    973             $wp_roles->add_cap('administrator', $cap);
    974         }
    975     }
    976929}
Note: See TracChangeset for help on using the changeset viewer.