Quelles sont les lunettes de prescription non http://belgiquepharmacie.be/ Unie de liberté de santé oxford

Une petite classe PHP abstraite pour aider la création de shortcodes dans WordPress.

Pour un exemple d’utilisation, se reporter au shortcode attachments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
 * Abstract class to create shortcodes in WordPress.
 *
 * @author Mehdi Kabab <http://pioupioum.fr/>
 * @copyright Copyright (C) 2010 Mehdi Kabab
 * @license http://www.gnu.org/licenses/gpl.html  GNU GPL version 3 or later
 * @version 1.0.0
 * @link http://pioupioum.fr/snippets/wordpress-create-shortcode-class.html
 */
abstract class PPM_Shortcode
{
    /**
     * Default options.
     *
     * @var array
     **/
    protected $_default_options = array();
 
    /**
     * Options after initialization of shortcode.
     *
     * @var ArrayObject
     */
    private $_options;
 
    /**
     * Tag name of shortcode.
     *
     * @var string
     **/
    private $_name = null;
 
    /**
     * Constructor.
     *
     * @param string $tag Shortcode tag to be searched in post content.
     */
    public function __construct($tag)
    {
        $this->setTagName($tag);
        $this->_options = new ArrayObject(array(),
                                          ArrayObject::ARRAY_AS_PROPS);
    }
 
    /**
     * Excecute the shortcode.
     *
     * Called by the shortcode API.
     *
     * @param array $attributes Attributes used in the call of the shortcode.
     * @param string $content The content of the shortcode. Default to null.
     * @return string
     */
    public function run($attributes, $content = null)
    {
        $this->setOptions(shortcode_atts($this->_default_options, $attributes));
 
        return $this->_process($attributes, $content);
    }
 
    /**
     * Check if the current shortcode tag already registered.
     *
     * @global array $shortcode_tags
     * @return boolean
     */
    public function isRegistered()
    {
        global $shortcode_tags;
 
        return array_key_exists($this->getTagName(), $shortcode_tags);
    }
 
    /**
     * Register the shortcode.
     *
     * @return PPM_Shortcode
     * @throws Exception If the shortcode already exists.
     */
    public function register()
    {
        if ($this->isRegistered()) {
            throw new Exception(sprintf('The shortcode `%s` already exists!',
                                        $this->getTagName()));
        }
 
        add_shortcode($this->getTagName(), array($this, 'run'));
 
        return $this;
    }
 
    /**
     * Unregister the shortcode.
     *
     * @return PPM_Shortcode
     */
    public function unregister()
    {
        remove_shortcode($this->getTagName());
 
        return $this;
    }
 
    /**
     * Retrieve the name of shortcode.
     *
     * @return string
     */
    public function getTagName()
    {
        return $this->_name;
    }
 
    /**
     * Set the name of shortcode.
     *
     * @param string $name Shortcode tag to be searched in post content.
     * @return PPM_Shortcode
     */
    public function setTagName($name)
    {
        $this->_name = $name;
 
        return $this;
    }
 
    /**
     * Retrieve the requested option.
     *
     * @param string $name The option to search.
     * @return mixed
     */
    public function getOption($name)
    {
        return $this->_options->offsetGet($name);
    }
 
    /**
     * Test if an option exists for the shortcode.
     *
     * @param string $name
     * @return boolean
     */
    public function hasOption($name)
    {
        return $this->_options->offsetExists($name);
    }
 
    /**
     * Set an option.
     *
     * @param string $name
     * @param mixed $value
     * @return PPM_Shortcode
     */
    public function setOption($name, $value)
    {
        $this->_options->offsetSet($name, $value);
 
        return $this;
    }
 
    /**
     * Retrieve the options.
     *
     * @return ArrayObject
     */
    public function getOptions()
    {
        return $this->_options;
    }
 
    /**
     * Set the options.
     *
     * @param array $options
     * @return PPM_Shortcode
     */
    public function setOptions($options)
    {
        foreach ((array) $options as $name => $value) {
            $this->setOption($name, $value);
        }
 
        return $this;
    }
 
    /**
     * Concrete excecution of the shortcode.
     *
     * @param array $attributes Attributes used in the call of the shortcode.
     * @param string $content The content of the shortcode. Default to null.
     * @return string
     */
    abstract protected function _process($attributes, $content = null);
 
}
?>
publicité (chargement)

2 réponses pour WordPress : une classe abstraite pour aider la création de shortcodes

  1. mika dit :

    Une petite explication sur “Comment ça s’utilise ?” …

  2. Blog s..e dit :

    +1 , auriez-vous un petit exemple a fournir avec cette classe

Ajouter un commentaire


Syndication

Réseaux sociaux