| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file contains Blocks functions. |
| 4 |
* |
| 5 |
* @package blockmanager. |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Block Manager Blocks class. |
| 10 |
* |
| 11 |
* @author ConnektMedia |
| 12 |
* @since 1.0 |
| 13 |
*/ |
| 14 |
class GBM_Blocks { |
| 15 |
|
| 16 |
/** |
| 17 |
* Get all disabled blocks. |
| 18 |
* |
| 19 |
* @author ConnektMedia |
| 20 |
* @since 1.0 |
| 21 |
* @return array |
| 22 |
*/ |
| 23 |
public static function gbm_get_disabled_blocks() { |
| 24 |
$blocks = get_option( BLOCK_MANAGER_OPTION, [] ); // Get disabled blocks. |
| 25 |
return ! empty( $blocks ) ? array_values( $blocks ) : []; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Get all filtered blocks. |
| 30 |
* |
| 31 |
* @author ConnektMedia |
| 32 |
* @since 1.1 |
| 33 |
* @return array |
| 34 |
*/ |
| 35 |
public static function gbm_get_filtered_blocks() { |
| 36 |
$blocks = apply_filters( 'gbm_disabled_blocks', [] ); // Get filtered disabled blocks. |
| 37 |
return ! empty( $blocks ) ? array_values( $blocks ) : []; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get all disabled blocks. |
| 42 |
* |
| 43 |
* @author ConnektMedia |
| 44 |
* @since 1.0 |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
public static function gbm_get_all_disabled_blocks() { |
| 48 |
$disabled = self::gbm_get_disabled_blocks(); |
| 49 |
$filtered = self::gbm_get_filtered_blocks(); |
| 50 |
$blocks = array_merge( $disabled, $filtered ); |
| 51 |
return ! empty( $blocks ) ? $blocks : []; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Remove any duplicate blocks when using blocks hook. |
| 56 |
* |
| 57 |
* @param array $options The blocks from WP options. |
| 58 |
* @param array $filtered The filtered blocks. |
| 59 |
* @return array Modified options. |
| 60 |
*/ |
| 61 |
public static function gbm_remove_duplicate_blocks( $options, $filtered ) { |
| 62 |
if ( $options && $filtered ) { |
| 63 |
$updated = false; |
| 64 |
foreach ( $filtered as $filter ) { |
| 65 |
// Search array for filtered block. |
| 66 |
$key = array_search( $filter, $options, 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_OPTION, $options ); |
| 75 |
} |
| 76 |
} |
| 77 |
return array_values( $options ); |
| 78 |
} |
| 79 |
} |
| 80 |
new GBM_Blocks(); |
| 81 |
|