| 1 |
<?php |
| 2 |
|
| 3 |
namespace FloatingButton\Publisher; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
use FloatingButton\Dashboard\DBManager; |
| 8 |
|
| 9 |
class Display { |
| 10 |
|
| 11 |
public static function init(): array { |
| 12 |
|
| 13 |
$results = DBManager::get_all_data(); |
| 14 |
$arr = []; |
| 15 |
if(empty($results)) { |
| 16 |
return $arr; |
| 17 |
} |
| 18 |
foreach ( $results as $result ) { |
| 19 |
|
| 20 |
if ( Conditions::init( $result ) === false ) { |
| 21 |
continue; |
| 22 |
} |
| 23 |
|
| 24 |
$param = maybe_unserialize( $result->param ); |
| 25 |
|
| 26 |
$count = 0; |
| 27 |
if ( ! empty( $param['show'] ) && is_array( $param['show'] ) ) { |
| 28 |
$count = count( $param['show'] ); |
| 29 |
} |
| 30 |
|
| 31 |
if ( $count === 0 ) { |
| 32 |
$param['show'] = [ 0 => 'shortcode' ]; |
| 33 |
} |
| 34 |
|
| 35 |
$id = $result->id; |
| 36 |
|
| 37 |
if ( $count > 0 ) { |
| 38 |
for ( $i = 0; $i < $count; $i ++ ) { |
| 39 |
|
| 40 |
$show = $param['show'][ $i ]; |
| 41 |
if ( str_contains( $show, 'custom_post_' ) && self::custom_post( $i, $param ) ) { |
| 42 |
$arr[ $id ] = $result; |
| 43 |
continue; |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
switch ( $show ) { |
| 48 |
case 'everywhere': |
| 49 |
$arr[ $id ] = $result; |
| 50 |
break; |
| 51 |
case 'post_all': |
| 52 |
if ( is_singular( 'post' ) ) { |
| 53 |
$arr[ $id ] = $result; |
| 54 |
} |
| 55 |
break; |
| 56 |
case 'page_all': |
| 57 |
if ( is_singular( 'page' ) ) { |
| 58 |
$arr[ $id ] = $result; |
| 59 |
} |
| 60 |
break; |
| 61 |
case 'page_type': |
| 62 |
if ( (bool) $param['operator'][ $i ] === call_user_func( $param['page_type'][ $i ] ) ) { |
| 63 |
$arr[ $id ] = $result; |
| 64 |
} |
| 65 |
break; |
| 66 |
case 'page_selected': |
| 67 |
if ( self::page_selected( $i, $param ) === true ) { |
| 68 |
$arr[ $id ] = $result; |
| 69 |
} |
| 70 |
break; |
| 71 |
case 'post_selected': |
| 72 |
if ( self::post_selected( $i, $param ) === true ) { |
| 73 |
$arr[ $id ] = $result; |
| 74 |
} |
| 75 |
break; |
| 76 |
|
| 77 |
case 'post_category': |
| 78 |
if ( self::post_category( $i, $param ) === true ) { |
| 79 |
$arr[ $id ] = $result; |
| 80 |
|
| 81 |
} |
| 82 |
break; |
| 83 |
|
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
return $arr; |
| 94 |
} |
| 95 |
|
| 96 |
private static function custom_post( $i, $param ): bool { |
| 97 |
$show = $param['show'][ $i ]; |
| 98 |
$post = explode( '_', $param['show'][ $i ] )[3]; |
| 99 |
|
| 100 |
if ( str_contains( $show, 'custom_post_selected' ) ) { |
| 101 |
if ( is_singular( $post ) ) { |
| 102 |
return (bool) $param['operator'][ $i ] === is_single( explode( ',', $param['ids'][ $i ] ) ); |
| 103 |
} |
| 104 |
} |
| 105 |
if ( str_contains( $show, 'custom_post_tax' ) ) { |
| 106 |
|
| 107 |
$args = [ |
| 108 |
'object_type' => [ $post ] |
| 109 |
]; |
| 110 |
|
| 111 |
$taxonomies = get_taxonomies( $args ); |
| 112 |
$ids = preg_split( "/[,]+/", $param['ids'][ $i ] ); |
| 113 |
|
| 114 |
if ( is_single() ) { |
| 115 |
return (bool) $param['operator'][ $i ] === has_term( $ids, reset($taxonomies), get_the_ID() ); |
| 116 |
} |
| 117 |
|
| 118 |
if ( is_tax() ) { |
| 119 |
return (bool) $param['operator'][ $i ] === is_tax( $taxonomies, $ids ); |
| 120 |
} |
| 121 |
return false; |
| 122 |
|
| 123 |
|
| 124 |
} |
| 125 |
if ( str_contains( $show, 'custom_post_all' ) ) { |
| 126 |
return is_singular( $post ); |
| 127 |
} |
| 128 |
|
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
private static function post_category( $i, $param ): bool { |
| 133 |
|
| 134 |
if ( is_single() ) { |
| 135 |
return (bool) $param['operator'][ $i ] === in_category( explode( ',', $param['ids'][ $i ] ) ); |
| 136 |
} |
| 137 |
|
| 138 |
if ( is_category() ) { |
| 139 |
|
| 140 |
return (bool) $param['operator'][ $i ] === is_category( explode( ',', $param['ids'][ $i ] ) ); |
| 141 |
} |
| 142 |
|
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
private static function page_selected( $i, $param ): bool { |
| 147 |
if ( is_page() ) { |
| 148 |
$pages = preg_split( "/[,]+/", $param['ids'][ $i ] ); |
| 149 |
$pages_arr = map_deep( $pages, 'trim' ); |
| 150 |
|
| 151 |
return (bool) $param['operator'][ $i ] === is_page( $pages_arr ); |
| 152 |
} |
| 153 |
|
| 154 |
return false; |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
private static function post_selected( $i, $param ): bool { |
| 159 |
if ( is_singular( 'post' ) ) { |
| 160 |
return (bool) $param['operator'][ $i ] === is_single( explode( ',', $param['ids'][ $i ] ) ); |
| 161 |
} |
| 162 |
|
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
} |