*/ class TICA_Tools_Admin { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the admin area. * * @since 1.0.0 */ public function enqueue_styles() { wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/tica-tools-admin.css', $this->version, 'all' ); } /** * Register the JavaScript for the admin area. * @param $hook * * @since 1.0.0 */ public function enqueue_scripts( $hook ) { global $post; // add scripts to add/edit form if ( TICA_Tools::POST_TYPE == $post->post_type && in_array( $hook, [ 'post-new.php', 'post.php' ] ) ) { wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/tica-tools-edit.js', ['jquery'], $this->version, false ); } } public function register_tools_cpt() { // register tools post type $labels = array( 'name' => _x( 'Tools', 'Post Type General Name', 'e' ), 'singular_name' => _x( 'Tool', 'Post Type Singular Name', 'tica-tools' ), 'menu_name' => __( 'TICA Tools', 'tica-tools' ), 'name_admin_bar' => __( 'Tool', 'tica-tools' ), 'archives' => __( 'Tools', 'tica-tools' ), 'attributes' => __( 'Tool Attributes', 'tica-tools' ), 'parent_item_colon' => __( 'Parent Item:', 'tica-tools' ), 'all_items' => __( 'All Tools', 'tica-tools' ), 'add_new_item' => __( 'Add New Tool', 'tica-tools' ), 'add_new' => __( 'Add New', 'tica-tools' ), 'new_item' => __( 'New Tool', 'tica-tools' ), 'edit_item' => __( 'Edit Tool', 'tica-tools' ), 'update_item' => __( 'Update Tool', 'tica-tools' ), 'view_item' => __( 'View Tool', 'tica-tools' ), 'view_items' => __( 'View Tools', 'tica-tools' ), 'search_items' => __( 'Search Tools', 'tica-tools' ), 'not_found' => __( 'Not found', 'tica-tools' ), 'not_found_in_trash' => __( 'Not found in Trash', 'tica-tools' ), 'featured_image' => __( 'Featured Image', 'tica-tools' ), 'set_featured_image' => __( 'Set featured image', 'tica-tools' ), 'remove_featured_image' => __( 'Remove featured image', 'tica-tools' ), 'use_featured_image' => __( 'Use as featured image', 'tica-tools' ), 'insert_into_item' => __( /** @lang text */ 'Insert into Tool', 'tica-tools' ), 'uploaded_to_this_item' => __( 'Uploaded to this tools item', 'tica-tools' ), 'items_list' => __( 'Tools list', 'tica-tools' ), 'items_list_navigation' => __( 'Tools list navigation', 'tica-tools' ), 'filter_items_list' => __( 'Filter Tools list', 'tica-tools' ), ); $args = array( 'label' => __( 'Tools', 'tica-tools' ), 'description' => __( 'Tools Description', 'tica-tools' ), 'labels' => $labels, 'supports' => array( 'title', 'editor', 'page-attributes', 'thumbnail' ), 'hierarchical' => false, 'public' => false, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 30, 'menu_icon' => plugin_dir_url( __FILE__ ) . 'img/icon.png', 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => false, 'exclude_from_search' => true, 'publicly_queryable' => true, 'capability_type' => 'post', 'rewrite' => [] ); register_post_type( TICA_Tools::POST_TYPE, $args ); } public function posts_columns( $columns ) { $first_val = reset( $columns ); $first_key = key( $columns ); if ( 'cb' == $first_key ) { array_shift( $columns ); $columns = [ $first_key => $first_val, TICA_Tools::DEFAULT_ORDERBY => 'Order' ] + $columns; } else { $columns = [ TICA_Tools::DEFAULT_ORDERBY => 'Order' ] + $columns; } return $columns; } public function sortable_columns( $columns ) { $columns[TICA_Tools::DEFAULT_ORDERBY] = [ TICA_Tools::DEFAULT_ORDERBY, ( 'DESC' == strtoupper( TICA_Tools::DEFAULT_ORDER ) ) ]; return $columns; } public function posts_custom_column( $column, $post_id ) { global $post; esc_html_e( $post->{$column} ); } public function default_order() { if ( TICA_Tools::POST_TYPE == @$_REQUEST['post_type'] && empty( $_REQUEST['orderby'] ) ) { $url = get_admin_url( null, '/edit.php?' ); $url .= http_build_query( [ 'post_type' => TICA_Tools::POST_TYPE, 'orderby' => TICA_Tools::DEFAULT_ORDERBY, 'order' => TICA_Tools::DEFAULT_ORDER, ] ); wp_redirect( $url ); } } public function print_admin_styles() { if ( ! is_admin() ) { return; } global $pagenow; $column = TICA_Tools::DEFAULT_ORDERBY; if ( $pagenow == 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] == TICA_Tools::POST_TYPE ) { echo << STYLE; } } }