Skip to content

Commit

Permalink
Settings for all meta fields, sortable by default
Browse files Browse the repository at this point in the history
  • Loading branch information
zahardev committed Jul 30, 2021
1 parent 4cee082 commit 3bbd472
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 61 deletions.
86 changes: 65 additions & 21 deletions app/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,55 @@ public function add_plugin_page() {
public function init_settings() {
register_setting( self::SETTINGS_URL, self::SETTINGS_URL );

//Next, add settings for taxonomies
foreach ( get_post_types() as $post_type ) {
$fields = ACF_Helper::get_post_type_fields( $post_type );

if ( $fields ) {
$this->init_fields_settings( $post_type, $fields );
//Init ACF Fields settings
if ( $fields = ACF_Helper::get_acf_fields( $post_type ) ) {
$this->init_acf_fields_settings( $post_type, $fields );
}

$taxonomies = $this->get_taxonomies( $post_type );
//Init settings for other meta fields
$this->init_meta_fields_settings( $post_type );

if ( $taxonomies ) {
//Init taxonomy settings
if ( $taxonomies = $this->get_taxonomies( $post_type ) ) {
$this->init_tax_settings( $post_type, $taxonomies );
}
}
}

/**
* @param string $post_type
* @param int $posts_to_check
*
* @return array
*/
public static function get_post_type_meta_keys( $post_type, $posts_to_check = 20 ) {
$meta_keys = array();
$posts = get_posts( array( 'post_type' => $post_type, 'limit' => $posts_to_check ) );

foreach ( $posts as $post ) {
$post_meta_keys = get_post_custom_keys( $post->ID );
if ( ! empty( $post_meta_keys ) ) {
$meta_keys = array_merge( $meta_keys, $post_meta_keys );
}
}

return array_values( array_unique( $meta_keys ) );
}

/**
* @param string $post_type
* @param array $taxonomies
*/
protected function init_tax_settings( $post_type, $taxonomies ) {
$this->add_settings_section( $post_type, 'tax' );
$setting_type = 'tax';
$this->add_settings_section( $post_type, $setting_type );
foreach ( $taxonomies as $tax ) {
foreach ( $this->options_map() as $option_name => $label ) {
$option_label = sprintf( $label, $tax->label );
$is_supported = Settings_Helper::is_supported( $option_name, 'tax' );
$this->add_settings_field( $post_type, 'tax', $tax->name, $option_name, $option_label, $is_supported );
$is_supported = Settings_Helper::is_supported( $option_name, $setting_type );
$this->add_settings_checkbox( $post_type, 'tax', $tax->name, $option_name, $option_label, $is_supported );
}
}
}
Expand Down Expand Up @@ -130,13 +153,34 @@ protected function get_taxonomies( $post_type ) {
* @param string $post_type
* @param array $fields
*/
protected function init_fields_settings( $post_type, $fields ) {
$this->add_settings_section( $post_type, 'fields' );
protected function init_acf_fields_settings( $post_type, $fields ) {
$setting_type = 'fields';
$this->add_settings_section( $post_type, $setting_type );
foreach ( $fields as $k => $field ) {
foreach ( $this->options_map() as $option_name => $label ) {
$option_label = sprintf( $label, $field['label'] );
$is_supported = Settings_Helper::is_supported( $option_name, 'fields', $field['type'] );
$this->add_settings_field( $post_type, 'fields', $field['name'], $option_name, $option_label, $is_supported );
$is_supported = Settings_Helper::is_supported( $option_name, $setting_type, $field['type'] );
$this->add_settings_checkbox( $post_type, $setting_type, $field['name'], $option_name, $option_label, $is_supported );
}
}
}

/**
* @param string $post_type
*/
protected function init_meta_fields_settings( $post_type ) {
$setting_type = 'meta_fields';
$meta_fields = self::get_post_type_meta_keys( $post_type );
if ( empty( $meta_fields ) ) {
return;
}

$this->add_settings_section( $post_type, $setting_type );

foreach ( $meta_fields as $meta_field ) {
foreach ( $this->options_map() as $option_name => $label ) {
$option_label = sprintf( $label, Settings_Helper::get_meta_field_name( $meta_field ) );
$this->add_settings_checkbox( $post_type, $setting_type, $meta_field, $option_name, $option_label, true );
}
}
}
Expand Down Expand Up @@ -165,11 +209,12 @@ protected function add_settings_section( $post_type, $type ) {
*/
protected function get_settings_section_title( $post_label, $type ) {
$name_map = [
'tax' => 'taxonomies',
'fields' => 'meta fields',
'tax' => 'taxonomies',
'fields' => 'acf fields',
'meta_fields' => 'meta fields',
];

return __( sprintf( 'Manage %s %s', $post_label, $name_map[ $type ] ), 'wordpress' );
return __( sprintf( '%s %s columns', $post_label, $name_map[ $type ] ), 'wordpress' );
}


Expand All @@ -178,10 +223,10 @@ protected function get_settings_section_title( $post_label, $type ) {
*/
public function options_map() {
return [
'show_in_column' => 'Show %s in column',
'filter' => 'Filter by %s',
'show_in_column' => '%s',
/* 'filter' => 'Filter by %s',
'is_numeric' => 'Is numeric',
'sort' => 'Sort by %s',
'sort' => 'Sort by %s',*/
];
}

Expand All @@ -193,7 +238,7 @@ public function options_map() {
* @param string $option_label
* @param bool $is_supported
*/
public function add_settings_field( $post_type, $type, $field_name, $option_name, $option_label, $is_supported ) {
public function add_settings_checkbox( $post_type, $type, $field_name, $option_name, $option_label, $is_supported ) {
$settings = $this->get_settings();
$settings_page = self::SETTINGS_URL;
add_settings_field(
Expand Down Expand Up @@ -261,7 +306,6 @@ public function get_settings() {
public function get_post_settings( $post_type = null ) {
$settings = $this->get_settings();


if ( empty( $post_type ) ) {
$current_screen = get_current_screen();
$post_type = $current_screen->post_type;
Expand Down
5 changes: 2 additions & 3 deletions app/helpers/class-acf-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class ACF_Helper {

protected static $post_type_fields;

public static function get_post_type_fields( $post_type = 'post' ) {
public static function get_acf_fields( $post_type = 'post' ) {
if ( ! function_exists( 'acf_get_field_groups' ) ) {
return [];
}
if ( !isset( self::$post_type_fields[ $post_type ] ) ) {
if ( ! isset( self::$post_type_fields[ $post_type ] ) ) {
$field_groups = acf_get_field_groups( [ 'post_type' => $post_type ] );
$fields = [];
if ( empty( $field_groups ) ) {
Expand All @@ -22,7 +22,6 @@ public static function get_post_type_fields( $post_type = 'post' ) {
self::$post_type_fields[ $post_type ] = $fields;
}


return self::$post_type_fields[ $post_type ];
}

Expand Down
14 changes: 4 additions & 10 deletions app/helpers/class-settings-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,10 @@ public static function is_supported( $action, $type, $field_type = null ) {
],
];

if ( empty( $supported[ $type ][ $action ] ) ) {
return false;
}

$is_supported = $supported[ $type ][ $action ];

if ( is_bool( $is_supported ) ) {
return $is_supported;
}
return ! empty( $supported[ $type ][ $action ] );
}

return ! empty( $is_supported[ $field_type ] );
public static function get_meta_field_name($meta_field){
return ucfirst( trim( str_replace( '_', ' ', $meta_field ) ) );
}
}
23 changes: 12 additions & 11 deletions app/managers/class-abstract-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PCM\Entities\Column;
use PCM\Helpers\ACF_Helper;
use PCM\Helpers\Settings_Helper;

abstract class Abstract_Manager {
/**
Expand All @@ -15,18 +16,18 @@ abstract class Abstract_Manager {
* @return Column
*/
protected function get_column( $type, $name ) {
if ( 'tax' === $type ) {
$taxonomy = get_taxonomy( $name );

return new Column( $taxonomy->name, $taxonomy->label );
}
switch ($type) {
case 'tax':
$taxonomy = get_taxonomy( $name );
return new Column( $taxonomy->name, $taxonomy->label );

if ( $acf_field = ACF_Helper::acf_get_field( $name ) ) {
$column = new Column( $name, $acf_field['label'] );
} else {
$column = new Column( $name, $name );
}
case 'field':
$acf_field = ACF_Helper::acf_get_field( $name );
return new Column( $name, $acf_field['label'] );

return $column;
default:
return new Column( $name, Settings_Helper::get_meta_field_name( $name ) );
}
}
}
}
8 changes: 3 additions & 5 deletions app/managers/class-columns-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function init_manager() {
foreach ( $post_types as $post_type ) {
$settings = $this->get_columns_settings( $post_type );
if( $settings ){
add_filter( "manage_{$post_type}_posts_columns", [ $this, 'manage_posts_columns' ], 100 );
add_filter( "manage_{$post_type}_posts_columns", [ $this, 'manage_posts_columns' ], 5 );
}
}

Expand All @@ -41,9 +41,7 @@ public function sortable_columns( $columns ) {

foreach ( $columns_settings as $type => $type_settings ) {
foreach ( $type_settings as $field_name => $column_settings ) {
if ( ! empty( $column_settings['sort'] ) ) {
$columns[ $field_name ] = $field_name;
}
$columns[ $field_name ] = $field_name;
}
}

Expand Down Expand Up @@ -78,7 +76,7 @@ public function echo_column_value( $column_name ) {
return;
}

foreach ( [ 'fields', 'tax' ] as $type ) {
foreach ( [ 'meta_fields', 'fields', 'tax' ] as $type ) {
if ( ! empty( $columns_settings[ $type ] ) ) {
foreach ( $columns_settings[ $type ] as $field_name => $column_settings ) {
if ( $column_name != $field_name || ! $column_settings['show_in_column'] ) {
Expand Down
20 changes: 9 additions & 11 deletions app/managers/class-filters-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@

class Filters_Manager extends Abstract_Manager {

/**
* @var
*/
private $fields;

/**
* @var Settings $settings
*/
Expand Down Expand Up @@ -78,7 +73,10 @@ private function get_tax_meta_query( $tax ) {

private function get_fields_meta_queries( $type_group, $meta_query ) {
foreach ( $type_group as $field_name => $column_settings ) {
if ( ! $column_settings['filter'] ) {

$filter = $this->get_query_val( $field_name );

if ( empty( $filter ) ) {
continue;
}

Expand All @@ -89,7 +87,7 @@ private function get_fields_meta_queries( $type_group, $meta_query ) {
}

if ( $query ) {
$meta_query = array_merge( $meta_query, $query);
$meta_query = array_merge( $meta_query, $query );
}
}

Expand Down Expand Up @@ -124,7 +122,7 @@ private function get_number_meta_query( $field_name ) {
}

private function get_text_meta_query( $field_name ) {
$val = $this->get_query_val( $field_name );
$val = $this->get_query_val( $field_name );
$meta_query = [];

if ( $val ) {
Expand All @@ -146,20 +144,20 @@ public function show_filters() {

foreach ( $columns_settings as $type => $type_group ) {
foreach ( $type_group as $name => $column_settings ) {
if ( ! $column_settings['filter'] ) {
if ( empty( $column_settings['filter'] ) ) {
continue;
}

if ( 'tax' === $type ) {
$tax = get_taxonomy($name);
$tax = get_taxonomy( $name );
wp_dropdown_categories( [
'hide_empty' => true,
'show_option_all' => 'All from ' . $tax->label,
'taxonomy' => $name,
'hierarchical' => 1,
'name' => $name,
'selected' => $this->get_query_val( $name ),
'value_field' => 'slug'
'value_field' => 'slug',
] );
continue;
}
Expand Down

0 comments on commit 3bbd472

Please sign in to comment.