Skip to content

Commit

Permalink
Merge pull request #3 from pmichelazzo/develop
Browse files Browse the repository at this point in the history
Merge the last changes into the main branch
  • Loading branch information
pmichelazzo authored Aug 25, 2021
2 parents f8b7d7b + e7e6ba2 commit dd903a5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
## What is this?
This class is used to create meta boxes with fields inside of any WordPress
content type (custom or not) without the hard job to code the meta boxes and
fields. to do that, you just need to call the class, set some parameters for
the meta boxes, write the fields passing some parameters and, that's it, the
job is done!
fields and without dependencies from external code/plugins. To use, you just
need to call the class, set some parameters for the meta boxes, write the fields
passing some parameters and the job is done!

## Why another class/plugin/code/whatever to handle meta boxes
Well, why do we have so many different cars models? Why we have so many different
Expand All @@ -15,7 +15,16 @@ There's a lot of options to work with meta boxes around. The WordPress ecosystem
is huge and every day we find a new code to solve WordPress problems (IMHO, the
WP content types is one of the worst).

This class solved a need that I had and I decide to share. Feel free to use it
But, there is a big difference between this class and plugins that you can find
on the web: **dependencies**. In other words, you don't need to install another
plugin to create meta boxes with fields and your custom content type **is not**
dependent of anything else. On this way, you can share your custom content type
or plugin for someone and everything you need is here: datepickers, colorpickers,
fields, nice selection boxes, etc.
No additional code, no additional plugins, nothing. Just a class you call from
your code. That's it.

This class solved a problem that I had and I decide to share. Feel free to use it
or, perhaps, find a better or beautiful option. It's up to you :)

## The history behind the code
Expand All @@ -33,7 +42,7 @@ I love documentation (inside and outside the code). You don't need to search the
code to find parameters or something else. Goes to the [wiki](https://github.com/pmichelazzo/easy-wp-metabox/wiki)
and you'll find everything there :)

## To Do, new features and issues
## To do, new features and issues
You can check the plans for this code on the [Project dashboard](https://github.com/pmichelazzo/easy-wp-metabox/projects/1).
I always listen the user's needs and try to do my best to add new features in
the code.
Expand All @@ -43,5 +52,5 @@ directly by [email]([email protected]).

Have fun!

*Release: 1.0*<br/>
*Last main release: 1.0*<br/>
*Date: 01 September 2021*
File renamed without changes.
38 changes: 22 additions & 16 deletions easy-wp-metabox-class.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ public function load_scripts_styles() {
global $typenow;
if (in_array($typenow,$this->_meta_box['pages']) && $this->is_edit_page()) {
// Enqueue Meta Box Style
wp_enqueue_style('at-meta-box', $plugin_path . '/css/meta-box.css');
wp_enqueue_style('easy-wp-metabox', $plugin_path . '/css/easy-wp-metabox.css');
// Enqueue Meta Box Scripts
wp_enqueue_script('at-meta-box', $plugin_path . '/js/meta-box.js', ['jquery'], null, true);
wp_enqueue_script('easy-wp-metabox', $plugin_path . '/js/easy-wp-metabox.js', ['jquery'], null, true);
wp_enqueue_script('repeater', $plugin_path . '/js/repeater.js', ['jquery'], null, true);
// Make upload feature work event when custom post type doesn't support 'editor'
if ($this->has_field('image') || $this->has_field('file')) {
Expand Down Expand Up @@ -423,7 +423,7 @@ public function show_field_repeater($field, $data) {
print '</div>';
}
$this->show_field_end($field);
print '<script>jQuery(function($){$("#repeater").createRepeater({showFirstItemToDefault: true,});});</script>';
print '<script>jQuery(function($){$("#repeater").createRepeater({showFirstItemToDefault:true});});</script>';
}

/**
Expand Down Expand Up @@ -508,7 +508,7 @@ public function show_field_text($field, $data) {
];
// Print the field.
$this->show_field_begin($field);
print '<input type="text" ' . implode(' ', $attributes) . '/>';
print '<input type="text" ' . implode(' ', $attributes) . '>';
$this->show_field_end($field);
}

Expand All @@ -522,13 +522,19 @@ public function show_field_text($field, $data) {
* @param string $data
*/
public function show_field_number($field, $data) {
// Prepare the field attributes.
$attributes = [
'id' => 'id="' . $field['id'] . '"',
'data' => 'data-name="' . $field['id'] . '"',
'min' => 'min="' . $field['min'] . '"',
'max' => 'max="' . $field['max'] . '"',
'step' => 'step="' . $field['step'] . '"',
'value' => 'value="' . $data . '"',
'class' => $this->field_classes($field),
];
// Print the field.
$this->show_field_begin($field);
$id = $field['id'];
$min = (isset($field['min'])) ? 'min="' . $field['min'] . '"' : '';
$max = (isset($field['max'])) ? 'max="' . $field['max'] . '"' : '';
$step = ($field['step'] != 1) ? 'step="' . $field['step'] . '"' : '';
$class = $this->field_classes($field);
print '<input type="number"' . $class . ' name="' . $id . '" id="' . $id . '" value="' . $data . '"' . $step . $min . $max .'/>';
print '<input type="number" ' . implode(' ', $attributes) . '>';
$this->show_field_end($field);
}

Expand Down Expand Up @@ -958,11 +964,11 @@ public function show_field_cond($field, $data) {
public function save($post_id) {
global $post_type;
$post_type_object = get_post_type_object($post_type);
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) // Check Autosave
|| ( ! isset( $_POST['post_ID'] ) || $post_id != $_POST['post_ID'] ) // Check Revision
|| ( ! in_array( $post_type, $this->_meta_box['pages'] ) ) // Check if current post type is supported.
|| ( ! check_admin_referer( basename( __FILE__ ), 'at_meta_box_nonce') ) // Check nonce - Security
|| ( ! current_user_can( $post_type_object->cap->edit_post, $post_id ) ) ) // Check permission
if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) // Check Autosave
|| (!isset($_POST['post_ID']) || $post_id != $_POST['post_ID']) // Check Revision
|| (!in_array($post_type, $this->_meta_box['pages'])) // Check if current post type is supported.
|| (!check_admin_referer(basename(__FILE__), 'easy_meta_box_nonce')) // Check nonce - Security
|| (!current_user_can($post_type_object->cap->edit_post, $post_id))) // Check permission
{
return $post_id;
}
Expand Down Expand Up @@ -1315,7 +1321,7 @@ public function addNumber($id, $args, $repeater = false) {
'name' => 'Number Field',
'desc' => '',
'std' => 0,
'min' => '0',
'min' => 0,
'max' => '',
'step' => 1,
'side' => 0,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion js/repeater.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jQuery.fn.extend({
var itemContent = items;
var group = itemContent.data("group");
var item = itemContent;
var input = item.find('input,select,textarea');
var input = item.find('input,select,textarea,number');

input.each(function (index, el) {
var attrName = $(el).data('name');
Expand Down

0 comments on commit dd903a5

Please sign in to comment.