Last active
July 18, 2022 07:49
-
-
Save runezero/e355b0ed94b200d6bf959a516135d3c1 to your computer and use it in GitHub Desktop.
[WP Post fill validator] Adds a JS content fill validator to check #wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .dev_plugin_validate_metabox, | |
| .dev_plugin_validate_metabox table{ | |
| width: 100%; | |
| } | |
| .dev_plugin_validate_metabox table tr .title{ | |
| width:80%; | |
| } | |
| .dev_plugin_validate_metabox table tr .result{ | |
| width:20%; | |
| } | |
| .dev_plugin_validate_metabox .result{ | |
| text-align: right; | |
| } | |
| .dev_plugin_validate_metabox .button{ | |
| width: 100%; | |
| text-align: center; | |
| margin-top: 10px; | |
| } | |
| .dev_plugin_valid_row_valid{ | |
| color: green; | |
| } | |
| .dev_plugin_valid_row_invalid{ | |
| color: red; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jQuery( document ).ready(function($) { | |
| var fields = { | |
| title : $("input[name='post_title']"), | |
| editor : $("#content_ifr").contents().find("body"), | |
| thumbnail : $("input[name='_thumbnail_id']"), | |
| exerpt : "", | |
| categories : $("input[type='checkbox']") | |
| }; | |
| //title check | |
| $(fields.title).on('change', function(){ | |
| console.log(fields.title.val()); | |
| var value = fields.title.val().length; | |
| update_validation_field('title', value); | |
| }); | |
| //editor check | |
| dev_plugin_validate_editor_input(); | |
| setInterval(function(){ | |
| dev_plugin_validate_editor_input(); | |
| }, 10 * 1000); | |
| function dev_plugin_validate_editor_input(){ | |
| var value = 0; | |
| if($('.avia-builder-button:contains("Stand")').length < 1){ | |
| //console.log($("#content_ifr").contents().find("body").html()); | |
| value = $("#content_ifr").contents().find("body").html(); | |
| if(value == '<p><br data-mce-bogus="1"></p>' || value == '<p><br></p>'){ | |
| value = 0; | |
| } else { | |
| value = value.length; | |
| } | |
| } else { | |
| //console.log($('#aviaLayoutBuilder').html()); | |
| value = $('#aviaLayoutBuilder').html().length; | |
| } | |
| update_validation_field('editor', value); | |
| } | |
| //thumbnail check | |
| setInterval(function(){ | |
| var value = $("input[name='_thumbnail_id']").val(); | |
| if(value > 0){ | |
| value = 1; | |
| } else { | |
| value = 0; | |
| } | |
| update_validation_field('thumbnail', value); | |
| }, 7 * 1000); | |
| //category check | |
| var catergory_counter = 0; | |
| $(fields.categories).on('change', function(){ | |
| catergory_counter = 0 | |
| var total_categories = $("#categorychecklist li").length; | |
| var unchecked_categories = $("#categorychecklist .wpseo-term-unchecked").length; | |
| catergory_counter = total_categories - unchecked_categories; // loop all checked items | |
| update_validation_field('categories', catergory_counter); | |
| }); | |
| // console.log($("input[name='post_title']").val()); | |
| // console.log($("input[name='_thumbnail_id']").val()); | |
| // console.log($("#content_ifr").contents().find("body").html()); | |
| // var checkbox_counter = 0; | |
| // $("input[name='post_category[]']").each(function (index, obj) { | |
| // // loop all checked items | |
| // if($(this).val() > 0){ | |
| // checkbox_counter++; | |
| // } | |
| // }); | |
| //console.log(checkbox_counter); | |
| //input[name='post_title'] -> value | |
| //input[name='_thumbnail_id'] -> value | |
| //input[name='post_category[]'] -> checkboxes count | |
| // #content_ifr body -> html content counter / string lenght | |
| }); | |
| function update_validation_field(field, value){ | |
| var new_class = "dev_plugin_valid_row_valid"; | |
| var icon = '<i class="far fa-check-circle"></i>'; | |
| if(value < 1){ | |
| new_class = "dev_plugin_valid_row_invalid"; | |
| icon = '<i class="far fa-times-circle"></i>'; | |
| } | |
| var row = jQuery("#dev_plugin_valid_" + field); | |
| row.removeClass( [ "dev_plugin_valid_row_valid", "dev_plugin_valid_row_invalid" ] ).addClass( new_class ); | |
| row.find('.result').html(icon); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_action( 'admin_menu', 'dev_plugin_post_validate_metabox' ); | |
| function dev_plugin_post_validate_metabox() { | |
| add_meta_box( | |
| 'dev_plugin_validation_metabox', // metabox ID | |
| 'Content validatie', // title | |
| 'dev_plugin_post_validate_metabox_callback', // callback function | |
| ['post', 'page', 'portfolio', 'product'], // post type or post types in array | |
| 'side', // position (normal, side, advanced) | |
| 'high' // priority (default, low, high, core) | |
| ); | |
| } | |
| function add_dev_plugin_validation_fields($extra_elements) { | |
| $post_type = get_post_type(); | |
| if($post_type == "product"){ | |
| $product = wc_get_product( get_the_ID() ); | |
| } | |
| //custom elements | |
| $custom_elements = [ | |
| 'categories' => [['post'], get_the_category(), null], | |
| //woocommerce | |
| 'product categories' => [['product'], ($post_type == "product") ? get_the_terms( get_the_ID() , 'product_cat' ) : "" , "woocommerce/woocommerce.php"], | |
| 'sku' => [['product'], ($post_type == "product") ? $product->get_sku() : "" , "woocommerce/woocommerce.php"], | |
| 'price' => [['product'], ($post_type == "product") ? $product->get_price() : "" , "woocommerce/woocommerce.php"], | |
| 'attributes' => [['product'], ($post_type == "product") ? $product->get_attributes() : "" , "woocommerce/woocommerce.php"], | |
| 'short description' => [['product'], ($post_type == "product") ? $product->get_short_description() : "" , "woocommerce/woocommerce.php"], | |
| //yoast | |
| 'meta title' => [['post', 'page', 'portfolio', 'product'], is_plugin_active( 'wordpress-seo/wp-seo.php' ) ? YoastSEO()->meta->for_current_page()->title : "", 'wordpress-seo/wp-seo.php'], | |
| 'meta description' => [['post', 'page', 'portfolio', 'product'], get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true), 'wordpress-seo/wp-seo.php'], | |
| 'meta keyphrase' => [['post', 'page', 'portfolio', 'product'], get_post_meta(get_the_ID(), '_yoast_wpseo_focuskw', true), 'wordpress-seo/wp-seo.php'], | |
| ]; | |
| $custom_elements = array_merge($custom_elements, $extra_elements); | |
| return $custom_elements; | |
| } | |
| add_filter('dev_plugin_validation_fields', 'add_dev_plugin_validation_fields'); | |
| function dev_plugin_post_validate_metabox_callback( $post ) { | |
| //global $_wp_post_type_features; | |
| //var_dump($_wp_post_type_features['page']); | |
| //var_dump(get_option('active_plugins')); | |
| //elements for validating | (feature) $key => $data | |
| $elements = [ | |
| 'title' => get_the_title(), | |
| 'editor' => get_the_content(), | |
| 'thumbnail' => get_the_post_thumbnail_url(), | |
| 'exerpt' => get_the_excerpt(), | |
| ]; | |
| //custom elements for validating | $custom_key => [array $supported_types, $data, $plugin-requirement] | |
| if(has_filter('dev_plugin_validation_fields')) { | |
| $custom_elements = apply_filters('dev_plugin_validation_fields', []); | |
| } | |
| ?> | |
| <div class="dev_plugin_validate_metabox"> | |
| <table class='dev_plugin_validate_table'> | |
| <tbody> | |
| <?php foreach($elements as $element => $value): | |
| if(post_type_supports( get_post_type(), $element )): | |
| dev_plugin_print_validation_row($element, $value); | |
| endif; | |
| endforeach;?> | |
| <?php foreach($custom_elements as $element => $value): | |
| if(in_array(get_post_type(), $value[0]) && ($value[2] == null || is_plugin_active( $value[2] ))): | |
| switch(gettype($value[1])){ | |
| case "array": | |
| if(count($value[1]) > 0){ | |
| $value[1] = dev_plugin_validation_check_array($element, $value[1]); | |
| } | |
| break; | |
| } | |
| dev_plugin_print_validation_row($element, $value[1] ); | |
| endif; | |
| endforeach; ?> | |
| </tbody> | |
| </table> | |
| <a href="#" class="button button-large">Hulp bij het vullen</a> | |
| </div> | |
| <?php | |
| } | |
| /** | |
| * Enqueue a script in the WordPress admin on edit.php. | |
| * | |
| * @param int $hook Hook suffix for the current admin page. | |
| */ | |
| function dev_plugin_lex_admin_enqueue_scripts( $hook ) { | |
| $hooks = ['edit.php', 'post.php']; | |
| if(!in_array($hook, $hooks)): | |
| return; | |
| endif; | |
| wp_enqueue_script( 'dev_plugin_admin_post_validate_metabox_script', '/wp-content/themes/enfold-child/assets/js/admin-post-validate-metablock.js', array('jquery'), '1.0.2' ); | |
| wp_enqueue_style( 'dev_plugin_admin_post_validate_metabox_css', '/wp-content/themes/enfold-child/assets/css/admin-post-validate-metablock.css', false, '1.0.2' ); | |
| wp_enqueue_style( 'font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css', false, '5.15.4'); | |
| } | |
| add_action( 'admin_enqueue_scripts', 'dev_plugin_lex_admin_enqueue_scripts' ); | |
| function dev_plugin_print_validation_row($element, $value){ | |
| $correct = '<i class="far fa-check-circle"></i>'; | |
| $incorrect = '<i class="far fa-times-circle"></i>'; | |
| if(gettype($value) == 'integer'){ | |
| $check = ($value > 0) ? "valid" : "invalid"; | |
| $check_icon = ($value > 0) ? $correct : $incorrect; | |
| } else{ | |
| $check = (strlen($value) > 0) ? "valid" : "invalid"; | |
| $check_icon = (strlen($value) > 0) ? $correct : $incorrect; | |
| } ?> | |
| <tr class='dev_plugin_valid_row_<?=$check?>' id="dev_plugin_valid_<?= $element; ?>"> | |
| <td class="title"><?=__(ucfirst($element)); ?></td> | |
| <td class="result"><?= $check_icon ?></td> | |
| </tr> | |
| <?php | |
| } | |
| function dev_plugin_validation_check_array($element, $value){ | |
| if($element == "categories" || $element == "product categories"){ | |
| foreach($value as $key => $category ){ | |
| if($category->slug == "geen-categorie"){ | |
| unset($value[$key]); | |
| } | |
| } | |
| } | |
| return count($value); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment