isset( $s['types'] ) && is_array( $s['types'] ) ? $s['types'] : array(), 'taxes' => isset( $s['taxes'] ) && is_array( $s['taxes'] ) ? $s['taxes'] : array(), ); } private static function put( array $store ) { update_option( self::OPTION, $store, true ); } public static function type_defaults() { return array( 'name' => '', 'plural' => '', 'singular' => '', 'description' => '', 'public' => 1, 'hierarchical' => 0, 'has_archive' => 1, 'archive_slug' => '', 'show_in_rest' => 1, 'show_in_menu' => 1, 'show_in_nav_menus' => 1, 'exclude_from_search' => 0, 'menu_icon' => 'dashicons-admin-post', 'menu_position' => 25, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'revisions' ), 'taxonomies' => array(), 'rewrite_slug' => '', 'with_front' => 1, 'capability_type' => 'post', 'delete_with_user' => 0, ); } public static function tax_defaults() { return array( 'name' => '', 'plural' => '', 'singular' => '', 'description' => '', 'post_types' => array(), 'hierarchical' => 1, 'public' => 1, 'show_in_rest' => 1, 'show_admin_column' => 1, 'show_in_nav_menus' => 1, 'show_tagcloud' => 1, 'rewrite_slug' => '', 'with_front' => 1, 'rewrite_hier' => 0, ); } /* ------------------------------ registering ------------------------------ */ public static function register() { $store = self::store(); foreach ( $store['types'] as $def ) { self::register_type( array_merge( self::type_defaults(), $def ) ); } foreach ( $store['taxes'] as $def ) { self::register_tax( array_merge( self::tax_defaults(), $def ) ); } } /** * The arguments for register_post_type(). * * @param array $d Definition. * @return array */ public static function type_args( array $d ) { $slug = '' !== $d['rewrite_slug'] ? $d['rewrite_slug'] : $d['name']; return array( 'labels' => self::type_labels( $d['singular'], $d['plural'] ), 'description' => $d['description'], 'public' => (bool) $d['public'], 'publicly_queryable' => (bool) $d['public'], 'show_ui' => true, 'show_in_menu' => (bool) $d['show_in_menu'], 'show_in_nav_menus' => (bool) $d['show_in_nav_menus'], 'show_in_admin_bar' => (bool) $d['show_in_menu'], 'show_in_rest' => (bool) $d['show_in_rest'], 'exclude_from_search' => (bool) $d['exclude_from_search'], 'hierarchical' => (bool) $d['hierarchical'], 'has_archive' => $d['has_archive'] ? ( '' !== $d['archive_slug'] ? $d['archive_slug'] : true ) : false, 'menu_icon' => $d['menu_icon'], 'menu_position' => (int) $d['menu_position'], 'supports' => $d['supports'] ? array_values( $d['supports'] ) : false, 'taxonomies' => array_values( (array) $d['taxonomies'] ), 'rewrite' => $d['public'] ? array( 'slug' => $slug, 'with_front' => (bool) $d['with_front'], ) : false, 'query_var' => (bool) $d['public'], 'capability_type' => 'page' === $d['capability_type'] ? 'page' : 'post', 'map_meta_cap' => true, 'delete_with_user' => (bool) $d['delete_with_user'], ); } public static function tax_args( array $d ) { $slug = '' !== $d['rewrite_slug'] ? $d['rewrite_slug'] : $d['name']; return array( 'labels' => self::tax_labels( $d['singular'], $d['plural'], (bool) $d['hierarchical'] ), 'description' => $d['description'], 'hierarchical' => (bool) $d['hierarchical'], 'public' => (bool) $d['public'], 'publicly_queryable' => (bool) $d['public'], 'show_ui' => true, 'show_in_menu' => true, 'show_in_rest' => (bool) $d['show_in_rest'], 'show_admin_column' => (bool) $d['show_admin_column'], 'show_in_nav_menus' => (bool) $d['show_in_nav_menus'], 'show_tagcloud' => (bool) $d['show_tagcloud'], 'query_var' => (bool) $d['public'], 'rewrite' => $d['public'] ? array( 'slug' => $slug, 'with_front' => (bool) $d['with_front'], 'hierarchical' => (bool) $d['rewrite_hier'], ) : false, ); } private static function register_type( array $d ) { if ( '' === $d['name'] || ( post_type_exists( $d['name'] ) && ! self::is_ours( 'type', $d['name'] ) ) ) { return; } register_post_type( $d['name'], self::type_args( $d ) ); } private static function register_tax( array $d ) { if ( '' === $d['name'] || ( taxonomy_exists( $d['name'] ) && ! self::is_ours( 'tax', $d['name'] ) ) ) { return; } register_taxonomy( $d['name'], array_values( (array) $d['post_types'] ), self::tax_args( $d ) ); } private static function is_ours( $kind, $name ) { if ( 'type' === $kind ) { $obj = get_post_type_object( $name ); } else { $obj = get_taxonomy( $name ); } $store = self::store(); return $obj && isset( $store[ 'type' === $kind ? 'types' : 'taxes' ][ $name ] ) && ! $obj->_builtin; } public static function type_labels( $one, $many ) { return array( 'name' => $many, 'singular_name' => $one, 'menu_name' => $many, 'all_items' => sprintf( /* translators: %s: many */ __( 'All %s', 'ai-guardwall' ), $many ), 'add_new' => __( 'Add New', 'ai-guardwall' ), 'add_new_item' => sprintf( /* translators: %s: one */ __( 'Add New %s', 'ai-guardwall' ), $one ), 'edit_item' => sprintf( /* translators: %s: one */ __( 'Edit %s', 'ai-guardwall' ), $one ), 'new_item' => sprintf( /* translators: %s: one */ __( 'New %s', 'ai-guardwall' ), $one ), 'view_item' => sprintf( /* translators: %s: one */ __( 'View %s', 'ai-guardwall' ), $one ), 'view_items' => sprintf( /* translators: %s: many */ __( 'View %s', 'ai-guardwall' ), $many ), 'search_items' => sprintf( /* translators: %s: many */ __( 'Search %s', 'ai-guardwall' ), $many ), 'not_found' => sprintf( /* translators: %s: many */ __( 'No %s found', 'ai-guardwall' ), $many ), 'not_found_in_trash' => sprintf( /* translators: %s: many */ __( 'No %s found in Trash', 'ai-guardwall' ), $many ), 'parent_item_colon' => sprintf( /* translators: %s: one */ __( 'Parent %s:', 'ai-guardwall' ), $one ), 'archives' => sprintf( /* translators: %s: one */ __( '%s Archives', 'ai-guardwall' ), $one ), 'attributes' => sprintf( /* translators: %s: one */ __( '%s Attributes', 'ai-guardwall' ), $one ), 'insert_into_item' => sprintf( /* translators: %s: one */ __( 'Insert into %s', 'ai-guardwall' ), $one ), 'uploaded_to_this_item' => sprintf( /* translators: %s: one */ __( 'Uploaded to this %s', 'ai-guardwall' ), $one ), 'filter_items_list' => sprintf( /* translators: %s: many */ __( 'Filter %s list', 'ai-guardwall' ), $many ), 'items_list' => sprintf( /* translators: %s: many */ __( '%s list', 'ai-guardwall' ), $many ), 'item_published' => sprintf( /* translators: %s: one */ __( '%s published.', 'ai-guardwall' ), $one ), 'item_updated' => sprintf( /* translators: %s: one */ __( '%s updated.', 'ai-guardwall' ), $one ), ); } public static function tax_labels( $one, $many, $hierarchical ) { $labels = array( 'name' => $many, 'singular_name' => $one, 'menu_name' => $many, 'all_items' => sprintf( /* translators: %s: many */ __( 'All %s', 'ai-guardwall' ), $many ), 'edit_item' => sprintf( /* translators: %s: one */ __( 'Edit %s', 'ai-guardwall' ), $one ), 'view_item' => sprintf( /* translators: %s: one */ __( 'View %s', 'ai-guardwall' ), $one ), 'update_item' => sprintf( /* translators: %s: one */ __( 'Update %s', 'ai-guardwall' ), $one ), 'add_new_item' => sprintf( /* translators: %s: one */ __( 'Add New %s', 'ai-guardwall' ), $one ), 'new_item_name' => sprintf( /* translators: %s: one */ __( 'New %s Name', 'ai-guardwall' ), $one ), 'search_items' => sprintf( /* translators: %s: many */ __( 'Search %s', 'ai-guardwall' ), $many ), 'not_found' => sprintf( /* translators: %s: many */ __( 'No %s found', 'ai-guardwall' ), $many ), 'back_to_items' => sprintf( /* translators: %s: many */ __( '← Back to %s', 'ai-guardwall' ), $many ), ); if ( $hierarchical ) { $labels['parent_item'] = sprintf( /* translators: %s: one */ __( 'Parent %s', 'ai-guardwall' ), $one ); $labels['parent_item_colon'] = sprintf( /* translators: %s: one */ __( 'Parent %s:', 'ai-guardwall' ), $one ); } else { $labels['popular_items'] = sprintf( /* translators: %s: many */ __( 'Popular %s', 'ai-guardwall' ), $many ); $labels['separate_items_with_commas'] = sprintf( /* translators: %s: many */ __( 'Separate %s with commas', 'ai-guardwall' ), strtolower( $many ) ); $labels['add_or_remove_items'] = sprintf( /* translators: %s: many */ __( 'Add or remove %s', 'ai-guardwall' ), strtolower( $many ) ); $labels['choose_from_most_used'] = sprintf( /* translators: %s: many */ __( 'Choose from the most used %s', 'ai-guardwall' ), strtolower( $many ) ); } return $labels; } /* ------------------------------ saving ------------------------------ */ private static function clean_name( $name, $max ) { $name = strtolower( (string) $name ); $name = preg_replace( '/[^a-z0-9_-]/', '', str_replace( ' ', '_', $name ) ); return substr( $name, 0, $max ); } private static function flag( $value ) { return empty( $value ) || 'false' === $value || '0' === $value ? 0 : 1; } /** * Add or change a post type or a taxonomy. * * @param string $kind 'type' or 'tax'. * @param array $in Fields. * @param string $original The name before a rename ('' for a new one). * @param bool $move When renamed, move the existing items to the new name. * @return array|WP_Error */ public static function save( $kind, array $in, $original = '', $move = true ) { $is_type = 'type' === $kind; $store = self::store(); $key = $is_type ? 'types' : 'taxes'; $d = $is_type ? self::type_defaults() : self::tax_defaults(); $name = self::clean_name( isset( $in['name'] ) ? $in['name'] : '', $is_type ? 20 : 32 ); $orig = self::clean_name( $original, 32 ); if ( '' === $name ) { return new WP_Error( 'svabg_cpt', $is_type ? __( 'Give the post type a name of up to 20 lower-case letters, digits, dashes or underscores, such as "product" or "team_member".', 'ai-guardwall' ) : __( 'Give the taxonomy a name of up to 32 lower-case letters, digits, dashes or underscores, such as "genre".', 'ai-guardwall' ) ); } if ( in_array( $name, self::RESERVED, true ) ) { return new WP_Error( 'svabg_cpt', sprintf( /* translators: %s: name */ __( '"%s" is a name WordPress uses itself. Pick another one.', 'ai-guardwall' ), $name ) ); } $taken = $is_type ? post_type_exists( $name ) : taxonomy_exists( $name ); if ( $taken && ! isset( $store[ $key ][ $name ] ) ) { return new WP_Error( 'svabg_cpt', sprintf( /* translators: %s: name */ __( '"%s" is already registered by WordPress, the theme or another plugin.', 'ai-guardwall' ), $name ) ); } if ( $name !== $orig && isset( $store[ $key ][ $name ] ) ) { return new WP_Error( 'svabg_cpt', sprintf( /* translators: %s: name */ __( 'You already have one called "%s".', 'ai-guardwall' ), $name ) ); } $d['name'] = $name; $d['singular'] = sanitize_text_field( isset( $in['singular'] ) ? (string) $in['singular'] : '' ); $d['plural'] = sanitize_text_field( isset( $in['plural'] ) ? (string) $in['plural'] : '' ); $d['singular'] = '' !== $d['singular'] ? $d['singular'] : ucwords( str_replace( array( '_', '-' ), ' ', $name ) ); $d['plural'] = '' !== $d['plural'] ? $d['plural'] : $d['singular'] . 's'; $d['description'] = sanitize_textarea_field( isset( $in['description'] ) ? (string) $in['description'] : '' ); $d['rewrite_slug'] = trim( preg_replace( '#[^a-z0-9_/-]#', '', strtolower( isset( $in['rewrite_slug'] ) ? (string) $in['rewrite_slug'] : '' ) ), '/' ); if ( $is_type ) { foreach ( array( 'public', 'hierarchical', 'has_archive', 'show_in_rest', 'show_in_menu', 'show_in_nav_menus', 'exclude_from_search', 'with_front', 'delete_with_user' ) as $f ) { if ( isset( $in[ $f ] ) ) { $d[ $f ] = self::flag( $in[ $f ] ); } } $d['archive_slug'] = trim( preg_replace( '#[^a-z0-9_/-]#', '', strtolower( isset( $in['archive_slug'] ) ? (string) $in['archive_slug'] : '' ) ), '/' ); $d['menu_icon'] = isset( $in['menu_icon'] ) && preg_match( '/^dashicons-[a-z0-9-]+$/', (string) $in['menu_icon'] ) ? (string) $in['menu_icon'] : 'dashicons-admin-post'; $d['menu_position'] = isset( $in['menu_position'] ) ? max( 1, min( 200, (int) $in['menu_position'] ) ) : 25; $d['supports'] = array_values( array_intersect( self::SUPPORTS, (array) ( isset( $in['supports'] ) ? $in['supports'] : array() ) ) ); $d['taxonomies'] = array_values( array_filter( array_map( 'sanitize_key', (array) ( isset( $in['taxonomies'] ) ? $in['taxonomies'] : array() ) ) ) ); $d['capability_type'] = isset( $in['capability_type'] ) && 'page' === $in['capability_type'] ? 'page' : 'post'; } else { foreach ( array( 'hierarchical', 'public', 'show_in_rest', 'show_admin_column', 'show_in_nav_menus', 'show_tagcloud', 'with_front', 'rewrite_hier' ) as $f ) { if ( isset( $in[ $f ] ) ) { $d[ $f ] = self::flag( $in[ $f ] ); } } $d['post_types'] = array_values( array_filter( array_map( 'sanitize_key', (array) ( isset( $in['post_types'] ) ? $in['post_types'] : array() ) ) ) ); } $moved = 0; if ( '' !== $orig && $orig !== $name && isset( $store[ $key ][ $orig ] ) ) { unset( $store[ $key ][ $orig ] ); if ( $move ) { $moved = self::rename_content( $kind, $orig, $name, $store ); } } $store[ $key ][ $name ] = $d; self::put( $store ); // Make it live in this request too, then rebuild the permalinks. if ( $is_type ) { self::register_type( $d ); } else { self::register_tax( $d ); } flush_rewrite_rules( false ); Softvolt_Ai_Bodyguard_Logger::log( 'info', 'cpt_saved', sprintf( $is_type ? /* translators: %s: value */ __( 'Post type saved: %s', 'ai-guardwall' ) : /* translators: %s: value */ __( 'Taxonomy saved: %s', 'ai-guardwall' ), $name ) ); return array( 'item' => $d, 'message' => $moved ? sprintf( /* translators: %d: moved */ _n( 'Saved. %d item was moved to the new name.', 'Saved. %d items were moved to the new name.', $moved, 'ai-guardwall' ), $moved ) : __( 'Saved.', 'ai-guardwall' ), ); } /** * Move posts (or terms) from an old name to a new one, and fix the references. */ private static function rename_content( $kind, $old, $new, array &$store ) { global $wpdb; if ( 'type' === $kind ) { $n = (int) $wpdb->update( $wpdb->posts, array( 'post_type' => $new ), array( 'post_type' => $old ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ( $store['taxes'] as &$tax ) { $tax['post_types'] = array_values( array_unique( str_replace( $old, $new, (array) $tax['post_types'] ) ) ); } unset( $tax ); clean_post_cache( 0 ); wp_cache_flush(); return $n; } $n = (int) $wpdb->update( $wpdb->term_taxonomy, array( 'taxonomy' => $new ), array( 'taxonomy' => $old ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ( $store['types'] as &$type ) { $type['taxonomies'] = array_values( array_unique( str_replace( $old, $new, (array) $type['taxonomies'] ) ) ); } unset( $type ); wp_cache_flush(); return $n; } public static function delete( $kind, $name ) { $store = self::store(); $key = 'type' === $kind ? 'types' : 'taxes'; $name = sanitize_key( $name ); if ( ! isset( $store[ $key ][ $name ] ) ) { return new WP_Error( 'svabg_cpt', __( 'It is not there any more.', 'ai-guardwall' ) ); } unset( $store[ $key ][ $name ] ); self::put( $store ); if ( 'type' === $kind ) { unregister_post_type( $name ); } else { unregister_taxonomy( $name ); } flush_rewrite_rules( false ); Softvolt_Ai_Bodyguard_Logger::log( 'info', 'cpt_deleted', sprintf( 'type' === $kind ? /* translators: %s: value */ __( 'Post type removed: %s', 'ai-guardwall' ) : /* translators: %s: value */ __( 'Taxonomy removed: %s', 'ai-guardwall' ), $name ) ); return array( 'message' => __( 'Removed. The content stays in the database and comes back if you add the same name again.', 'ai-guardwall' ) ); } /* ------------------------------ the screen ------------------------------ */ public static function overview() { global $wpdb; $store = self::store(); $types = array(); foreach ( $store['types'] as $name => $d ) { $d = array_merge( self::type_defaults(), $d ); $d['count'] = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = %s AND post_status NOT IN ('auto-draft','trash')", $name ) ); $d['edit'] = admin_url( 'edit.php?post_type=' . $name ); $types[] = $d; } $taxes = array(); foreach ( $store['taxes'] as $name => $d ) { $d = array_merge( self::tax_defaults(), $d ); $d['count'] = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->term_taxonomy} WHERE taxonomy = %s", $name ) ); $first = $d['post_types'] ? $d['post_types'][0] : 'post'; $d['edit'] = admin_url( 'edit-tags.php?taxonomy=' . $name . ( 'post' !== $first ? '&post_type=' . $first : '' ) ); $taxes[] = $d; } $all_types = array(); foreach ( get_post_types( array( 'show_ui' => true ), 'objects' ) as $n => $obj ) { if ( in_array( $n, array( 'wp_block', 'wp_navigation', 'wp_template', 'wp_template_part', 'wp_font_family', 'wp_font_face', 'wp_global_styles' ), true ) ) { continue; } $all_types[ $n ] = $obj->labels->name; } $all_taxes = array(); foreach ( get_taxonomies( array( 'show_ui' => true ), 'objects' ) as $n => $obj ) { $all_taxes[ $n ] = $obj->labels->name; } return array( 'types' => $types, 'taxes' => $taxes, 'all_types' => $all_types, 'all_taxes' => $all_taxes, ); } public static function icons() { return array( 'admin-post', 'admin-page', 'admin-media', 'admin-links', 'admin-users', 'admin-home', 'admin-site', 'admin-generic', 'admin-tools', 'portfolio', 'book', 'book-alt', 'products', 'cart', 'store', 'money-alt', 'tag', 'category', 'calendar', 'calendar-alt', 'clock', 'location', 'location-alt', 'building', 'groups', 'businessman', 'businesswoman', 'id', 'id-alt', 'awards', 'star-filled', 'heart', 'thumbs-up', 'testimonial', 'format-quote', 'format-gallery', 'format-image', 'format-video', 'format-audio', 'video-alt3', 'microphone', 'camera', 'images-alt2', 'media-document', 'media-spreadsheet', 'clipboard', 'list-view', 'feedback', 'megaphone', 'email', 'phone', 'car', 'airplane', 'palmtree', 'food', 'coffee', 'pets', 'carrot', 'hammer', 'lightbulb', 'welcome-learn-more', 'welcome-widgets-menus', 'analytics', 'chart-bar', 'chart-pie', 'shield', 'lock', 'admin-network', 'cloud', 'database', 'desktop', 'smartphone', 'games', 'sos', 'universal-access', 'translation', 'tickets-alt', 'buddicons-activity' ); } /* ------------------------------ export ------------------------------ */ public static function export_json() { return array( 'generator' => 'AI GuardWall ' . SOFTVOLT_AI_BODYGUARD_VERSION, 'cpt' => self::store(), ); } public static function import_json( $json ) { $data = json_decode( (string) $json, true ); if ( ! is_array( $data ) || ! isset( $data['cpt'] ) || ! is_array( $data['cpt'] ) ) { return new WP_Error( 'svabg_cpt', __( 'This is not a post types export file.', 'ai-guardwall' ) ); } $done = 0; $skip = array(); foreach ( array( 'types' => 'type', 'taxes' => 'tax' ) as $key => $kind ) { foreach ( isset( $data['cpt'][ $key ] ) && is_array( $data['cpt'][ $key ] ) ? $data['cpt'][ $key ] : array() as $def ) { if ( ! is_array( $def ) ) { continue; } $exists = isset( self::store()[ $key ][ isset( $def['name'] ) ? $def['name'] : '' ] ); $result = self::save( $kind, $def, $exists ? $def['name'] : '' ); if ( is_wp_error( $result ) ) { $skip[] = ( isset( $def['name'] ) ? $def['name'] : '?' ) . ': ' . $result->get_error_message(); } else { ++$done; } } } return array( 'message' => sprintf( /* translators: %d: done */ _n( '%d definition imported.', '%d definitions imported.', $done, 'ai-guardwall' ), $done ), 'skipped' => $skip, ); } /** * The same definitions as PHP code for a theme or a plugin. * * @return string */ public static function php_code() { $store = self::store(); $out = " $v ) { $out .= $pad . ( $list ? '' : var_export( $k, true ) . ' => ' ) . self::export_value( $v, $depth + 1 ) . ",\n"; // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export -- see php_code(). } return $out . str_repeat( "\t", $depth ) . ')'; } }