From a5228809f2803e0c932b632ceecffc61ec8b78ce Mon Sep 17 00:00:00 2001 From: Bagus <25834188+contactjavas@users.noreply.github.com> Date: Tue, 23 Jul 2019 15:20:10 +0700 Subject: [PATCH] Initial upload --- .gitattributes | 2 + autoload.php | 33 +++ helpers/class-admin-ajax-helper.php | 66 ++++++ helpers/class-css-helper.php | 124 ++++++++++ helpers/class-file-helper.php | 196 ++++++++++++++++ helpers/class-image-size-helper.php | 88 +++++++ helpers/class-js-helper.php | 149 ++++++++++++ helpers/class-metabox-helper.php | 161 +++++++++++++ helpers/class-post-type-helper.php | 351 ++++++++++++++++++++++++++++ helpers/class-taxonomy-helper.php | 276 ++++++++++++++++++++++ utilities/class-admin-ajax.php | 37 +++ utilities/class-asset.php | 97 ++++++++ utilities/class-css.php | 37 +++ utilities/class-debug.php | 60 +++++ utilities/class-file-util.php | 87 +++++++ utilities/class-image-size.php | 27 +++ utilities/class-js.php | 108 +++++++++ utilities/class-metabox.php | 37 +++ utilities/class-post-type.php | 39 ++++ utilities/class-taxonomy.php | 39 ++++ utilities/class-vars.php | 60 +++++ wp-functions-wrapper.php | 25 ++ 22 files changed, 2099 insertions(+) create mode 100644 .gitattributes create mode 100644 autoload.php create mode 100644 helpers/class-admin-ajax-helper.php create mode 100644 helpers/class-css-helper.php create mode 100644 helpers/class-file-helper.php create mode 100644 helpers/class-image-size-helper.php create mode 100644 helpers/class-js-helper.php create mode 100644 helpers/class-metabox-helper.php create mode 100644 helpers/class-post-type-helper.php create mode 100644 helpers/class-taxonomy-helper.php create mode 100644 utilities/class-admin-ajax.php create mode 100644 utilities/class-asset.php create mode 100644 utilities/class-css.php create mode 100644 utilities/class-debug.php create mode 100644 utilities/class-file-util.php create mode 100644 utilities/class-image-size.php create mode 100644 utilities/class-js.php create mode 100644 utilities/class-metabox.php create mode 100644 utilities/class-post-type.php create mode 100644 utilities/class-taxonomy.php create mode 100644 utilities/class-vars.php create mode 100644 wp-functions-wrapper.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/autoload.php b/autoload.php new file mode 100644 index 0000000..b03d33e --- /dev/null +++ b/autoload.php @@ -0,0 +1,33 @@ +id++; + $this->item[ $this->id ] = []; + $this->item[ $this->id ]['action'] = $action; + $this->item[ $this->id ]['is_private'] = true; + return $this; + } + + /** + * Set the request as public + * + * @return object + */ + public function set_public() { + $this->item[ $this->id ]['is_private'] = false; + return $this; + } + + /** + * Set handler class for the request + * + * @param object $handler Class to handle the request. + * @return void + */ + public function set_handler( $handler ) { + add_action( 'wp_ajax_' . $this->item[ $this->id ]['action'], [ $handler, 'ajax' ] ); + + if ( ! $this->item[ $this->id ]['is_private'] ) { + add_action( 'wp_ajax_nopriv_' . $this->item[ $this->id ]['action'], [ $handler, 'ajax' ] ); + } + } +} diff --git a/helpers/class-css-helper.php b/helpers/class-css-helper.php new file mode 100644 index 0000000..e744800 --- /dev/null +++ b/helpers/class-css-helper.php @@ -0,0 +1,124 @@ +id++; + $this->item[ $this->id ] = []; + $this->item[ $this->id ]['handle'] = $handle; + return $this; + } + + /** + * Set the stylesheet url + * + * @param string $url Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. + * @return object + */ + public function set_url( $url ) { + $this->item[ $this->id ]['url'] = $url; + return $this; + } + + /** + * Set the stylesheet dependencies + * + * @param array $deps An array of registered stylesheet handles which this stylesheet depends on. + * @return object + */ + public function set_dependencies( $deps ) { + $this->item[ $this->id ]['deps'] = $deps; + return $this; + } + + /** + * Set the stylesheet version + * + * @param string $ver String specifying stylesheet version number. + * @return object + */ + public function set_version( $ver ) { + $this->item[ $this->id ]['ver'] = $ver; + return $this; + } + + /** + * Register the stylesheet to WP + * + * @return void + */ + public function save() { + $handle = $this->item[ $this->id ]['handle']; + $url = $this->item[ $this->id ]['url']; + $deps = isset( $this->item[ $this->id ]['deps'] ) ? $this->item[ $this->id ]['deps'] : []; + $ver = isset( $this->item[ $this->id ]['ver'] ) ? $this->item[ $this->id ]['ver'] : null; // remove default WordPress version for security. + $ver = $ver && 'auto' === $ver ? Asset::get_modified_time( $url ) : $ver; + + wp_register_style( $handle, $url, $deps, $ver ); + } + + /** + * Enqueue the stylesheet + * + * @param string $handle Name of the stylesheet. Should be unique. + * @return void + */ + public function enqueue( $handle = false ) { + if ( $handle ) { + wp_enqueue_style( $handle ); + } else { + $this->save(); + wp_enqueue_style( $this->item[ $this->id ]['handle'] ); + } + } + + /** + * Unregister a stylesheet. + * + * @param string $handle Name of the stylesheet to be un-registered. + * @return void + */ + public function deregister( $handle ) { + wp_deregister_style( $handle ); + } + + /** + * Dequeue a stylesheet. + * + * @param string $handle Name of the stylesheet to be de-queued. + * @return void + */ + public function dequeue( $handle ) { + wp_dequeue_style( $handle ); + } +} diff --git a/helpers/class-file-helper.php b/helpers/class-file-helper.php new file mode 100644 index 0000000..ac6d3a1 --- /dev/null +++ b/helpers/class-file-helper.php @@ -0,0 +1,196 @@ + + * @version 1.0.1 + * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ + * + * @param string $source Source path. + * @param string $dest Destination path. + * @param int $permissions New folder creation permissions. + * + * @return bool Returns true on success, false on failure + */ + public function copy( $source, $dest, $permissions = 0755 ) { + // check for symlinks. + if ( is_link( $source ) ) { + return symlink( readlink( $source ), $dest ); // phpcs:ignore + } + + // simple copy for a file. + if ( is_file( $source ) ) { + return copy( $source, $dest ); + } + + // make destination directory. + if ( ! is_dir( $dest ) ) { + mkdir( $dest, $permissions ); // phpcs:ignore + } + + // loop through the folder. + $dir = dir( $source ); + $entry = $dir->read(); + + while ( false !== $entry ) { + // skip pointers. + if ( '.' === $entry || '..' === $entry ) { + continue; + } + + // deep copy directories. + $this->copy( "$source/$entry", "$dest/$entry", $permissions ); + } + + // clean up. + $dir->close(); + return true; + } + + /** + * Thanks for PHP Community + * - http://php.net/manual/en/function.rmdir.php#110489 + * - http://php.net/manual/de/function.rmdir.php#98622 + * + * @param string $dir directory to be removed. + * + * @return bool + */ + public function remove_dir( $dir ) { + $files = array_diff( scandir( $dir ), array( '.', '..' ) ); + + foreach ( $files as $file ) { + ( is_dir( "$dir/$file" ) ) ? rmdir( "$dir/$file" ) : unlink( "$dir/$file" ); // phpcs:ignore + } + + return rmdir( $dir ); // phpcs:ignore + } + + /** + * Empty a directory + * + * @param string $dir The directory path. + * @return void + */ + public function empty_dir( $dir ) { + $files = array_diff( scandir( $dir ), array( '.', '..' ) ); + + foreach ( $files as $file ) { + ( is_dir( "$dir/$file" ) ) ? rmdir( "$dir/$file" ) : unlink( "$dir/$file" ); // phpcs:ignore + } + } + + /** + * Based on scripts by Paul Wenzel (https://github.com/pwenzel) + * on: Recursively include all PHP files (https://gist.github.com/pwenzel/3438784) + * + * @param string $type include/ require/ module . + * @param string $dir directory to scan. + * @param int $depth the depth level. + * @return void + */ + protected function scan_dir( $type, $dir, $depth ) { + if ( $depth > $this->max_scan_depth ) { + return; + } + + $scan = glob( $dir . '/*' ); + + foreach ( $scan as $path ) { + if ( 'module' === $type ) { + if ( is_dir( $path ) ) { + $this->scan_dir( 'module', $path, $depth + 1 ); + } else { + $pos = strpos( $path, 'autoload.php' ); + if ( false !== $pos ) { + require_once $path; + } + } + } else { + if ( preg_match( '/\.php$/', $path ) ) { + if ( 'include' === $type ) { + include_once $path; + } elseif ( 'require' === $type ) { + require_once $path; + } + } elseif ( is_dir( $path ) ) { + if ( 'include' === $type ) { + $this->scan_dir( 'include', $path, $depth + 1 ); + } elseif ( 'require' === $type ) { + $this->scan_dir( 'require', $path, $depth + 1 ); + } + } + } + } + } + + /** + * Load components + * + * @param string $dir The directory path. + * @param integer $max_depth Maximum depth to scan. + * @return void + */ + public function load_components( $dir, $max_depth = 1 ) { + if ( $max_depth < 1 ) { + $max_depth = 1; + } + + $this->max_scan_depth = $max_depth; + $this->scan_dir( 'module', $dir, 1 ); + } + + /** + * Include all files + * + * @param string $dir The directory path. + * @param integer $max_depth Maximum depth to scan. + * @return void + */ + public function include_all( $dir, $max_depth = 1 ) { + if ( $max_depth < 1 ) { + $max_depth = 1; + } + + $this->max_scan_depth = $max_depth; + $this->scan_dir( 'include', $dir, 1 ); + } + + /** + * Require all files + * + * @param string $dir The directory path. + * @param integer $max_depth Maximum depth to scan. + * @return void + */ + public function require_all( $dir, $max_depth = 1 ) { + if ( $max_depth < 1 ) { + $max_depth = 1; + } + + $this->max_scan_depth = $max_depth; + $this->scan_dir( 'require', $dir, 1 ); + } +} diff --git a/helpers/class-image-size-helper.php b/helpers/class-image-size-helper.php new file mode 100644 index 0000000..197493d --- /dev/null +++ b/helpers/class-image-size-helper.php @@ -0,0 +1,88 @@ +id++; + $this->item[ $this->id ] = []; + $this->item[ $this->id ]['name'] = $size_name; + return $this; + } + + /** + * Set the width + * + * @param int $width Image width in pixels. + * @return object + */ + public function set_width( $width ) { + $this->item[ $this->id ]['width'] = $width; + return $this; + } + + /** + * Set the height + * + * @param int $height Image height in pixels. + * @return object + */ + public function set_height( $height ) { + $this->item[ $this->id ]['height'] = $height; + return $this; + } + + /** + * Set the crop mode + * + * @param boolean $crop Whether to crop images to specified width and height or resize. + * @return object + */ + public function set_crop( $crop = true ) { + $this->item[ $this->id ]['crop'] = $crop; + return $this; + } + + /** + * Add the registered size + * + * @return void + */ + public function add() { + $name = $this->item[ $this->id ]['name']; + $width = isset( $this->item[ $this->id ]['width'] ) ? $this->item[ $this->id ]['width'] : 9999; + $height = isset( $this->item[ $this->id ]['height'] ) ? $this->item[ $this->id ]['height'] : 9999; + $crop = isset( $this->item[ $this->id ]['crop'] ) ? $this->item[ $this->id ]['crop'] : false; + + add_image_size( $name, $width, $height, $crop ); + } +} diff --git a/helpers/class-js-helper.php b/helpers/class-js-helper.php new file mode 100644 index 0000000..fe6e544 --- /dev/null +++ b/helpers/class-js-helper.php @@ -0,0 +1,149 @@ +id++; + $this->item[ $this->id ] = []; + $this->item[ $this->id ]['name'] = $handle; + return $this; + } + + /** + * Set the script url + * + * @param string $url Full URL of the script, or path of the script relative to the WordPress root directory. + * @return object + */ + public function set_url( $url ) { + $this->item[ $this->id ]['url'] = $url; + return $this; + } + + /** + * Set the script dependencies + * + * @param array $deps An array of registered script handles which this script depends on. + * @return object + */ + public function set_dependencies( $deps ) { + $this->item[ $this->id ]['deps'] = $deps; + return $this; + } + + /** + * Put the script inside head tag + * + * @return object + */ + public function put_on_header() { + $this->item[ $this->id ]['in_footer'] = false; + return $this; + } + + /** + * Put the script before the closing body tag + * + * @return object + */ + public function put_on_footer() { + $this->item[ $this->id ]['in_footer'] = true; + return $this; + } + + /** + * Set the script version + * + * @param string $ver String specifying script version number. + * @return object + */ + public function set_version( $ver ) { + $this->item[ $this->id ]['ver'] = $ver; + return $this; + } + + /** + * Localize global object to the script + * + * @param array $array Array of object name & object value. Object value can be either a single or multi-dimensional array. + * @return object + */ + public function set_global_object( $array ) { + $this->item[ $this->id ]['localize'] = [ + 'name' => $array['name'], + 'value' => $array['value'], + ]; + + return $this; + } + + /** + * Register the script to WP + * + * @return void + */ + public function save() { + $url = $this->item[ $this->id ]['url']; + $handle = $this->item[ $this->id ]['name']; + $deps = isset( $this->item[ $this->id ]['deps'] ) ? $this->item[ $this->id ]['deps'] : []; + $ver = isset( $this->item[ $this->id ]['ver'] ) ? $this->item[ $this->id ]['ver'] : null; + $ver = $ver && 'auto' === $ver ? Asset::get_modified_time( $url ) : $ver; + $in_footer = isset( $this->item[ $this->id ]['in_footer'] ) ? $this->item[ $this->id ]['in_footer'] : true; // put default to footer for non-blocking request. + $localize = isset( $this->item[ $this->id ]['localize'] ) ? $this->item[ $this->id ]['localize'] : false; + + wp_register_script( $handle, $url, $deps, $ver, $in_footer ); + + if ( $localize ) { + wp_localize_script( + $this->item[ $this->id ]['name'], + $this->item[ $this->id ]['localize']['name'], + $this->item[ $this->id ]['localize']['value'] + ); + } + } + + /** + * Enqueue the script + * + * @param string $handle Name of the script. Should be unique. + * @return void + */ + public function enqueue( $handle = false ) { + if ( $handle ) { + wp_enqueue_script( $handle ); + } else { + $this->save(); + wp_enqueue_script( $this->item[ $this->id ]['name'] ); + } + } +} diff --git a/helpers/class-metabox-helper.php b/helpers/class-metabox-helper.php new file mode 100644 index 0000000..2a0f94c --- /dev/null +++ b/helpers/class-metabox-helper.php @@ -0,0 +1,161 @@ +id++; + $this->item[ $this->id ] = []; + $this->item[ $this->id ]['name'] = $name; + + return $this; + } + + /** + * Set the metabox title + * + * @param string $title Title of the meta box. + * @return object + */ + public function set_title( $title ) { + $this->item[ $this->id ]['title'] = $title; + return $this; + } + + /** + * Display callback + * + * @param callable $display_callback Function that fills the box with the desired content. The function should echo its output. + * @return object + */ + public function render_display( $display_callback ) { + $this->item[ $this->id ]['display_callback'] = $display_callback; + return $this; + } + + /** + * Add metabox to specific screen + * + * @param string|array|WP_Screen $screen The screen(s) on which to show the box. + * @return object + */ + public function add_to_screen( $screen ) { + $this->item[ $this->id ]['screen'] = $screen; + return $this; + } + + /** + * Set screen where the boxes should be displayed + * + * @param string $screen_context The context within the screen ('normal', 'side', 'advanced'). + * @return object + */ + public function set_screen_context( $screen_context = 'advanced' ) { + $this->item[ $this->id ]['screen_context'] = $screen_context; + return $this; + } + + /** + * Set the screen context's priority where the boxes should show + * + * @param string $context_priority The priority within the context ('high', 'default', 'low'). + * @return object + */ + public function set_context_priority( $context_priority = 'default' ) { + $this->item[ $this->id ]['context_priority'] = $context_priority; + return $this; + } + + /** + * Set callback arguments + * + * @param array $callback_args Data that should be set as the $args property of the box array (which is the second parameter passed to the render_display). + * @return object + */ + public function set_callback_args( $callback_args = null ) { + $this->item[ $this->id ]['callback_args'] = $args; + return $this; + } + + /** + * Collect the arguments then add them to the metabox + * + * @return object + */ + public function save() { + $name = $this->item[ $this->id ]['name']; + $title = $this->item[ $this->id ]['title']; + $display_callback = $this->item[ $this->id ]['display_callback']; + $screen = $this->item[ $this->id ]['screen']; + $screen_context = isset( $this->item[ $this->id ]['screen_context'] ) ? $this->item[ $this->id ]['screen_context'] : 'advanced'; + $context_priority = isset( $this->item[ $this->id ]['context_priority'] ) ? $this->item[ $this->id ]['context_priority'] : 'default'; + $callback_args = isset( $this->item[ $this->id ]['callback_args'] ) ? $this->item[ $this->id ]['callback_args'] : null; + + add_meta_box( + $name, + $title, + $display_callback, + $screen, + $screen_context, + $context_priority, + $callback_args + ); + + return $this; + } + + /** + * Provide callback when processing the submission + * + * @param callable $submission_callback The function/ method to be called using 'save_post', or custom hook. + * @param string $submission_hook The hook where $onsave_callback is called. + * @return object + */ + public function set_submission_callback( $submission_callback, $submission_hook = 'save_post' ) { + $this->item[ $this->id ]['submission_callback'] = $submission_callback; + $this->item[ $this->id ]['submission_hook'] = $submission_hook; + return $this; + } + + /** + * Add the metabox + * + * @param string $hook The hook which the metabox will be registered in. E.g: 'add_meta_boxes', 'load-nav-menus.php'. + * @return void + */ + public function add( $hook = 'add_meta_boxes' ) { + add_action( $hook, [ $this, 'save' ] ); + $item = $this->item[ $this->id ]; + + if ( isset( $item['submission_callback'] ) && $item['submission_callback'] ) { + add_action( $item['submission_hook'], $item['submission_callback'] ); + } + } +} diff --git a/helpers/class-post-type-helper.php b/helpers/class-post-type-helper.php new file mode 100644 index 0000000..4dd4137 --- /dev/null +++ b/helpers/class-post-type-helper.php @@ -0,0 +1,351 @@ +id++; + $this->item[ $this->id ] = []; + + $this->item[ $this->id ]['args'] = []; + $this->item[ $this->id ]['args']['labels'] = []; + $this->item[ $this->id ]['singular_name'] = $singular_name; + $this->item[ $this->id ]['plural_name'] = $plural_name; + + return $this; + } + + /** + * Set the post type key + * This is the 1st parameter in "register_post_type" function. + * + * @param string $post_type_key The post type key. + * @return object + */ + public function set_id( $post_type_key ) { + $this->item[ $this->id ]['post_type_key'] = $post_type_key; + return $this; + } + + /** + * Set "labels" args + * + * @param array $array Array of labels. + * @return object + */ + public function set_labels( $array = [] ) { + if ( ! $array ) { + return $this; + } + + foreach ( $array as $key => $value ) { + $this->item[ $this->id ]['args']['labels'][ $key ] = $value; + } + + return $this; + } + + /** + * Set the post type description + * + * @param string $description Post type description. + * @return object + */ + public function set_description( $description ) { + $this->item[ $this->id ]['args']['description'] = $description; + return $this; + } + + /** + * Set "public" args as true + * + * @param boolean $is_public Whether to set the "public" args true or false. + * @return object + */ + public function set_public( $is_public = true ) { + $this->item[ $this->id ]['args']['public'] = $is_public; + return $this; + } + + /** + * Set "public" args as false + * + * @return object + */ + public function set_private() { + $this->item[ $this->id ]['args']['public'] = false; + return $this; + } + + /** + * Set the "hierarchical" args + * + * @param boolean $hierarchical Whether the post type is hierarchical (e.g. page). + * @return object + */ + public function set_hierarchical( $hierarchical = true ) { + $this->item[ $this->id ]['args']['hierarchical'] = $hierarchical; + return $this; + } + + /** + * Set "show_ui" args as true + * + * @param boolean $is_shown Whether to set the "show_ui" args true or false. + * @return object + */ + public function show_ui( $is_shown = true ) { + $this->item[ $this->id ]['args']['show_ui'] = $is_shown; + return $this; + } + + /** + * Set "show_ui" args as false + * + * @return object + */ + public function hide_ui() { + $this->item[ $this->id ]['args']['show_ui'] = false; + return $this; + } + + /** + * Set "show_in_menu" args as true + * + * @param boolean $is_shown Whether to set the "show_in_menu" args true or false. + * @return object + */ + public function show_in_menu( $is_shown = true ) { + $this->item[ $this->id ]['args']['show_in_menu'] = $is_shown; + return $this; + } + + /** + * Set "show_in_menu" args as false + * + * @return object + */ + public function hide_from_menu() { + $this->item[ $this->id ]['args']['show_in_menu'] = false; + return $this; + } + + /** + * Set the "supports" args + * + * @param array $supports Core feature(s) the post type supports. + * @return object + */ + public function set_supports( $supports ) { + $this->item[ $this->id ]['args']['supports'] = $supports; + return $this; + } + + /** + * Set the "menu_icon" args + * + * @param string $icon The url to the icon to be used for this menu. + * @return object + */ + public function set_icon( $icon ) { + $this->item[ $this->id ]['args']['menu_icon'] = $icon; + return $this; + } + + /** + * Set "has_archive" args to true + * + * @return object + */ + public function enable_archive() { + $this->item[ $this->id ]['args']['has_archive'] = true; + return $this; + } + + /** + * Set "has_archive" args to false + * + * @return object + */ + public function disable_archive() { + $this->item[ $this->id ]['args']['has_archive'] = false; + return $this; + } + + /** + * Set argument using key, value as param + * + * @param string $arg_name The argument name. + * @param mixed $arg_value The argument value. + * @return object + */ + public function set_argument( $arg_name, $arg_value ) { + $this->item[ $this->id ]['args'][ $arg_name ] = $arg_value; + return $this; + } + + /** + * Set arguments using array of key-value pair + * + * @param array $args The arguments (key-value pair). + * @return object + */ + public function set_arguments( $args ) { + if ( ! $args ) { + return; + } + if ( ! is_array( $args ) ) { + return; + } + + foreach ( $args as $arg_name => $arg_value ) { + $this->setArgument( $arg_name, $arg_value ); + } + + return $this; + } + + /** + * Set admin menu name + * Part of "labels" args + * + * @param string $menu_name Label for the menu name. + * @return object + */ + public function set_menu_name( $menu_name ) { + $this->item[ $this->id ]['menu_name'] = $menu_name; + return $this; + } + + /** + * Collect the arguments + * + * @return void + */ + public function save() { + $item = $this->item[ $this->id ]; + $singular_name = $item['singular_name']; + $plural_name = $item['plural_name']; + $post_type_key = isset( $item['post_type_key'] ) ? $item['post_type_key'] : strtolower( str_replace( ' ', '_', $singular_name ) ); + $menu_name = isset( $item['menu_name'] ) ? $item['menu_name'] : $plural_name; + + $args = $this->item[ $this->id ]['args']; + $labels = [ + 'name' => $plural_name, + 'singular_name' => $singular_name, + 'add_new' => 'Add New', + 'add_new_item' => 'Add New ' . $singular_name, + 'edit_item' => 'Edit ' . $singular_name, + 'new_item' => 'New ' . $singular_name, + 'view_item' => 'View ' . $singular_name, + 'view_items' => 'View ' . $plural_name, + 'search_items' => 'Search ' . $plural_name, + 'name_admin_bar' => $singular_name, + 'not_found' => 'Nothing found', + 'not_found_in_trash' => 'Nothing found in Trash', + 'parent_item_colon' => '', + 'all_items' => 'All ' . $plural_name, + 'archives' => $singular_name . ' Archives', + 'attributes' => $singular_name . ' Attributes', + 'insert_into_item ' => 'Insert into ' . $singular_name, + 'uploaded_to_this_item' => 'Uploaded to this ' . $singular_name, + 'menu_name' => $menu_name, + 'filter_items_list' => 'Filter' . $plural_name . 'list', + 'items_list_navigation' => $plural_name . ' list navigation', + 'items_list' => $plural_name . ' list', + ]; + + if ( $args['labels'] ) { + foreach ( $args['labels'] as $key => $value ) { + if ( $args['labels'][ $key ] ) { + $labels[ $key ] = $value; + } + } + } + + unset( $args['labels'] ); + $args['labels'] = $labels; + + register_post_type( $post_type_key, $args ); + } + + /** + * Add the post type + * + * @param string $hook The hook used to register the post type. + * @return void + */ + public function add( $hook = 'init' ) { + add_action( $hook, [ $this, 'save' ] ); + } +} diff --git a/helpers/class-taxonomy-helper.php b/helpers/class-taxonomy-helper.php new file mode 100644 index 0000000..2fe1408 --- /dev/null +++ b/helpers/class-taxonomy-helper.php @@ -0,0 +1,276 @@ +id++; + $this->item[ $this->id ] = []; + + $this->item[ $this->id ]['object_type'] = 'post'; + $this->item[ $this->id ]['args'] = []; + $this->item[ $this->id ]['args']['labels'] = []; + $this->item[ $this->id ]['singular_name'] = $singular_name; + $this->item[ $this->id ]['plural_name'] = $plural_name; + + return $this; + } + + /** + * Set the taxonomy type key + * This is the 1st parameter in "register_taxonomy" function. + * + * @param string $tax_key The taxonomy key. + * @return object + */ + public function set_id( $tax_key ) { + $this->item[ $this->id ]['tax_key'] = $tax_key; + return $this; + } + + /** + * Set "labels" args + * + * @param array $array Array of labels. + * @return object + */ + public function set_labels( $array = [] ) { + if ( ! $array ) { + return $this; + } + + foreach ( $array as $key => $value ) { + $this->item[ $this->id ]['args']['labels'][ $key ] = $value; + } + + return $this; + } + + /** + * Set the taxonomy description + * + * @param string $description Taxonomy description. + * @return object + */ + public function set_description( $description ) { + $this->item[ $this->id ]['args']['description'] = $description; + return $this; + } + + /** + * Set "public" args as true + * + * @param boolean $is_public Whether to set the "public" args true or false. + * @return object + */ + public function set_public( $is_public = true ) { + $this->item[ $this->id ]['args']['public'] = $is_public; + return $this; + } + + /** + * Set "public" args as false + * + * @return object + */ + public function set_private() { + $this->item[ $this->id ]['args']['public'] = false; + return $this; + } + + /** + * Set the "hierarchical" args + * + * @param boolean $hierarchical Whether the post type is hierarchical (e.g. page). + * @return object + */ + public function set_hierarchical( $hierarchical = true ) { + $this->item[ $this->id ]['args']['hierarchical'] = $hierarchical; + return $this; + } + + /** + * Set "show_ui" args as true + * + * @param boolean $is_shown Whether to set the "show_ui" args true or false. + * @return object + */ + public function show_ui( $is_shown = true ) { + $this->item[ $this->id ]['args']['show_ui'] = $is_shown; + return $this; + } + + /** + * Set "show_ui" args as false + * + * @return object + */ + public function hide_ui() { + $this->item[ $this->id ]['args']['show_ui'] = false; + return $this; + } + + /** + * Set "show_admin_column" args as true + * + * @param boolean $is_shown Whether to set the "show_admin_column" args true or false. + * @return object + */ + public function show_admin_column( $is_shown = true ) { + $this->item[ $this->id ]['args']['show_admin_column'] = $is_shown; + return $this; + } + + /** + * Set "show_admin_column" args as false + * + * @return object + */ + public function hide_admin_column() { + $this->item[ $this->id ]['args']['show_admin_column'] = false; + return $this; + } + + /** + * Set argument using key, value as param + * + * @param string $arg_name The argument name. + * @param mixed $arg_value The argument value. + * @return object + */ + public function set_argument( $arg_name, $arg_value ) { + $this->item[ $this->id ]['args'][ $arg_name ] = $arg_value; + return $this; + } + + /** + * Set arguments using array of key-value pair + * + * @param array $args The arguments (key-value pair). + * @return object + */ + public function set_arguments( $args ) { + if ( ! $args ) { + return; + } + if ( ! is_array( $args ) ) { + return; + } + + foreach ( $args as $arg_name => $arg_value ) { + $this->setArgument( $arg_name, $arg_value ); + } + + return $this; + } + + /** + * Set admin menu name + * Part of "labels" args + * + * @param string $menu_name Label for the menu name. + * @return object + */ + public function set_menu_name( $menu_name ) { + $this->item[ $this->id ]['menu_name'] = $menu_name; + return $this; + } + + /** + * Collect the arguments + * + * @return void + */ + public function save() { + $item = $this->item[ $this->id ]; + $singular_name = $item['singular_name']; + $plural_name = $item['plural_name']; + $tax_key = isset( $item['tax_key'] ) ? $item['tax_key'] : strtolower( str_replace( ' ', '_', $singular_name ) ); + $object_type = $item['object_type']; + $current_args = $item['args']; + $menu_name = isset( $item['menu_name'] ) ? $item['menu_name'] : $plural_name; + + $args = $item['args']; + $labels = [ + 'name' => $plural_name, + 'singular_name' => $singular_name, + 'search_items' => 'Search ' . $plural_name, + 'all_items' => 'All ' . $plural_name, + 'parent_item' => 'Parent ' . $singular_name, + 'parent_item_colon' => 'Parent ' . $singular_name . ' :', + 'edit_item' => 'Edit ' . $singular_name, + 'update_item' => 'Update ' . $singular_name, + 'add_new_item' => 'Add New ' . $singular_name, + 'new_item_name' => 'New ' . $singular_name . ' Name', + 'menu_name' => $menu_name, + ]; + + if ( $args['labels'] ) { + foreach ( $args['labels'] as $key => $value ) { + if ( $args['labels'][ $key ] ) { + $labels[ $key ] = $value; + } + } + } + + unset( $args['labels'] ); + $args['labels'] = $labels; + + register_taxonomy( $tax_key, $object_type, $args ); + } + + /** + * Add to object type + * + * @param string|array $object_type Object type or array of object types with which the taxonomy should be associated. + * @param string $hook The hook which taxonomy will be added in. + * @return void + */ + public function add_to_object_type( $object_type, $hook = 'init' ) { + $this->item[ $this->id ]['object_type'] = $object_type; + add_action( $hook, [ $this, 'save' ] ); + } + + /** + * Beautiful alias for "add_to_object_type" + * + * @param string|array $object_type Object type or array of object types with which the taxonomy should be associated. + * @param string $hook The hook which taxonomy will be added in. + * @return void + */ + public function add_to_post_type( $object_type, $hook = 'init' ) { + $this->add_to_object_type( $object_type, $hook ); + } +} diff --git a/utilities/class-admin-ajax.php b/utilities/class-admin-ajax.php new file mode 100644 index 0000000..a399e22 --- /dev/null +++ b/utilities/class-admin-ajax.php @@ -0,0 +1,37 @@ +register( $action ); + } + + /** + * Beautiful alias for "register" + * + * @param string $action The action to be registered to wp_ajax_ . + * @return object + */ + public static function make( $action ) { + return self::register( $action ); + } +} diff --git a/utilities/class-asset.php b/utilities/class-asset.php new file mode 100644 index 0000000..43ba095 --- /dev/null +++ b/utilities/class-asset.php @@ -0,0 +1,97 @@ +register( $handle ); + } + + /** + * Enqueue a stylesheet + * + * @param string $handle Name of the stylesheet. Should be unique. + * @return void + */ + public static function enqueue( $handle ) { + wp_enqueue_style( $handle ); + } +} diff --git a/utilities/class-debug.php b/utilities/class-debug.php new file mode 100644 index 0000000..45d6809 --- /dev/null +++ b/utilities/class-debug.php @@ -0,0 +1,60 @@ +'; // phpcs:ignore + if ( 'var_dump' === $mode ) { + var_dump( $var ); // phpcs:ignore + } elseif ( 'print_r' === $mode ) { + print_r($var); // phpcs:ignore + } + echo ''; // phpcs:ignore + } + + /** + * Print variable as JSON + * + * @param mixed $var The variable to be printed. + * @return void + */ + public static function print_as_json( $var ) { + $var = wp_json_encode( $var, JSON_PRETTY_PRINT ); + echo '
'; // phpcs:ignore
+		echo $var; // phpcs:ignore
+		echo '
'; // phpcs:ignore + } + + /** + * Log the variable + * + * @todo Handle the $file param + * + * @param mixed $var The variable to be logged. + * @param boolean $file Whether to log it tp debug.log or custom file location. + * @return void + */ + public static function log( $var, $file = false ) { + error_log( print_r( $var, true ) ); // phpcs:ignore + } +} diff --git a/utilities/class-file-util.php b/utilities/class-file-util.php new file mode 100644 index 0000000..5703f63 --- /dev/null +++ b/utilities/class-file-util.php @@ -0,0 +1,87 @@ +copy( $source, $dest, $permissions ); + } + + /** + * Remove the whole directory + * + * @param string $dir The directory path. + * @return bool + */ + public static function remove_dir( $dir ) { + $object = new File_Helper(); + return $object->remove_dir( $dir ); + } + + /** + * Empty a directory + * + * @param string $dir The directory path. + * @return void + */ + public static function empty_dir( $dir ) { + $object = new File_Helper(); + $object->empty_dir( $dir ); + } + + /** + * Load components + * + * @param string $dir The directory path. + * @param integer $max_depth Maximum depth to load the components. + * @return void + */ + public static function load_components( $dir, $max_depth = 1 ) { + $object = new File_Helper(); + $object->load_components( $dir, $max_depth ); + } + + /** + * Include all files in specific directory + * + * @param string $dir The directory path. + * @param integer $max_depth Maximum depth to include. + * @return void + */ + public static function include_all( $dir, $max_depth = 1 ) { + $object = new File_Helper(); + $object->include_all( $dir, $max_depth ); + } + + /** + * Require all files in specific directory + * + * @param string $dir The directory path. + * @param integer $max_depth Maximum depth to require. + * @return void + */ + public static function require_all( $dir, $max_depth = 1 ) { + $object = new File_Helper(); + $object->require_all( $dir, $max_depth ); + } +} diff --git a/utilities/class-image-size.php b/utilities/class-image-size.php new file mode 100644 index 0000000..b920c5d --- /dev/null +++ b/utilities/class-image-size.php @@ -0,0 +1,27 @@ +register( $size_name ); + } +} diff --git a/utilities/class-js.php b/utilities/class-js.php new file mode 100644 index 0000000..896ca0e --- /dev/null +++ b/utilities/class-js.php @@ -0,0 +1,108 @@ +register( $handle ); + } + + /** + * Localize global object to a specific script + * Wrapper for wp_localize_script + * + * @param string $handle Script handle which the data will be attached to. + * @param string $object_name Name for the JavaScript object. + * @param array $object_value The data which can be either a single or multi-dimensional array. + * @return void + */ + public static function localize_global_object( $handle, $object_name, $object_value ) { + wp_localize_script( $handle, $object_name, $object_value ); + } + + /** + * Add global object to a specific script + * Wrapper for wp_localize_script + * + * @param string $handle Script handle which the data will be attached to. + * @param string $object_name Name for the JavaScript object. + * @param array $object_value The data which can be either a single or multi-dimensional array. + * @return void + */ + public static function add_global_object( $handle, $object_name, $object_value ) { + wp_localize_script( $handle, $object_name, $object_value ); + } + + /** + * Output global object instead of localizing it to specific script + * + * @see See https://developer.wordpress.org/reference/classes/wp_scripts/localize/ + * + * @param string $object_name Name for the JavaScript object. + * @param array $object_value The data which can be either a single or multi-dimensional array. + * @return void + */ + public static function print_global_object( $object_name, $object_value ) { + foreach ( $object_value as $key => $value ) { + if ( ! is_scalar( $value ) ) { + continue; + } + $object_value[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); + } + + $script = ''; + + // phpcs:ignore + echo $script; + } + + /** + * Enqueue a script + * + * @param string $handle Name of the script. Should be unique. + * @return void + */ + public static function enqueue( $handle ) { + wp_enqueue_script( $handle ); + } + + /** + * Deregister a script. + * + * @param string $handle Name of the script to be deregistered. + * @return void + */ + public static function deregister( $handle ) { + wp_deregister_script( $handle ); + } + + /** + * Dequeue a script. + * + * @param string $handle Name of the script to be de-queued. + * @return void + */ + public static function dequeue( $handle ) { + wp_dequeue_script( $handle ); + } +} diff --git a/utilities/class-metabox.php b/utilities/class-metabox.php new file mode 100644 index 0000000..6071c66 --- /dev/null +++ b/utilities/class-metabox.php @@ -0,0 +1,37 @@ +register( $name ); + } + + /** + * Beautiful alias for "register" + * + * @param string $name The metabox id. + * @return object + */ + public static function make( $name ) { + return self::register( $name ); + } +} diff --git a/utilities/class-post-type.php b/utilities/class-post-type.php new file mode 100644 index 0000000..a8ab93f --- /dev/null +++ b/utilities/class-post-type.php @@ -0,0 +1,39 @@ +register( $singular_name, $plural_name ); + } + + /** + * Beautiful alias for "register" + * + * @param string $singular_name The singular name. + * @param string $plural_name The plural name. + * @return object + */ + public static function make( $singular_name, $plural_name = '' ) { + return self::register( $singular_name, $plural_name ); + } +} diff --git a/utilities/class-taxonomy.php b/utilities/class-taxonomy.php new file mode 100644 index 0000000..258c433 --- /dev/null +++ b/utilities/class-taxonomy.php @@ -0,0 +1,39 @@ +register( $singular_name, $plural_name ); + } + + /** + * Beautiful alias for "register" + * + * @param string $singular_name The singular name. + * @param string $plural_name The plural name. + * @return object + */ + public static function make( $singular_name, $plural_name = '' ) { + return self::register( $singular_name, $plural_name ); + } +} diff --git a/utilities/class-vars.php b/utilities/class-vars.php new file mode 100644 index 0000000..4e055a0 --- /dev/null +++ b/utilities/class-vars.php @@ -0,0 +1,60 @@ + $value ) { + self::$vars[ $key ] = $value; + } + } else { + self::$vars[ $name ] = $value; + } + } +} diff --git a/wp-functions-wrapper.php b/wp-functions-wrapper.php new file mode 100644 index 0000000..043f64c --- /dev/null +++ b/wp-functions-wrapper.php @@ -0,0 +1,25 @@ +