| Conditions | 1 |
| Paths | 1 |
| Total Lines | 323 |
| Code Lines | 199 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 109 | function yourprefix_register_demo_metabox() {
|
||
| 110 | $prefix = 'yourprefix_demo_'; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Sample metabox to demonstrate each field type included |
||
| 114 | */ |
||
| 115 | $cmb_demo = new_cmb2_box( array( |
||
| 116 | 'id' => $prefix . 'metabox', |
||
| 117 | 'title' => esc_html__( 'Test Metabox', 'cmb2' ), |
||
| 118 | 'object_types' => array( 'page', ), // Post type |
||
| 119 | // 'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value |
||
| 120 | // 'context' => 'normal', |
||
| 121 | // 'priority' => 'high', |
||
| 122 | // 'show_names' => true, // Show field names on the left |
||
| 123 | // 'cmb_styles' => false, // false to disable the CMB stylesheet |
||
| 124 | // 'closed' => true, // true to keep the metabox closed by default |
||
| 125 | // 'classes' => 'extra-class', // Extra cmb2-wrap classes |
||
| 126 | // 'classes_cb' => 'yourprefix_add_some_classes', // Add classes through a callback. |
||
| 127 | // 'show_in_rest' => WP_REST_Server::READABLE|WP_REST_Server::ALLMETHODS|WP_REST_Server::EDITABLE, // Determines which HTTP methods the box is visible in. More here: https://github.com/WebDevStudios/CMB2/wiki/REST-API |
||
| 128 | ) ); |
||
| 129 | |||
| 130 | $cmb_demo->add_field( array( |
||
| 131 | 'name' => esc_html__( 'Test Text', 'cmb2' ), |
||
| 132 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 133 | 'id' => $prefix . 'text', |
||
| 134 | 'type' => 'text', |
||
| 135 | 'show_on_cb' => 'yourprefix_hide_if_no_cats', // function should return a bool value |
||
| 136 | // 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter |
||
| 137 | // 'escape_cb' => 'my_custom_escaping', // custom escaping callback parameter |
||
| 138 | // 'on_front' => false, // Optionally designate a field to wp-admin only |
||
| 139 | // 'repeatable' => true, |
||
| 140 | // 'column' => true, // Display field value in the admin post-listing columns |
||
| 141 | // 'show_in_rest' => WP_REST_Server::READABLE|WP_REST_Server::ALLMETHODS|WP_REST_Server::EDITABLE, // Determines which HTTP methods the field is visible in. Will override the cmb2_box 'show_in_rest' param. More here: https://github.com/WebDevStudios/CMB2/wiki/REST-API |
||
| 142 | ) ); |
||
| 143 | |||
| 144 | $cmb_demo->add_field( array( |
||
| 145 | 'name' => esc_html__( 'Test Text Small', 'cmb2' ), |
||
| 146 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 147 | 'id' => $prefix . 'textsmall', |
||
| 148 | 'type' => 'text_small', |
||
| 149 | // 'repeatable' => true, |
||
| 150 | // 'column' => array( |
||
| 151 | // 'name' => esc_html__( 'Column Title', 'cmb2' ), // Set the admin column title |
||
| 152 | // 'position' => 2, // Set as the second column. |
||
| 153 | // ); |
||
| 154 | // 'display_cb' => 'yourprefix_display_text_small_column', // Output the display of the column values through a callback. |
||
| 155 | ) ); |
||
| 156 | |||
| 157 | $cmb_demo->add_field( array( |
||
| 158 | 'name' => esc_html__( 'Test Text Medium', 'cmb2' ), |
||
| 159 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 160 | 'id' => $prefix . 'textmedium', |
||
| 161 | 'type' => 'text_medium', |
||
| 162 | ) ); |
||
| 163 | |||
| 164 | $cmb_demo->add_field( array( |
||
| 165 | 'name' => esc_html__( 'Read-only Disabled Field', 'cmb2' ), |
||
| 166 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 167 | 'id' => $prefix . 'readonly', |
||
| 168 | 'type' => 'text_medium', |
||
| 169 | 'default' => esc_attr__( 'Hey there, I\'m a read-only field', 'cmb2' ), |
||
| 170 | 'save_field' => false, // Disables the saving of this field. |
||
| 171 | 'attributes' => array( |
||
| 172 | 'disabled' => 'disabled', |
||
| 173 | 'readonly' => 'readonly', |
||
| 174 | ), |
||
| 175 | ) ); |
||
| 176 | |||
| 177 | $cmb_demo->add_field( array( |
||
| 178 | 'name' => esc_html__( 'Custom Rendered Field', 'cmb2' ), |
||
| 179 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 180 | 'id' => $prefix . 'render_row_cb', |
||
| 181 | 'type' => 'text', |
||
| 182 | 'render_row_cb' => 'yourprefix_render_row_cb', |
||
| 183 | ) ); |
||
| 184 | |||
| 185 | $cmb_demo->add_field( array( |
||
| 186 | 'name' => esc_html__( 'Website URL', 'cmb2' ), |
||
| 187 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 188 | 'id' => $prefix . 'url', |
||
| 189 | 'type' => 'text_url', |
||
| 190 | // 'protocols' => array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'), // Array of allowed protocols
|
||
| 191 | // 'repeatable' => true, |
||
| 192 | ) ); |
||
| 193 | |||
| 194 | $cmb_demo->add_field( array( |
||
| 195 | 'name' => esc_html__( 'Test Text Email', 'cmb2' ), |
||
| 196 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 197 | 'id' => $prefix . 'email', |
||
| 198 | 'type' => 'text_email', |
||
| 199 | // 'repeatable' => true, |
||
| 200 | ) ); |
||
| 201 | |||
| 202 | $cmb_demo->add_field( array( |
||
| 203 | 'name' => esc_html__( 'Test Time', 'cmb2' ), |
||
| 204 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 205 | 'id' => $prefix . 'time', |
||
| 206 | 'type' => 'text_time', |
||
| 207 | // 'time_format' => 'H:i', // Set to 24hr format |
||
| 208 | ) ); |
||
| 209 | |||
| 210 | $cmb_demo->add_field( array( |
||
| 211 | 'name' => esc_html__( 'Time zone', 'cmb2' ), |
||
| 212 | 'desc' => esc_html__( 'Time zone', 'cmb2' ), |
||
| 213 | 'id' => $prefix . 'timezone', |
||
| 214 | 'type' => 'select_timezone', |
||
| 215 | ) ); |
||
| 216 | |||
| 217 | $cmb_demo->add_field( array( |
||
| 218 | 'name' => esc_html__( 'Test Date Picker', 'cmb2' ), |
||
| 219 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 220 | 'id' => $prefix . 'textdate', |
||
| 221 | 'type' => 'text_date', |
||
| 222 | // 'date_format' => 'Y-m-d', |
||
| 223 | ) ); |
||
| 224 | |||
| 225 | $cmb_demo->add_field( array( |
||
| 226 | 'name' => esc_html__( 'Test Date Picker (UNIX timestamp)', 'cmb2' ), |
||
| 227 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 228 | 'id' => $prefix . 'textdate_timestamp', |
||
| 229 | 'type' => 'text_date_timestamp', |
||
| 230 | // 'timezone_meta_key' => $prefix . 'timezone', // Optionally make this field honor the timezone selected in the select_timezone specified above |
||
| 231 | ) ); |
||
| 232 | |||
| 233 | $cmb_demo->add_field( array( |
||
| 234 | 'name' => esc_html__( 'Test Date/Time Picker Combo (UNIX timestamp)', 'cmb2' ), |
||
| 235 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 236 | 'id' => $prefix . 'datetime_timestamp', |
||
| 237 | 'type' => 'text_datetime_timestamp', |
||
| 238 | ) ); |
||
| 239 | |||
| 240 | // This text_datetime_timestamp_timezone field type |
||
| 241 | // is only compatible with PHP versions 5.3 or above. |
||
| 242 | // Feel free to uncomment and use if your server meets the requirement |
||
| 243 | // $cmb_demo->add_field( array( |
||
| 244 | // 'name' => esc_html__( 'Test Date/Time Picker/Time zone Combo (serialized DateTime object)', 'cmb2' ), |
||
| 245 | // 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 246 | // 'id' => $prefix . 'datetime_timestamp_timezone', |
||
| 247 | // 'type' => 'text_datetime_timestamp_timezone', |
||
| 248 | // ) ); |
||
| 249 | |||
| 250 | $cmb_demo->add_field( array( |
||
| 251 | 'name' => esc_html__( 'Test Money', 'cmb2' ), |
||
| 252 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 253 | 'id' => $prefix . 'textmoney', |
||
| 254 | 'type' => 'text_money', |
||
| 255 | // 'before_field' => '£', // override '$' symbol if needed |
||
| 256 | // 'repeatable' => true, |
||
| 257 | ) ); |
||
| 258 | |||
| 259 | $cmb_demo->add_field( array( |
||
| 260 | 'name' => esc_html__( 'Test Color Picker', 'cmb2' ), |
||
| 261 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 262 | 'id' => $prefix . 'colorpicker', |
||
| 263 | 'type' => 'colorpicker', |
||
| 264 | 'default' => '#ffffff', |
||
| 265 | // 'attributes' => array( |
||
| 266 | // 'data-colorpicker' => json_encode( array( |
||
| 267 | // 'palettes' => array( '#3dd0cc', '#ff834c', '#4fa2c0', '#0bc991', ), |
||
| 268 | // ) ), |
||
| 269 | // ), |
||
| 270 | ) ); |
||
| 271 | |||
| 272 | $cmb_demo->add_field( array( |
||
| 273 | 'name' => esc_html__( 'Test Text Area', 'cmb2' ), |
||
| 274 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 275 | 'id' => $prefix . 'textarea', |
||
| 276 | 'type' => 'textarea', |
||
| 277 | ) ); |
||
| 278 | |||
| 279 | $cmb_demo->add_field( array( |
||
| 280 | 'name' => esc_html__( 'Test Text Area Small', 'cmb2' ), |
||
| 281 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 282 | 'id' => $prefix . 'textareasmall', |
||
| 283 | 'type' => 'textarea_small', |
||
| 284 | ) ); |
||
| 285 | |||
| 286 | $cmb_demo->add_field( array( |
||
| 287 | 'name' => esc_html__( 'Test Text Area for Code', 'cmb2' ), |
||
| 288 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 289 | 'id' => $prefix . 'textarea_code', |
||
| 290 | 'type' => 'textarea_code', |
||
| 291 | ) ); |
||
| 292 | |||
| 293 | $cmb_demo->add_field( array( |
||
| 294 | 'name' => esc_html__( 'Test Title Weeeee', 'cmb2' ), |
||
| 295 | 'desc' => esc_html__( 'This is a title description', 'cmb2' ), |
||
| 296 | 'id' => $prefix . 'title', |
||
| 297 | 'type' => 'title', |
||
| 298 | ) ); |
||
| 299 | |||
| 300 | $cmb_demo->add_field( array( |
||
| 301 | 'name' => esc_html__( 'Test Select', 'cmb2' ), |
||
| 302 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 303 | 'id' => $prefix . 'select', |
||
| 304 | 'type' => 'select', |
||
| 305 | 'show_option_none' => true, |
||
| 306 | 'options' => array( |
||
| 307 | 'standard' => esc_html__( 'Option One', 'cmb2' ), |
||
| 308 | 'custom' => esc_html__( 'Option Two', 'cmb2' ), |
||
| 309 | 'none' => esc_html__( 'Option Three', 'cmb2' ), |
||
| 310 | ), |
||
| 311 | ) ); |
||
| 312 | |||
| 313 | $cmb_demo->add_field( array( |
||
| 314 | 'name' => esc_html__( 'Test Radio inline', 'cmb2' ), |
||
| 315 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 316 | 'id' => $prefix . 'radio_inline', |
||
| 317 | 'type' => 'radio_inline', |
||
| 318 | 'show_option_none' => 'No Selection', |
||
| 319 | 'options' => array( |
||
| 320 | 'standard' => esc_html__( 'Option One', 'cmb2' ), |
||
| 321 | 'custom' => esc_html__( 'Option Two', 'cmb2' ), |
||
| 322 | 'none' => esc_html__( 'Option Three', 'cmb2' ), |
||
| 323 | ), |
||
| 324 | ) ); |
||
| 325 | |||
| 326 | $cmb_demo->add_field( array( |
||
| 327 | 'name' => esc_html__( 'Test Radio', 'cmb2' ), |
||
| 328 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 329 | 'id' => $prefix . 'radio', |
||
| 330 | 'type' => 'radio', |
||
| 331 | 'options' => array( |
||
| 332 | 'option1' => esc_html__( 'Option One', 'cmb2' ), |
||
| 333 | 'option2' => esc_html__( 'Option Two', 'cmb2' ), |
||
| 334 | 'option3' => esc_html__( 'Option Three', 'cmb2' ), |
||
| 335 | ), |
||
| 336 | ) ); |
||
| 337 | |||
| 338 | $cmb_demo->add_field( array( |
||
| 339 | 'name' => esc_html__( 'Test Taxonomy Radio', 'cmb2' ), |
||
| 340 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 341 | 'id' => $prefix . 'text_taxonomy_radio', |
||
| 342 | 'type' => 'taxonomy_radio', |
||
| 343 | 'taxonomy' => 'category', // Taxonomy Slug |
||
| 344 | // 'inline' => true, // Toggles display to inline |
||
| 345 | ) ); |
||
| 346 | |||
| 347 | $cmb_demo->add_field( array( |
||
| 348 | 'name' => esc_html__( 'Test Taxonomy Select', 'cmb2' ), |
||
| 349 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 350 | 'id' => $prefix . 'taxonomy_select', |
||
| 351 | 'type' => 'taxonomy_select', |
||
| 352 | 'taxonomy' => 'category', // Taxonomy Slug |
||
| 353 | ) ); |
||
| 354 | |||
| 355 | $cmb_demo->add_field( array( |
||
| 356 | 'name' => esc_html__( 'Test Taxonomy Multi Checkbox', 'cmb2' ), |
||
| 357 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 358 | 'id' => $prefix . 'multitaxonomy', |
||
| 359 | 'type' => 'taxonomy_multicheck', |
||
| 360 | 'taxonomy' => 'post_tag', // Taxonomy Slug |
||
| 361 | // 'inline' => true, // Toggles display to inline |
||
| 362 | ) ); |
||
| 363 | |||
| 364 | $cmb_demo->add_field( array( |
||
| 365 | 'name' => esc_html__( 'Test Checkbox', 'cmb2' ), |
||
| 366 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 367 | 'id' => $prefix . 'checkbox', |
||
| 368 | 'type' => 'checkbox', |
||
| 369 | ) ); |
||
| 370 | |||
| 371 | $cmb_demo->add_field( array( |
||
| 372 | 'name' => esc_html__( 'Test Multi Checkbox', 'cmb2' ), |
||
| 373 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 374 | 'id' => $prefix . 'multicheckbox', |
||
| 375 | 'type' => 'multicheck', |
||
| 376 | // 'multiple' => true, // Store values in individual rows |
||
| 377 | 'options' => array( |
||
| 378 | 'check1' => esc_html__( 'Check One', 'cmb2' ), |
||
| 379 | 'check2' => esc_html__( 'Check Two', 'cmb2' ), |
||
| 380 | 'check3' => esc_html__( 'Check Three', 'cmb2' ), |
||
| 381 | ), |
||
| 382 | // 'inline' => true, // Toggles display to inline |
||
| 383 | ) ); |
||
| 384 | |||
| 385 | $cmb_demo->add_field( array( |
||
| 386 | 'name' => esc_html__( 'Test wysiwyg', 'cmb2' ), |
||
| 387 | 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
||
| 388 | 'id' => $prefix . 'wysiwyg', |
||
| 389 | 'type' => 'wysiwyg', |
||
| 390 | 'options' => array( 'textarea_rows' => 5, ), |
||
| 391 | ) ); |
||
| 392 | |||
| 393 | $cmb_demo->add_field( array( |
||
| 394 | 'name' => esc_html__( 'Test Image', 'cmb2' ), |
||
| 395 | 'desc' => esc_html__( 'Upload an image or enter a URL.', 'cmb2' ), |
||
| 396 | 'id' => $prefix . 'image', |
||
| 397 | 'type' => 'file', |
||
| 398 | ) ); |
||
| 399 | |||
| 400 | $cmb_demo->add_field( array( |
||
| 401 | 'name' => esc_html__( 'Multiple Files', 'cmb2' ), |
||
| 402 | 'desc' => esc_html__( 'Upload or add multiple images/attachments.', 'cmb2' ), |
||
| 403 | 'id' => $prefix . 'file_list', |
||
| 404 | 'type' => 'file_list', |
||
| 405 | 'preview_size' => array( 100, 100 ), // Default: array( 50, 50 ) |
||
| 406 | ) ); |
||
| 407 | |||
| 408 | $cmb_demo->add_field( array( |
||
| 409 | 'name' => esc_html__( 'oEmbed', 'cmb2' ), |
||
| 410 | 'desc' => sprintf( |
||
| 411 | /* translators: %s: link to codex.wordpress.org/Embeds */ |
||
| 412 | esc_html__( 'Enter a youtube, twitter, or instagram URL. Supports services listed at %s.', 'cmb2' ), |
||
| 413 | '<a href="https://codex.wordpress.org/Embeds">codex.wordpress.org/Embeds</a>' |
||
| 414 | ), |
||
| 415 | 'id' => $prefix . 'embed', |
||
| 416 | 'type' => 'oembed', |
||
| 417 | ) ); |
||
| 418 | |||
| 419 | $cmb_demo->add_field( array( |
||
| 420 | 'name' => 'Testing Field Parameters', |
||
| 421 | 'id' => $prefix . 'parameters', |
||
| 422 | 'type' => 'text', |
||
| 423 | 'before_row' => 'yourprefix_before_row_if_2', // callback |
||
| 424 | 'before' => '<p>Testing <b>"before"</b> parameter</p>', |
||
| 425 | 'before_field' => '<p>Testing <b>"before_field"</b> parameter</p>', |
||
| 426 | 'after_field' => '<p>Testing <b>"after_field"</b> parameter</p>', |
||
| 427 | 'after' => '<p>Testing <b>"after"</b> parameter</p>', |
||
| 428 | 'after_row' => '<p>Testing <b>"after_row"</b> parameter</p>', |
||
| 429 | ) ); |
||
| 430 | |||
| 431 | } |
||
| 432 | |||
| 675 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.