| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file contains Categories functions. |
| 4 |
* |
| 5 |
* @package blockmanager. |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Block Manager Categories class. |
| 10 |
* |
| 11 |
* @author ConnektMedia |
| 12 |
* @since 1.0 |
| 13 |
*/ |
| 14 |
class GBM_Categories { |
| 15 |
|
| 16 |
/** |
| 17 |
* Get all updated categories. |
| 18 |
* |
| 19 |
* @author ConnektMedia |
| 20 |
* @since 1.2 |
| 21 |
* @return array |
| 22 |
*/ |
| 23 |
public static function gbm_get_block_categories() { |
| 24 |
$categories = get_option( BLOCK_MANAGER_CATEGORIES, [] ); // Get option. |
| 25 |
return $categories ? array_values( $categories ) : []; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Get all filtered categories. |
| 30 |
* |
| 31 |
* @author ConnektMedia |
| 32 |
* @since 2.0 |
| 33 |
* @return array |
| 34 |
*/ |
| 35 |
public static function gbm_get_filtered_categories() { |
| 36 |
$blocks = apply_filters( 'gbm_block_categories', [] ); // Get filtered block categories. |
| 37 |
return ! empty( $blocks ) ? array_values( $blocks ) : []; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get all block categories. |
| 42 |
* |
| 43 |
* @author ConnektMedia |
| 44 |
* @since 2.0 |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
public static function gbm_get_all_block_categories() { |
| 48 |
$updated = self::gbm_get_block_categories(); |
| 49 |
$filtered = self::gbm_get_filtered_categories(); |
| 50 |
$blocks = array_merge( $updated, $filtered ); |
| 51 |
return ! empty( $blocks ) ? $blocks : []; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Remove any duplicate block categories when using category hook. |
| 56 |
* |
| 57 |
* @param array $options The categories from WP options. |
| 58 |
* @param array $filtered The filtered categories. |
| 59 |
* @return array Modified options. |
| 60 |
*/ |
| 61 |
public static function gbm_remove_duplicate_categories( $options, $filtered ) { |
| 62 |
if ( $options && $filtered ) { |
| 63 |
$updated = false; |
| 64 |
foreach ( $filtered as $filter ) { |
| 65 |
// Search array for filtered category. |
| 66 |
$key = array_search( $filter['block'], array_column( $options, 'block' ), true ); |
| 67 |
if ( $key !== false ) { |
| 68 |
unset( $options[ $key ] ); // Remove filtered item from array. |
| 69 |
$options = array_values( $options ); // Reset array keys. |
| 70 |
$updated = true; |
| 71 |
} |
| 72 |
} |
| 73 |
if ( $updated ) { |
| 74 |
update_option( BLOCK_MANAGER_CATEGORIES, $options ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return array_values( $options ); |
| 79 |
} |
| 80 |
} |
| 81 |
new GBM_Categories(); |
| 82 |
|