| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Display |
| 5 |
* |
| 6 |
* This class is responsible for displaying the item based on specific conditions. |
| 7 |
* |
| 8 |
* @package WowPlugin |
| 9 |
* @subpackage Publish |
| 10 |
* @author Dmytro Lobov <dev@wow-company.com>, Wow-Company |
| 11 |
* @copyright 2024 Dmytro Lobov |
| 12 |
* @license GPL-2.0+ |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
namespace ButtonGenerator\Publish; |
| 17 |
|
| 18 |
defined( 'ABSPATH' ) || exit; |
| 19 |
|
| 20 |
class Display { |
| 21 |
|
| 22 |
private const POST_PREFIX = 'custom_post_'; |
| 23 |
|
| 24 |
public static function init( $id, $param ): bool { |
| 25 |
if ( self::can_abort_early( $id, $param ) ) { |
| 26 |
return false; |
| 27 |
} |
| 28 |
|
| 29 |
return self::check_shows( $param['show'], $param ); |
| 30 |
} |
| 31 |
|
| 32 |
private static function can_abort_early( $id, $param ): bool { |
| 33 |
return empty( $param ) || ! is_array( $param ) || empty( absint( $id ) ); |
| 34 |
} |
| 35 |
|
| 36 |
private static function check_shows( $showParams, $param ): bool { |
| 37 |
|
| 38 |
foreach ( $showParams as $i => $show ) { |
| 39 |
if ( str_contains( $show, self::POST_PREFIX ) && self::custom_post( $i, $param ) ) { |
| 40 |
return true; |
| 41 |
} |
| 42 |
|
| 43 |
if ( self::is_match( $show, $i, $param ) ) { |
| 44 |
return true; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
private static function is_match( $show, $i, $param ): bool { |
| 52 |
|
| 53 |
$cases = [ |
| 54 |
'everywhere' => 'check_everywhere', |
| 55 |
'post_all' => 'is_post', |
| 56 |
'post_selected' => 'post_selected', |
| 57 |
'post_category' => 'post_category', |
| 58 |
'post_tag' => 'post_tag', |
| 59 |
'page_all' => 'is_page', |
| 60 |
'page_type' => 'is_page_type', |
| 61 |
'page_selected' => 'page_selected', |
| 62 |
'is_archive' => 'is_archive_page', |
| 63 |
'is_category' => 'is_archive_page', |
| 64 |
'is_tag' => 'is_archive_page', |
| 65 |
'is_author' => 'is_archive_page', |
| 66 |
'is_date' => 'is_archive_page', |
| 67 |
'_is_category' => 'archive_page', |
| 68 |
'_is_tag' => 'archive_page', |
| 69 |
'_is_author' => 'archive_page', |
| 70 |
]; |
| 71 |
|
| 72 |
if ( ! isset( $cases[ $show ] ) ) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
$function = $cases[ $show ]; |
| 77 |
|
| 78 |
return self::$function( $i, $param ); |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
private static function check_everywhere(): bool { |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
private static function is_post(): bool { |
| 87 |
return is_singular( 'post' ); |
| 88 |
} |
| 89 |
|
| 90 |
private static function post_selected( $i, $param ): bool { |
| 91 |
$ids = explode( ',', $param['ids'][ $i ] ); |
| 92 |
$trimmed_ids = array_map( 'trim', $ids ); |
| 93 |
|
| 94 |
return (bool) $param['operator'][ $i ] === is_single( $trimmed_ids ); |
| 95 |
} |
| 96 |
|
| 97 |
private static function post_category( $i, $param ): bool { |
| 98 |
if ( ! is_single() ) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
$ids = explode( ',', $param['ids'][ $i ] ); |
| 103 |
$trimmed_ids = array_map( 'trim', $ids ); |
| 104 |
|
| 105 |
return (bool) $param['operator'][ $i ] === in_category( $trimmed_ids ); |
| 106 |
} |
| 107 |
|
| 108 |
private static function post_tag( $i, $param ): bool { |
| 109 |
|
| 110 |
if ( ! is_single() ) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
$ids = explode( ',', $param['ids'][ $i ] ); |
| 115 |
$trimmed_ids = array_map( 'trim', $ids ); |
| 116 |
|
| 117 |
return (bool) $param['operator'][ $i ] === has_tag( $trimmed_ids ); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
private static function is_page(): bool { |
| 122 |
return is_singular( 'page' ); |
| 123 |
} |
| 124 |
|
| 125 |
private static function is_page_type( $i, $param ): bool { |
| 126 |
|
| 127 |
return (bool) $param['operator'][ $i ] === call_user_func( $param['page_type'][ $i ] ); |
| 128 |
} |
| 129 |
|
| 130 |
private static function page_selected( $i, $param ): bool { |
| 131 |
if ( ! is_page() ) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
$ids = explode( ',', $param['ids'][ $i ] ); |
| 136 |
$trimmed_ids = array_map( 'trim', $ids ); |
| 137 |
|
| 138 |
return (bool) $param['operator'][ $i ] === is_page( $trimmed_ids ); |
| 139 |
} |
| 140 |
|
| 141 |
private static function is_archive_page( $i, $param ) { |
| 142 |
return call_user_func( $param['show'][ $i ] ); |
| 143 |
} |
| 144 |
|
| 145 |
private static function archive_page( $i, $param ): bool { |
| 146 |
$ids = explode( ',', $param['ids'][ $i ] ); |
| 147 |
$trimmed_ids = array_map( 'trim', $ids ); |
| 148 |
|
| 149 |
return (bool) $param['operator'][ $i ] === call_user_func( ltrim( $param['show'][ $i ], "_" ), $trimmed_ids ); |
| 150 |
} |
| 151 |
|
| 152 |
private static function custom_post( $i, $param ): bool { |
| 153 |
$show = $param['show'][ $i ]; |
| 154 |
$ids = explode( ',', $param['ids'][ $i ] ); |
| 155 |
$trimmed_ids = array_map( 'trim', $ids ); |
| 156 |
|
| 157 |
if ( str_contains( $show, 'custom_post_selected' ) ) { |
| 158 |
$post_type = str_replace( 'custom_post_selected_', '', $show ); |
| 159 |
if ( is_singular( $post_type ) ) { |
| 160 |
return (bool) $param['operator'][ $i ] === is_single( $trimmed_ids ); |
| 161 |
} |
| 162 |
} |
| 163 |
if ( str_contains( $show, 'custom_post_tax_' ) ) { |
| 164 |
|
| 165 |
$ids = preg_split( "/[,]+/", $param['ids'][ $i ] ); |
| 166 |
|
| 167 |
if ( is_single() ) { |
| 168 |
$taxonomy = explode( '|', $show )[1]; |
| 169 |
|
| 170 |
return (bool) $param['operator'][ $i ] === has_term( $ids, $taxonomy, get_the_ID() ); |
| 171 |
} |
| 172 |
|
| 173 |
return false; |
| 174 |
|
| 175 |
} |
| 176 |
if ( str_contains( $show, 'custom_post_taxonomy' ) ) { |
| 177 |
if ( is_tax() ) { |
| 178 |
$post_type = str_replace( 'custom_post_taxonomy_', '', $show ); |
| 179 |
$args = [ |
| 180 |
'object_type' => [ $post_type ] |
| 181 |
]; |
| 182 |
|
| 183 |
$taxonomies = get_taxonomies( $args ); |
| 184 |
$ids = preg_split( "/[,]+/", $param['ids'][ $i ] ); |
| 185 |
|
| 186 |
return (bool) $param['operator'][ $i ] === is_tax( $taxonomies, $ids ); |
| 187 |
} |
| 188 |
|
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
if ( str_contains( $show, 'custom_post_all' ) ) { |
| 193 |
$post_type = str_replace( 'custom_post_all_', '', $show ); |
| 194 |
|
| 195 |
return is_singular( $post_type ); |
| 196 |
} |
| 197 |
|
| 198 |
if ( str_contains( $show, 'custom_post_archive' ) ) { |
| 199 |
$post_type = str_replace( 'custom_post_archive_', '', $show ); |
| 200 |
|
| 201 |
return is_post_type_archive( $post_type ); |
| 202 |
} |
| 203 |
|
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
} |