Plugin Directory

Changeset 2076075


Ignore:
Timestamp:
04/27/2019 10:33:49 PM (7 years ago)
Author:
RobertGillmer
Message:

Error reporting is now 927% betterer.
Form submits via AJAX, which is why the error reporting is so much betterer.
Split list of CPT's up into groups based on ability to use the Gutenberg editor.
"Delete all the things!" button added. (Not all the things, just all the things this plugin created, but that's wordy.)

Location:
block-style-guides
Files:
2 added
2 deleted
8 edited
26 copied

Legend:

Unmodified
Added
Removed
  • block-style-guides/tags/1.1.0/block-style-guides.php

    r2074113 r2076075  
    44 * Plugin Name: Block Style Guides for Gutenberg
    55 * Description: Creates posts in draft mode showcasing all default Gutenberg blocks, for styling purposes.
    6  * Version:     1.0
     6 * Version:     1.1.0
    77 * Author:      Robert Gillmer
    88 * Author URI:  https://www.robertgillmer.com
  • block-style-guides/tags/1.2.0/README.md

    r2074389 r2076075  
    9191* Added filters for a buncha placeholder variables.
    9292* Used those variables throughout the markup.
     93
     94**1.2.0**
     95
     96* Error reporting is now 927% betterer.
     97* Form submits via AJAX, which is why the error reporting is so much betterer.
     98* Split list of CPT's up into groups based on ability to use the Gutenberg editor.
     99* "Delete all the things!" button added.  (Not all the things, just all the things this plugin created, but that's wordy.)
  • block-style-guides/tags/1.2.0/add-bsg-posts.php

    r2074113 r2076075  
    22
    33function bsg_add_posts() {
    4 
    54    $error_messages = array();
    65    $nonce = $_REQUEST[ 'bsgAddNonce' ];
     
    7877            // If it *still* doesn't exist, it just wasn't meant to be, man.
    7978            if( ! class_exists( $v ) ) {
    80                 $error_messages[ 'class_does_not_exist' ][] = $v;
     79                $error_messages[ 'classDoesNotExist' ][] = $k;
    8180                continue;
    8281            }
     
    8685            // Does the post exist? If so, skip it
    8786            if( $wpdb->get_row( "SELECT post_title FROM wp_posts WHERE post_title = '" . $k . "' AND post_type = '" . $post_type . "'", 'ARRAY_A' ) ) {
    88 
    89                 $error_messages[ 'post_already_exists' ][] = $post_type;
     87                $post_type_object = get_post_type_object( $post_type );
     88                $post_type_name = $post_type_object->labels->name;
     89                $error_messages[ 'postAlreadyExists' ][] = $post_type_name;
    9090                continue;
    9191            }
     
    107107
    108108            if( is_wp_error( $maybe_error ) ) {
    109                 $error_messages[ 'could_not_create_posts' ] = $k;
     109                $error_messages[ 'couldNotCreatePosts' ][] = $k;
    110110            }
    111111        }
     
    114114    if( $error_messages ) {
    115115        foreach( $error_messages as $k => $v ) {
    116             $error_message_transient[ $k ] = array_unique( $v );
     116            $unique_error_messages[ $k ] = array_values( array_unique( $v ) );
    117117        }
    118         set_transient( 'bsg-errors', $error_message_transient, DAY_IN_SECONDS );
    119118    }
    120119
    121     wp_send_json_success();
     120    if( empty( $unique_error_messages ) ) {
     121        wp_send_json_success( true );
     122    } else {
     123        wp_send_json_success( $unique_error_messages );
     124    }
    122125
    123126    /*
  • block-style-guides/tags/1.2.0/admin-menu-functions.php

    r2074113 r2076075  
    1313
    1414function bsg_settings_init() {
    15     register_setting( 'bsg_plugin_page', 'bsg_post_types', 'bsg_admin_notices' );
    16 
    1715    add_settings_section(
    1816        'bsg_plugin_page_section',
     
    3129}
    3230
    33 function bsg_admin_notices( $input ) {
    34     $message = '';
    35     $type = '';
    36     $error_transient = get_transient( 'bsg-errors' );
    37 
    38     if( $error_transient ) {
    39         $errors = ( $error_transient );
    40        
    41         foreach( $errors as $k => $v ) {
    42 
    43             switch( $k ) {
    44                 case 'nonce_failed':
    45                     $message .= 'Your request has timed out.  Please refresh the page and try again.<br />';
    46                     break;
    47 
    48                 case 'class_does_not_exist':
    49                     foreach( $v as $class ) {
    50                         $full_class_name = $class;
    51                         $class_name_array = explode( '\\', $full_class_name );
    52                         $class_name = array_pop( $class_name_array );
    53 
    54                         // @TODO - Make this error message suck 87% less.
    55                         $message .= 'The ' . $class_name . ' class could not be created, so the page(s) associated with it has not been created.<br />';
    56 
    57                     }
    58 
    59                     break;
    60 
    61                 case 'post_already_exists':
    62                     foreach( $v as $post_type ) {
    63                         $post_type_obj = get_post_type_object( $post_type );
    64                         $post_type_name = $post_type_obj->labels->singular_name;
    65                         $message .= 'The posts for the ' . ( $post_type_name ) . ' post type already existed, so they have not be created.<br />';
    66                     }
    67                     break;
    68 
    69                 case 'could_not_create_posts':
    70                     $message .= 'The system could not create your posts.  Please refresh and try again.<br />';
    71                     break;
    72             }
    73         }
    74 
    75         add_settings_error( 'bsg_post_types', 'bsg-update', $message, 'error' );
    76     } else {
    77         $message = 'Your posts have been created!';
    78         $type = 'updated';
    79         add_settings_error( 'bsg_post_types', 'bsg-update', $message, $type );
    80     }
    81 
    82     delete_transient( 'bsg-errors' );
    83    
    84     return $input;
    85 }
    86 
    8731function bsg_cpt_selection_render() {
     32    $out = '';
     33    $builtin_cpts = array();
     34    $gutenbergable_cpts = array(); // If that's not a word, it should be
     35    $non_gutenbergable_cpts = array();
     36    $non_contentable_cpts = array(); // That one too
    8837    $all_post_types = get_post_types(
    8938        array(
     
    9342    );
    9443
     44    // Attachments are public but neither Gutenbergable nor contentable.
    9545    unset( $all_post_types[ 'attachment' ] );
    9646
     47    foreach( $all_post_types as $cpt ) {
     48        if( $cpt->_builtin ) {
     49            $builtin_cpts[] = $cpt;
     50        } else if( ! post_type_supports( $cpt->name, 'editor' ) ) {
     51            $non_contentable_cpts[] = $cpt;
     52        } else if( ! $cpt->show_in_rest ) {
     53            $non_gutenbergable_cpts[] = $cpt;
     54        } else {
     55            $gutenbergable_cpts[] = $cpt;
     56        }
     57    }
     58
     59    if( is_array( $builtin_cpts ) && ! empty( $builtin_cpts ) ) {
     60        $out .= '<h4>Built-in post types</h4>';
     61        $out .= '<p style="margin-bottom: 10px;">These post types are part of WordPress core.</p>';
     62        $out .= bsg_display_checkboxes( $builtin_cpts );
     63    }
     64
     65    if( is_array( $gutenbergable_cpts ) && ! empty( $gutenbergable_cpts ) ) {
     66        $out .= '<h4>Custom post types</h4>';
     67        $out .= '<p style="margin-bottom: 10px;">These post types currently support the Gutenberg editor.</p>';
     68        $out .= bsg_display_checkboxes( $gutenbergable_cpts );
     69    }
     70
     71    if( is_array( $non_gutenbergable_cpts ) && ! empty( $non_gutenbergable_cpts ) ) {
     72        $out .= '<h4>Custom post types with no Gutenberg support</h4>';
     73        $out .= '<p style="margin-bottom: 10px;">These posts types do not currently support the Gutenberg editor.  You can still create style guides for them to see what they will look like if/when they declare support.</p>';
     74        $out .= bsg_display_checkboxes( $non_gutenbergable_cpts );
     75    }
     76
     77    if( is_array( $non_contentable_cpts ) && ! empty( $non_contentable_cpts ) ) {
     78        $out .= '<h4>Custom post types with no support for content</h4>';
     79        $out .= '<p style="margin-bottom: 10px;">These post types don\'t display <code>the_content</code> as part of their output.  You can still create style guides for them, but they likely won\'t display the style guides correctly or at all.</p>';
     80        $out .= bsg_display_checkboxes( $non_contentable_cpts );
     81    }
     82
     83    echo $out;
     84}
     85
     86function bsg_display_checkboxes( $cpts = array() ) {
     87    $out = '';
    9788    $options = get_option( 'bsg_post_types' );
    98     foreach( $all_post_types as $cpt ) {
     89
     90    if( ! is_array( $cpts ) || empty( $cpts ) ) {
     91        return;
     92    }
     93
     94    foreach( $cpts as $cpt ) {
    9995        if( isset( $options[ $cpt->name ] ) ) {
    10096            $checked = $options[ $cpt->name ];
     
    10298            $checked = '';
    10399        }
     100
     101        ob_start();
    104102        ?>
    105         <input class="bsg-cpt-type" data-cpt="<?php echo $cpt->name; ?>" type='checkbox' name='bsg_post_types[<?php echo $cpt->name; ?>]' <?php checked( $checked, 1 ); ?> value='1'>
    106         <label for='bsg_post_types[<?php echo $cpt->name; ?>]'><?php echo $cpt->label; ?></label>
    107         <br />
     103<input class="bsg-cpt-type" data-cpt="<?php echo $cpt->name; ?>" type='checkbox' name='bsg_post_types[<?php echo $cpt->name; ?>]' id="bsg_post_types[<?php echo $cpt->name; ?>]" <?php checked( $checked, 1 ); ?> value='1'>
     104<label for='bsg_post_types[<?php echo $cpt->name; ?>]'><?php echo $cpt->label; ?></label>
     105<br />
    108106        <?php
     107        $out .= ob_get_clean();
    109108    }
     109
     110    return $out;
    110111}
    111112
     
    116117function bsg_options_page() {
    117118    wp_enqueue_script( 'bsg-ajax' );
     119    wp_enqueue_script( 'json-form' );
    118120
    119121    $script_params = array(
    120122        'bsgAddNonce'       => wp_create_nonce( 'bsg-add-nonce' ),
    121         'bsgDeleteNonce'    => wp_create_nonce( 'bsg-delete-nonce' ), // upcoming
     123        'bsgDeleteNonce'    => wp_create_nonce( 'bsg-delete-nonce' ),
    122124    );
    123125   
     
    125127
    126128    ?>
    127     <h2>Gutenberg Block Style Guides</h2>
     129    <h2 class="bsg-title">Block Style Guides for Gutenberg</h2>
    128130    <?php settings_errors(); ?>
    129131    <form action='options.php' method='post' name="bsg-form" id="bsg-form">
     
    134136        ?>
    135137    </form>
     138    <p>If you want to delete all the posts created by this plugin, click the button below.</p>
     139    <button id="bsg-delete" class="button button-secondary">Delete all Block Style Guide posts</button>
    136140    <?php
    137141}
  • block-style-guides/tags/1.2.0/block-style-guides.php

    r2074113 r2076075  
    44 * Plugin Name: Block Style Guides for Gutenberg
    55 * Description: Creates posts in draft mode showcasing all default Gutenberg blocks, for styling purposes.
    6  * Version:     1.0
     6 * Version:     1.2.0
    77 * Author:      Robert Gillmer
    88 * Author URI:  https://www.robertgillmer.com
     
    2828include_once( plugin_dir_path( __FILE__ ) . 'admin-menu-functions.php' );
    2929include_once( plugin_dir_path( __FILE__ ) . 'add-bsg-posts.php');
     30include_once( plugin_dir_path( __FILE__ ) . 'delete-bsg-posts.php');
  • block-style-guides/tags/1.2.0/general.php

    r2074113 r2076075  
    22
    33function bsg_enqueue_scripts() {
    4     wp_register_script( 'bsg-ajax', plugin_dir_url( __FILE__ ) . '/js/bsg.js', array( 'jquery' ), false, true );
     4    wp_register_script( 'bsg-ajax', plugin_dir_url( __FILE__ ) . '/js/bsg.js', array( 'jquery', 'jquery-form' ), false, true );
    55}
    66
  • block-style-guides/tags/1.2.0/js/bsg.js

    r2074113 r2076075  
    11(function($) {
    22    var bsgAddNonce = bsgAjax.bsgAddNonce;
    3     var bsgDeleteNonce = bsgAjax.bsgDeleteNonce; // Upcoming
     3    var bsgDeleteNonce = bsgAjax.bsgDeleteNonce;
    44
    55    $( '#bsg-form' ).submit( function( e ) {
     6        e.preventDefault(); // Submitting via AJAX
     7        $(this).ajaxSubmit(); // See?
     8
    69        var selectedTypes = [];
    710        jQuery( '.bsg-cpt-type' ).each( function() {
     
    2124        })
    2225
    23         .done( function( data ) {
     26        .done( function( response ) {
     27            $( '.setting-error-bsg-update' ).remove();
     28            var data = response[ 'data' ];
     29                hasError = false,
     30                banners = '';
     31
     32            if( data === true ) { // true means there were no errors
     33                banners = makeBanner( 'Posts were created successfully.', 'updated' ) + banners;
     34            } else {
     35                if( 'couldNotCreatePosts' in data ) {
     36                    var message = 'The following post(s) could not be created due to internal errors: ',
     37                        errorArray = data[ 'couldNotCreatePosts' ];
     38                    banners += makeErrorBanner( message, errorArray );;
     39                    hasError = true;
     40                }
     41
     42                if( 'postAlreadyExists' in data ) {
     43                    var message = 'Posts already existed for the following post type(s), so new posts were not created: ',
     44                        errorArray = data[ 'postAlreadyExists' ];
     45                    banners += makeErrorBanner( message, errorArray );;
     46                    hasError = true;
     47                }
     48
     49                if( 'classDoesNotExist' in data ) {
     50                    var message = 'The following page(s) could not be created because the class doesn\'t exist: ',
     51                        errorArray = data[ 'classDoesNotExist' ];
     52                    banners += makeErrorBanner( message, errorArray );;
     53                    hasError = true;
     54                }
     55                // Success messages should be above the error messages, hence the no += operator.
     56                banners = makeBanner( 'Posts were created successfully, with the exceptions listed below.', 'updated' ) + banners;
     57            }
     58
     59            $( '.bsg-title' ).after( banners );
     60
    2461            return true;
    2562        })
     
    2966        })
    3067    } );
     68
     69    $( '#bsg-delete' ).click( function( e ) {
     70        e.preventDefault();
     71        $( '.setting-error-bsg-update' ).remove();
     72
     73        $.ajax({
     74            url: ajaxurl,
     75            type: 'post',
     76            data: {
     77                action: 'bsg_delete_posts',
     78                bsgDeleteNonce: bsgDeleteNonce,
     79            }
     80        })
     81
     82        .done( function( data ) {
     83            var banner = makeBanner( 'All posts created by Block Style Guides have been successfully deleted.', 'updated' );
     84            $( '.bsg-title' ).after( banner );
     85            return true;
     86        })
     87
     88        .fail( function() {
     89            var banner = makeBanner( 'There was an issue deleting the posts created by Block Style Guides.', 'error' );
     90            $( '.bsg-title' ).after( banner );
     91            return false; // More here too
     92        })
     93
     94        return false;
     95    } );
     96
     97    function makeBanner( text, type="updated" ) {
     98        if( ! text ) {
     99            return;
     100        }
     101
     102        var banner = '<div class="setting-error-bsg-update ' + type + ' settings-error notice is-dismissible">';
     103            banner += '<p><strong>' + text + '</strong></p>';
     104            banner += '<button type="button" class="notice-dismiss">';
     105            banner += '<span class="screen-reader-text">Dismiss this notice.</span>';
     106            banner += '</button>';
     107            banner += '</div>';
     108
     109        return banner;
     110    }
     111
     112    function makeErrorBanner( text, errorArray ) {
     113        var message = text + errorArray.join( ', ' );
     114
     115        return makeBanner( message, 'error' );
     116    }
    31117})( jQuery );
  • block-style-guides/tags/1.2.0/readme.txt

    r2074389 r2076075  
    9191* Added filters for a buncha placeholder variables.
    9292* Used those variables throughout the markup.
     93
     94= 1.2.0 =
     95
     96* Error reporting is now 927% betterer.
     97* Form submits via AJAX, which is why the error reporting is so much betterer.
     98* Split list of CPT's up into groups based on ability to use the Gutenberg editor.
     99* "Delete all the things!" button added.  (Not all the things, just all the things this plugin created, but that's wordy.)
  • block-style-guides/trunk/README.md

    r2074389 r2076075  
    9191* Added filters for a buncha placeholder variables.
    9292* Used those variables throughout the markup.
     93
     94**1.2.0**
     95
     96* Error reporting is now 927% betterer.
     97* Form submits via AJAX, which is why the error reporting is so much betterer.
     98* Split list of CPT's up into groups based on ability to use the Gutenberg editor.
     99* "Delete all the things!" button added.  (Not all the things, just all the things this plugin created, but that's wordy.)
  • block-style-guides/trunk/add-bsg-posts.php

    r2074113 r2076075  
    22
    33function bsg_add_posts() {
    4 
    54    $error_messages = array();
    65    $nonce = $_REQUEST[ 'bsgAddNonce' ];
     
    7877            // If it *still* doesn't exist, it just wasn't meant to be, man.
    7978            if( ! class_exists( $v ) ) {
    80                 $error_messages[ 'class_does_not_exist' ][] = $v;
     79                $error_messages[ 'classDoesNotExist' ][] = $k;
    8180                continue;
    8281            }
     
    8685            // Does the post exist? If so, skip it
    8786            if( $wpdb->get_row( "SELECT post_title FROM wp_posts WHERE post_title = '" . $k . "' AND post_type = '" . $post_type . "'", 'ARRAY_A' ) ) {
    88 
    89                 $error_messages[ 'post_already_exists' ][] = $post_type;
     87                $post_type_object = get_post_type_object( $post_type );
     88                $post_type_name = $post_type_object->labels->name;
     89                $error_messages[ 'postAlreadyExists' ][] = $post_type_name;
    9090                continue;
    9191            }
     
    107107
    108108            if( is_wp_error( $maybe_error ) ) {
    109                 $error_messages[ 'could_not_create_posts' ] = $k;
     109                $error_messages[ 'couldNotCreatePosts' ][] = $k;
    110110            }
    111111        }
     
    114114    if( $error_messages ) {
    115115        foreach( $error_messages as $k => $v ) {
    116             $error_message_transient[ $k ] = array_unique( $v );
     116            $unique_error_messages[ $k ] = array_values( array_unique( $v ) );
    117117        }
    118         set_transient( 'bsg-errors', $error_message_transient, DAY_IN_SECONDS );
    119118    }
    120119
    121     wp_send_json_success();
     120    if( empty( $unique_error_messages ) ) {
     121        wp_send_json_success( true );
     122    } else {
     123        wp_send_json_success( $unique_error_messages );
     124    }
    122125
    123126    /*
  • block-style-guides/trunk/admin-menu-functions.php

    r2074113 r2076075  
    1313
    1414function bsg_settings_init() {
    15     register_setting( 'bsg_plugin_page', 'bsg_post_types', 'bsg_admin_notices' );
    16 
    1715    add_settings_section(
    1816        'bsg_plugin_page_section',
     
    3129}
    3230
    33 function bsg_admin_notices( $input ) {
    34     $message = '';
    35     $type = '';
    36     $error_transient = get_transient( 'bsg-errors' );
    37 
    38     if( $error_transient ) {
    39         $errors = ( $error_transient );
    40        
    41         foreach( $errors as $k => $v ) {
    42 
    43             switch( $k ) {
    44                 case 'nonce_failed':
    45                     $message .= 'Your request has timed out.  Please refresh the page and try again.<br />';
    46                     break;
    47 
    48                 case 'class_does_not_exist':
    49                     foreach( $v as $class ) {
    50                         $full_class_name = $class;
    51                         $class_name_array = explode( '\\', $full_class_name );
    52                         $class_name = array_pop( $class_name_array );
    53 
    54                         // @TODO - Make this error message suck 87% less.
    55                         $message .= 'The ' . $class_name . ' class could not be created, so the page(s) associated with it has not been created.<br />';
    56 
    57                     }
    58 
    59                     break;
    60 
    61                 case 'post_already_exists':
    62                     foreach( $v as $post_type ) {
    63                         $post_type_obj = get_post_type_object( $post_type );
    64                         $post_type_name = $post_type_obj->labels->singular_name;
    65                         $message .= 'The posts for the ' . ( $post_type_name ) . ' post type already existed, so they have not be created.<br />';
    66                     }
    67                     break;
    68 
    69                 case 'could_not_create_posts':
    70                     $message .= 'The system could not create your posts.  Please refresh and try again.<br />';
    71                     break;
    72             }
    73         }
    74 
    75         add_settings_error( 'bsg_post_types', 'bsg-update', $message, 'error' );
    76     } else {
    77         $message = 'Your posts have been created!';
    78         $type = 'updated';
    79         add_settings_error( 'bsg_post_types', 'bsg-update', $message, $type );
    80     }
    81 
    82     delete_transient( 'bsg-errors' );
    83    
    84     return $input;
    85 }
    86 
    8731function bsg_cpt_selection_render() {
     32    $out = '';
     33    $builtin_cpts = array();
     34    $gutenbergable_cpts = array(); // If that's not a word, it should be
     35    $non_gutenbergable_cpts = array();
     36    $non_contentable_cpts = array(); // That one too
    8837    $all_post_types = get_post_types(
    8938        array(
     
    9342    );
    9443
     44    // Attachments are public but neither Gutenbergable nor contentable.
    9545    unset( $all_post_types[ 'attachment' ] );
    9646
     47    foreach( $all_post_types as $cpt ) {
     48        if( $cpt->_builtin ) {
     49            $builtin_cpts[] = $cpt;
     50        } else if( ! post_type_supports( $cpt->name, 'editor' ) ) {
     51            $non_contentable_cpts[] = $cpt;
     52        } else if( ! $cpt->show_in_rest ) {
     53            $non_gutenbergable_cpts[] = $cpt;
     54        } else {
     55            $gutenbergable_cpts[] = $cpt;
     56        }
     57    }
     58
     59    if( is_array( $builtin_cpts ) && ! empty( $builtin_cpts ) ) {
     60        $out .= '<h4>Built-in post types</h4>';
     61        $out .= '<p style="margin-bottom: 10px;">These post types are part of WordPress core.</p>';
     62        $out .= bsg_display_checkboxes( $builtin_cpts );
     63    }
     64
     65    if( is_array( $gutenbergable_cpts ) && ! empty( $gutenbergable_cpts ) ) {
     66        $out .= '<h4>Custom post types</h4>';
     67        $out .= '<p style="margin-bottom: 10px;">These post types currently support the Gutenberg editor.</p>';
     68        $out .= bsg_display_checkboxes( $gutenbergable_cpts );
     69    }
     70
     71    if( is_array( $non_gutenbergable_cpts ) && ! empty( $non_gutenbergable_cpts ) ) {
     72        $out .= '<h4>Custom post types with no Gutenberg support</h4>';
     73        $out .= '<p style="margin-bottom: 10px;">These posts types do not currently support the Gutenberg editor.  You can still create style guides for them to see what they will look like if/when they declare support.</p>';
     74        $out .= bsg_display_checkboxes( $non_gutenbergable_cpts );
     75    }
     76
     77    if( is_array( $non_contentable_cpts ) && ! empty( $non_contentable_cpts ) ) {
     78        $out .= '<h4>Custom post types with no support for content</h4>';
     79        $out .= '<p style="margin-bottom: 10px;">These post types don\'t display <code>the_content</code> as part of their output.  You can still create style guides for them, but they likely won\'t display the style guides correctly or at all.</p>';
     80        $out .= bsg_display_checkboxes( $non_contentable_cpts );
     81    }
     82
     83    echo $out;
     84}
     85
     86function bsg_display_checkboxes( $cpts = array() ) {
     87    $out = '';
    9788    $options = get_option( 'bsg_post_types' );
    98     foreach( $all_post_types as $cpt ) {
     89
     90    if( ! is_array( $cpts ) || empty( $cpts ) ) {
     91        return;
     92    }
     93
     94    foreach( $cpts as $cpt ) {
    9995        if( isset( $options[ $cpt->name ] ) ) {
    10096            $checked = $options[ $cpt->name ];
     
    10298            $checked = '';
    10399        }
     100
     101        ob_start();
    104102        ?>
    105         <input class="bsg-cpt-type" data-cpt="<?php echo $cpt->name; ?>" type='checkbox' name='bsg_post_types[<?php echo $cpt->name; ?>]' <?php checked( $checked, 1 ); ?> value='1'>
    106         <label for='bsg_post_types[<?php echo $cpt->name; ?>]'><?php echo $cpt->label; ?></label>
    107         <br />
     103<input class="bsg-cpt-type" data-cpt="<?php echo $cpt->name; ?>" type='checkbox' name='bsg_post_types[<?php echo $cpt->name; ?>]' id="bsg_post_types[<?php echo $cpt->name; ?>]" <?php checked( $checked, 1 ); ?> value='1'>
     104<label for='bsg_post_types[<?php echo $cpt->name; ?>]'><?php echo $cpt->label; ?></label>
     105<br />
    108106        <?php
     107        $out .= ob_get_clean();
    109108    }
     109
     110    return $out;
    110111}
    111112
     
    116117function bsg_options_page() {
    117118    wp_enqueue_script( 'bsg-ajax' );
     119    wp_enqueue_script( 'json-form' );
    118120
    119121    $script_params = array(
    120122        'bsgAddNonce'       => wp_create_nonce( 'bsg-add-nonce' ),
    121         'bsgDeleteNonce'    => wp_create_nonce( 'bsg-delete-nonce' ), // upcoming
     123        'bsgDeleteNonce'    => wp_create_nonce( 'bsg-delete-nonce' ),
    122124    );
    123125   
     
    125127
    126128    ?>
    127     <h2>Gutenberg Block Style Guides</h2>
     129    <h2 class="bsg-title">Block Style Guides for Gutenberg</h2>
    128130    <?php settings_errors(); ?>
    129131    <form action='options.php' method='post' name="bsg-form" id="bsg-form">
     
    134136        ?>
    135137    </form>
     138    <p>If you want to delete all the posts created by this plugin, click the button below.</p>
     139    <button id="bsg-delete" class="button button-secondary">Delete all Block Style Guide posts</button>
    136140    <?php
    137141}
  • block-style-guides/trunk/block-style-guides.php

    r2074113 r2076075  
    44 * Plugin Name: Block Style Guides for Gutenberg
    55 * Description: Creates posts in draft mode showcasing all default Gutenberg blocks, for styling purposes.
    6  * Version:     1.0
     6 * Version:     1.2.0
    77 * Author:      Robert Gillmer
    88 * Author URI:  https://www.robertgillmer.com
     
    2828include_once( plugin_dir_path( __FILE__ ) . 'admin-menu-functions.php' );
    2929include_once( plugin_dir_path( __FILE__ ) . 'add-bsg-posts.php');
     30include_once( plugin_dir_path( __FILE__ ) . 'delete-bsg-posts.php');
  • block-style-guides/trunk/general.php

    r2074113 r2076075  
    22
    33function bsg_enqueue_scripts() {
    4     wp_register_script( 'bsg-ajax', plugin_dir_url( __FILE__ ) . '/js/bsg.js', array( 'jquery' ), false, true );
     4    wp_register_script( 'bsg-ajax', plugin_dir_url( __FILE__ ) . '/js/bsg.js', array( 'jquery', 'jquery-form' ), false, true );
    55}
    66
  • block-style-guides/trunk/js/bsg.js

    r2074113 r2076075  
    11(function($) {
    22    var bsgAddNonce = bsgAjax.bsgAddNonce;
    3     var bsgDeleteNonce = bsgAjax.bsgDeleteNonce; // Upcoming
     3    var bsgDeleteNonce = bsgAjax.bsgDeleteNonce;
    44
    55    $( '#bsg-form' ).submit( function( e ) {
     6        e.preventDefault(); // Submitting via AJAX
     7        $(this).ajaxSubmit(); // See?
     8
    69        var selectedTypes = [];
    710        jQuery( '.bsg-cpt-type' ).each( function() {
     
    2124        })
    2225
    23         .done( function( data ) {
     26        .done( function( response ) {
     27            $( '.setting-error-bsg-update' ).remove();
     28            var data = response[ 'data' ];
     29                hasError = false,
     30                banners = '';
     31
     32            if( data === true ) { // true means there were no errors
     33                banners = makeBanner( 'Posts were created successfully.', 'updated' ) + banners;
     34            } else {
     35                if( 'couldNotCreatePosts' in data ) {
     36                    var message = 'The following post(s) could not be created due to internal errors: ',
     37                        errorArray = data[ 'couldNotCreatePosts' ];
     38                    banners += makeErrorBanner( message, errorArray );;
     39                    hasError = true;
     40                }
     41
     42                if( 'postAlreadyExists' in data ) {
     43                    var message = 'Posts already existed for the following post type(s), so new posts were not created: ',
     44                        errorArray = data[ 'postAlreadyExists' ];
     45                    banners += makeErrorBanner( message, errorArray );;
     46                    hasError = true;
     47                }
     48
     49                if( 'classDoesNotExist' in data ) {
     50                    var message = 'The following page(s) could not be created because the class doesn\'t exist: ',
     51                        errorArray = data[ 'classDoesNotExist' ];
     52                    banners += makeErrorBanner( message, errorArray );;
     53                    hasError = true;
     54                }
     55                // Success messages should be above the error messages, hence the no += operator.
     56                banners = makeBanner( 'Posts were created successfully, with the exceptions listed below.', 'updated' ) + banners;
     57            }
     58
     59            $( '.bsg-title' ).after( banners );
     60
    2461            return true;
    2562        })
     
    2966        })
    3067    } );
     68
     69    $( '#bsg-delete' ).click( function( e ) {
     70        e.preventDefault();
     71        $( '.setting-error-bsg-update' ).remove();
     72
     73        $.ajax({
     74            url: ajaxurl,
     75            type: 'post',
     76            data: {
     77                action: 'bsg_delete_posts',
     78                bsgDeleteNonce: bsgDeleteNonce,
     79            }
     80        })
     81
     82        .done( function( data ) {
     83            var banner = makeBanner( 'All posts created by Block Style Guides have been successfully deleted.', 'updated' );
     84            $( '.bsg-title' ).after( banner );
     85            return true;
     86        })
     87
     88        .fail( function() {
     89            var banner = makeBanner( 'There was an issue deleting the posts created by Block Style Guides.', 'error' );
     90            $( '.bsg-title' ).after( banner );
     91            return false; // More here too
     92        })
     93
     94        return false;
     95    } );
     96
     97    function makeBanner( text, type="updated" ) {
     98        if( ! text ) {
     99            return;
     100        }
     101
     102        var banner = '<div class="setting-error-bsg-update ' + type + ' settings-error notice is-dismissible">';
     103            banner += '<p><strong>' + text + '</strong></p>';
     104            banner += '<button type="button" class="notice-dismiss">';
     105            banner += '<span class="screen-reader-text">Dismiss this notice.</span>';
     106            banner += '</button>';
     107            banner += '</div>';
     108
     109        return banner;
     110    }
     111
     112    function makeErrorBanner( text, errorArray ) {
     113        var message = text + errorArray.join( ', ' );
     114
     115        return makeBanner( message, 'error' );
     116    }
    31117})( jQuery );
  • block-style-guides/trunk/readme.txt

    r2074389 r2076075  
    9191* Added filters for a buncha placeholder variables.
    9292* Used those variables throughout the markup.
     93
     94= 1.2.0 =
     95
     96* Error reporting is now 927% betterer.
     97* Form submits via AJAX, which is why the error reporting is so much betterer.
     98* Split list of CPT's up into groups based on ability to use the Gutenberg editor.
     99* "Delete all the things!" button added.  (Not all the things, just all the things this plugin created, but that's wordy.)
Note: See TracChangeset for help on using the changeset viewer.