diff --git a/README.md b/README.md index 1681844..8e1a7bd 100755 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ WP Generator Inspired by [https://github.com/tareq1988/wp-generators](https://github.com/tareq1988/wp-generators) -## Supported +## Features - WP List Table Generator ## Installation diff --git a/process.php b/process.php index 2b04449..1308e42 100755 --- a/process.php +++ b/process.php @@ -290,7 +290,9 @@ function process($post_data, $download = true, $delete = true) $plugin_main_file = file_get_contents(dirname(__FILE__) . '/stubs/plugin-name.stub'); $plugin_uninstall_file = file_get_contents(dirname(__FILE__) . '/stubs/uninstall.stub'); - $crud_admin_menu_file = file_get_contents(dirname(__FILE__) . '/stubs/class-crud-name-admin-menu.stub'); + $admin_menu_file = file_get_contents(dirname(__FILE__) . '/stubs/class-admin-menu.stub'); + $form_handler_file = file_get_contents(dirname(__FILE__) . '/stubs/class-form-handler.stub'); + $crud_functions_file = file_get_contents(dirname(__FILE__) . '/stubs/crud-name-functions.stub'); $crud_functions_file = file_get_contents(dirname(__FILE__) . '/stubs/crud-name-functions.stub'); $crud_wp_list_table_file = file_get_contents(dirname(__FILE__) . '/stubs/class-crud-name-list-table.stub'); $crud_list_view_file = file_get_contents(dirname(__FILE__) . '/stubs/views/crud-name.stub'); @@ -305,10 +307,11 @@ function process($post_data, $download = true, $delete = true) $crud_wp_list_table_file = str_replace("%%column_default%%", $column_default, $crud_wp_list_table_file); $crud_wp_list_table_file = str_replace("%%columns%%", $columns, $crud_wp_list_table_file); $crud_wp_list_table_file = str_replace("%%sortable_columns%%", $sortable_columns, $crud_wp_list_table_file); - $crud_wp_list_table_file = str_replace("%%form_submit_fields%%", $form_submit_fields, $crud_wp_list_table_file); - $crud_wp_list_table_file = str_replace("%%form_fields%%", $form_fields, $crud_wp_list_table_file); + $crud_functions_file = str_replace("%%form_single_default%%", $form_single_default, $crud_functions_file); - $crud_wp_list_table_file = str_replace("%%form_validation%%", $form_validation, $crud_wp_list_table_file); + + $form_handler_file = str_replace("%%form_submit_fields%%", $form_submit_fields, $form_handler_file); + $form_handler_file = str_replace("%%form_fields%%", $form_fields, $form_handler_file); // Repacing form html $crud_new_view_file = str_replace("%%new_view_form%%", $new_view_form, $crud_new_view_file); @@ -319,7 +322,8 @@ function process($post_data, $download = true, $delete = true) $plugin_main_file = str_replace("%%$key%%", $value, $plugin_main_file); $plugin_uninstall_file = str_replace("%%$key%%", $value, $plugin_uninstall_file); $crud_functions_file = str_replace("%%$key%%", $value, $crud_functions_file); - $crud_admin_menu_file = str_replace("%%$key%%", $value, $crud_admin_menu_file); + $admin_menu_file = str_replace("%%$key%%", $value, $admin_menu_file); + $form_handler_file = str_replace("%%$key%%", $value, $form_handler_file); $crud_wp_list_table_file = str_replace("%%$key%%", $value, $crud_wp_list_table_file); $crud_list_view_file = str_replace("%%$key%%", $value, $crud_list_view_file); $crud_new_view_file = str_replace("%%$key%%", $value, $crud_new_view_file); @@ -344,34 +348,37 @@ function process($post_data, $download = true, $delete = true) mkdir($plugin_dir . 'includes/', 0777); } - $crud_dir = $plugin_dir . 'includes/' . $data['crud_name'] . '/'; - if (!is_dir($crud_dir)) { - mkdir($crud_dir, 0777); + $admin_dir = $plugin_dir . 'includes/admin/'; + if (!is_dir($admin_dir)) { + mkdir($admin_dir, 0777); } // Creating admin menu file - file_put_contents($crud_dir . 'class-' . $data['crud_name'] . '-admin-menu.php', $crud_admin_menu_file); + file_put_contents($admin_dir . 'class-admin-menu.php', $admin_menu_file); + + // Creating form handler file + file_put_contents($admin_dir . 'class-form-handler.php', $form_handler_file); // Creating functions file - file_put_contents($crud_dir . $data['crud_name'] . '-functions.php', $crud_functions_file); + file_put_contents($plugin_dir . 'includes/' . $data['crud_name'] . '-functions.php', $crud_functions_file); // Creating plugin main file - file_put_contents($crud_dir . 'class-' . $data['crud_name'] . '-list-table.php', $crud_wp_list_table_file); + file_put_contents($plugin_dir . 'includes/' . 'class-' . $data['crud_name'] . '-list-table.php', $crud_wp_list_table_file); - if (!is_dir($crud_dir . 'views/')) { - mkdir($crud_dir . 'views/', 0777); + if (!is_dir($admin_dir . 'views/')) { + mkdir($admin_dir . 'views/', 0777); } // Creating plugin main file - file_put_contents($crud_dir . 'views/' . $data['crud_name'] . '.php', $crud_list_view_file); + file_put_contents($admin_dir . 'views/' . $data['crud_name'] . '.php', $crud_list_view_file); // Creating plugin main file - file_put_contents($crud_dir . 'views/' . $data['crud_name_singular'] . '-new.php', $crud_new_view_file); + file_put_contents($admin_dir . 'views/' . $data['crud_name_singular'] . '-new.php', $crud_new_view_file); // Creating plugin main file - file_put_contents($crud_dir . 'views/' . $data['crud_name_singular'] . '-edit.php', $crud_edit_view_file); + file_put_contents($admin_dir . 'views/' . $data['crud_name_singular'] . '-edit.php', $crud_edit_view_file); // Creating plugin main file - file_put_contents($crud_dir . 'views/' . $data['crud_name_singular'] . '-single.php', $crud_single_view_file); + file_put_contents($admin_dir . 'views/' . $data['crud_name_singular'] . '-single.php', $crud_single_view_file); zipDir($plugin_dir, $data['plugin_name_dash'] . '.zip', $download); diff --git a/stubs/class-admin-menu.stub b/stubs/class-admin-menu.stub new file mode 100755 index 0000000..51b7ec9 --- /dev/null +++ b/stubs/class-admin-menu.stub @@ -0,0 +1,72 @@ + '%%crud_name_cap%%', - 'default' => 20, - 'option' => '%%crud_name%%_per_page' - ); - - add_screen_option( $option, $args ); - - include dirname( __FILE__ ) . '/class-%%crud_name%%-list-table.php'; - $this->%%crud_name%%_obj = new %%crud_name_cap%%_List_Table(); - } - - /** - * Handle form submit data & delete record. - * - * @return void - */ - public function form_handler() { - if ( isset( $_GET['action'] ) && ( $_GET['action'] == 'new' || $_GET['action'] == 'edit' ) ) { - $this->%%crud_name%%_obj->process_form_submit(); - } - - if ( ( isset( $_GET['action'] ) && $_GET['action'] == 'delete' ) - || ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) - || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' ) - ) { - /** Process bulk action */ - $this->%%crud_name%%_obj->process_bulk_action(); - } - } -} \ No newline at end of file diff --git a/stubs/class-crud-name-list-table.stub b/stubs/class-crud-name-list-table.stub index 5084fa6..2e84d86 100755 --- a/stubs/class-crud-name-list-table.stub +++ b/stubs/class-crud-name-list-table.stub @@ -1,4 +1,7 @@ {$column_name}; default: return print_r( $item, true ); } @@ -51,7 +54,7 @@ class %%crud_name_cap%%_List_Table extends WP_List_Table { */ function column_cb( $item ) { return sprintf( - '', $item['id'] + '', $item->id ); } @@ -65,14 +68,14 @@ class %%crud_name_cap%%_List_Table extends WP_List_Table { function column_name( $item ) { $delete_nonce = wp_create_nonce( '%%prefix%%delete_%%crud_name_singular%%' ); - $title = '' . $item['name'] . ''; + $title = '' . $item->name . ''; $actions = [ - 'edit' => sprintf( 'Edit', esc_attr( $_REQUEST['page'] ), 'edit', absint( $item['id'] ) ), - 'delete' => sprintf( 'Delete', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['id'] ), $delete_nonce ) + 'edit' => sprintf( 'Edit', esc_attr( $_REQUEST['page'] ), 'edit', absint( $item->id ) ), + 'delete' => sprintf( 'Delete', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item->id ), $delete_nonce ) ]; - return sprintf( '%s', esc_attr( $_REQUEST['page'] ), 'view', absint( $item['id'] ), $title ) . $this->row_actions( $actions ); + return sprintf( '%s', esc_attr( $_REQUEST['page'] ), 'view', absint( $item->id ), $title ) . $this->row_actions( $actions ); } /** @@ -121,110 +124,38 @@ class %%crud_name_cap%%_List_Table extends WP_List_Table { * @return void */ public function prepare_items() { - $this->_column_headers = $this->get_column_info(); + $columns = $this->get_columns(); + $hidden = []; + $sortable = $this->get_sortable_columns(); - $per_page = $this->get_items_per_page( '%%crud_name%%_per_page', 5 ); + $this->_column_headers = array( $columns, $hidden, $sortable ); + + // Query parameters + $per_page = 20; $current_page = $this->get_pagenum(); - $total_items = %%prefix%%count_%%crud_name%%(); + $orderby = ( ! empty( $_REQUEST['orderby'] ) ) ? $_REQUEST['orderby'] : 'id'; + $order = ( ! empty( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : 'DESC'; + $s = ( ! empty( $_REQUEST['s'] ) ) ? $_REQUEST['s'] : ''; + $offset = ( $current_page - 1 ) * $per_page; + + $args = [ + 'number' => $per_page, + 'offset' => $offset, + 'orderby' => $orderby, + 'order' => $order, + 'count' => true, + 's' => $s, + ]; + + $total_items = %%prefix%%get_%%crud_name%%( $args ); $this->set_pagination_args( [ 'total_items' => $total_items, 'per_page' => $per_page ] ); - $this->items = %%prefix%%get_%%crud_name%%( $per_page, $current_page ); - } - - /** - * Handles bulk action and delete. - * - * @return void - */ - public function process_bulk_action() { - $page_url = menu_page_url( '%%plugin_page%%', false ); - - // Detect when a bulk action is being triggered... - if ( 'delete' === $this->current_action() ) { - // In our file that handles the request, verify the nonce. - $nonce = esc_attr( $_REQUEST['_wpnonce'] ); - - if ( ! wp_verify_nonce( $nonce, '%%prefix%%delete_%%crud_name_singular%%' ) ) { - die( 'Go get a life script kiddies' ); - } else { - %%prefix%%delete_%%crud_name_singular%%( absint( $_GET['id'] ) ); - - // Redirect - $query = array( 'message' => 'deleted'); - $redirect_to = add_query_arg( $query, $page_url ); - wp_redirect( $redirect_to ); - exit; - } - } - - // If the delete bulk action is triggered - if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) - || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' ) - ) { - $delete_ids = esc_sql( $_POST['bulk-delete'] ); - - // loop over the array of record ids and delete them - foreach ( $delete_ids as $id ) { - %%prefix%%delete_%%crud_name_singular%%( $id ); - } - - // Redirect - $query = array( 'message' => 'deleted'); - $redirect_to = add_query_arg( $query, $page_url ); - wp_redirect( $redirect_to ); - exit; - } - } - - /** - * Handles form data when submitted. - * - * @return void - */ - public function process_form_submit() { - if ( ! isset( $_POST['submit_%%crud_name_singular%%'] ) ) { - return; - } - - if ( ! wp_verify_nonce( $_POST['_wpnonce'], '%%prefix%%new_%%crud_name_singular%%' ) ) { - die( 'Go get a life script kiddies' ); - } - - if ( ! current_user_can( 'read' ) ) { - wp_die( __( 'Permission Denied!', '%%textdomain%%' ) ); - } - - $errors = array(); - $page_url = menu_page_url( '%%plugin_page%%', false ); - $field_id = isset( $_POST['field_id'] ) ? absint( $_POST['field_id'] ) : 0; - - %%form_submit_fields%% - - $fields = array( - %%form_fields%% - ); - - // New or edit? - if ( ! $field_id ) { - $insert_id = %%prefix%%insert_%%crud_name_singular%%( $fields ); - } else { - $fields['id'] = $field_id; - - $insert_id = %%prefix%%insert_%%crud_name_singular%%( $fields ); - } - - if ( is_wp_error( $insert_id ) ) { - $redirect_to = add_query_arg( array( 'message' => 'error' ), $page_url ); - } else { - $redirect_to = add_query_arg( array( 'message' => 'success' ), $page_url ); - } + unset( $args['count'] ); - // Redirect - wp_redirect( $redirect_to ); - exit; + $this->items = %%prefix%%get_%%crud_name%%( $args ); } } \ No newline at end of file diff --git a/stubs/class-form-handler.stub b/stubs/class-form-handler.stub new file mode 100644 index 0000000..8ae839f --- /dev/null +++ b/stubs/class-form-handler.stub @@ -0,0 +1,109 @@ + 'error' ), $page_url ); + } else { + $redirect_to = add_query_arg( array( 'message' => 'success' ), $page_url ); + } + + // Redirect + wp_redirect( $redirect_to ); + exit; + } + + /** + * Handles bulk action and delete. + * + * @return void + */ + public function handle_%%crud_name%%_bulk_action() { + $page_url = menu_page_url( '%%plugin_page%%', false ); + + // Detect when a bulk action is being triggered... + if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'delete' ) { + // In our file that handles the request, verify the nonce. + $nonce = esc_attr( $_REQUEST['_wpnonce'] ); + + if ( ! wp_verify_nonce( $nonce, '%%prefix%%delete_%%crud_name_singular%%' ) ) { + die( 'Go get a life script kiddies' ); + } else { + %%prefix%%delete_%%crud_name_singular%%( absint( $_REQUEST['id'] ) ); + + // Redirect + $query = array( 'message' => 'deleted'); + $redirect_to = add_query_arg( $query, $page_url ); + wp_redirect( $redirect_to ); + exit; + } + } + + // If the delete bulk action is triggered + if ( ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'bulk-delete' ) + || ( isset( $_REQUEST['action2'] ) && $_REQUEST['action2'] == 'bulk-delete' ) + ) { + $delete_ids = esc_sql( $_REQUEST['bulk-delete'] ); + + // loop over the array of record ids and delete them + foreach ( $delete_ids as $id ) { + %%prefix%%delete_%%crud_name_singular%%( $id ); + } + + // Redirect + $query = array( 'message' => 'deleted'); + $redirect_to = add_query_arg( $query, $page_url ); + wp_redirect( $redirect_to ); + exit; + } + } +} \ No newline at end of file diff --git a/stubs/crud-name-functions.stub b/stubs/crud-name-functions.stub index 207acdb..9f6e1b6 100755 --- a/stubs/crud-name-functions.stub +++ b/stubs/crud-name-functions.stub @@ -4,7 +4,7 @@ * * @param int $id * - * @return void + * @return object */ function %%prefix%%get_%%crud_name_singular%%( $id = 0 ) { global $wpdb; @@ -18,26 +18,46 @@ function %%prefix%%get_%%crud_name_singular%%( $id = 0 ) { * @param int $per_page * @param int $page_number * - * @return void + * @return array */ -function %%prefix%%get_%%crud_name%%( $per_page = 20, $page_number = 1 ) { +function %%prefix%%get_%%crud_name%%( $args = null ) { global $wpdb; - $sql = "SELECT * FROM {$wpdb->prefix}%%crud_name%%"; + $defaults = [ + 'number' => 20, + 'offset' => 0, + 'orderby' => 'id', + 'order' => 'DESC', + 'count' => false, + ]; - if ( ! empty( $_REQUEST['s'] ) ) { - $sql .= ' WHERE name LIKE "%' . esc_sql( $_REQUEST['s'] ) . '%"' ; + $args = wp_parse_args( $args, $defaults ); + + if ( $args['count'] ) { + $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}%%crud_name%%"; + } else { + $sql = "SELECT * FROM {$wpdb->prefix}%%crud_name%%"; } - if ( ! empty( $_REQUEST['orderby'] ) ) { - $sql .= ' ORDER BY ' . esc_sql( $_REQUEST['orderby'] ); - $sql .= ! empty( $_REQUEST['order'] ) ? ' ' . esc_sql( $_REQUEST['order'] ) : ' ASC'; + if ( ! empty( $args['s'] ) ) { + $sql .= ' WHERE name LIKE "%' . esc_sql( $args['s'] ) . '%"' ; } - $sql .= " LIMIT $per_page"; - $sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page; + if ( ! empty( $args['orderby'] ) ) { + $sql .= ' ORDER BY ' . esc_sql( $args['orderby'] ); + $sql .= ! empty( $args['order'] ) ? ' ' . esc_sql( $args['order'] ) : ' ASC'; + } - $result = $wpdb->get_results( $sql, 'ARRAY_A' ); + if ( $args['number'] != '-1' && ! $args['count'] ) { + $sql .= ' LIMIT ' . $args['number']; + $sql .= ' OFFSET ' . $args['offset']; + } + + if ( $args['count'] ) { + $result = $wpdb->get_var( $sql ); + } else { + $result = $wpdb->get_results( $sql); + } return $result; } @@ -59,23 +79,6 @@ function %%prefix%%delete_%%crud_name_singular%%( $id ) { ); } -/** - * Count %%crud_name%%. - * - * @return int - */ -function %%prefix%%count_%%crud_name%%() { - global $wpdb; - - $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}%%crud_name%%"; - - if ( ! empty( $_REQUEST['s'] ) ) { - $sql .= ' WHERE name LIKE "%' . esc_sql( $_REQUEST['s'] ) . '%"' ; - } - - return $wpdb->get_var( $sql ); -} - /** * Insert a new %%crud_name%%. * diff --git a/stubs/plugin-name.stub b/stubs/plugin-name.stub index c947d3c..67b4288 100755 --- a/stubs/plugin-name.stub +++ b/stubs/plugin-name.stub @@ -10,66 +10,67 @@ Author URI: %%plugin_author_url%% // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) { - exit; + exit; } class %%plugin_name_cap_u%% { - /** - * Instance of this class. - * - * @var static - */ - protected static $instance; + /** + * Instance of this class. + * + * @var static + */ + protected static $instance; - /** - * Constructor. - * - * @param mixed - */ - public function __construct() { - if ( is_admin() ) { - include dirname( __FILE__ ) . '/includes/%%crud_name%%/class-%%crud_name%%-admin-menu.php'; - include dirname( __FILE__ ) . '/includes/%%crud_name%%/%%crud_name%%-functions.php'; - new %%crud_name_cap%%_Admin_Menu(); - } + /** + * Constructor. + * + * @param mixed + */ + public function __construct() { + if ( is_admin() ) { + include dirname( __FILE__ ) . '/includes/admin/class-admin-menu.php'; + include dirname( __FILE__ ) . '/includes/%%crud_name%%-functions.php'; - register_activation_hook( __FILE__, array( $this, 'create_table' ) ); - } + new Admin_Menu(); + } - /** - * Singleton instance. - * - * @return object - */ - public static function get_instance() { - if ( ! isset( self::$instance ) ) { - self::$instance = new self(); - } + register_activation_hook( __FILE__, array( $this, 'create_table' ) ); + } - return self::$instance; - } + /** + * Singleton instance. + * + * @return object + */ + public static function get_instance() { + if ( ! isset( self::$instance ) ) { + self::$instance = new self(); + } - /** - * Create relevant table. - * - * @return mixed - */ - public function create_table() { - global $wpdb; + return self::$instance; + } - $charset_collate = $wpdb->get_charset_collate(); - $table_name = $wpdb->prefix . '%%crud_name%%'; + /** + * Create relevant table. + * + * @return mixed + */ + public function create_table() { + global $wpdb; - $sql = "CREATE TABLE IF NOT EXISTS $table_name ( - id int(11) NOT NULL AUTO_INCREMENT, - %%database_schema%% - date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, - PRIMARY KEY id (id) - ) $charset_collate;"; + $charset_collate = $wpdb->get_charset_collate(); + $table_name = $wpdb->prefix . '%%crud_name%%'; - require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); - dbDelta( $sql ); - } + $sql = "CREATE TABLE IF NOT EXISTS $table_name ( + id int(11) NOT NULL AUTO_INCREMENT, + %%database_schema%% + date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, + PRIMARY KEY id (id) + ) $charset_collate;"; + + require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); + dbDelta( $sql ); + } } diff --git a/stubs/views/crud-name.stub b/stubs/views/crud-name.stub index 1b0d996..15f0933 100755 --- a/stubs/views/crud-name.stub +++ b/stubs/views/crud-name.stub @@ -1,11 +1,13 @@