| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace FL\Assistant\Data\Repository; |
| 5 |
|
| 6 |
|
| 7 |
/** |
| 8 |
* Class CodeRepository |
| 9 |
* @package FL\Assistant\Data\Repository |
| 10 |
*/ |
| 11 |
class CodeRepository { |
| 12 |
|
| 13 |
/** |
| 14 |
* @param array $args |
| 15 |
* |
| 16 |
* @return \WP_Query |
| 17 |
*/ |
| 18 |
public function query( array $args = [] ) { |
| 19 |
$args = array_merge( |
| 20 |
$args, [ |
| 21 |
'post_status' => 'published', |
| 22 |
'perm' => 'editable', |
| 23 |
'post_type' => 'fl_code', |
| 24 |
] |
| 25 |
); |
| 26 |
|
| 27 |
return new \WP_Query( $args ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get array of code with the corresponding meta values. |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public function get_by_meta( $meta ) { |
| 35 |
|
| 36 |
$meta_query = []; |
| 37 |
foreach ( $meta as $key => $value ) { |
| 38 |
$meta_query[] = [ |
| 39 |
'key' => $key, |
| 40 |
'value' => $value, |
| 41 |
'compare' => '=', |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
$query = $this->query( |
| 46 |
[ |
| 47 |
'meta_query' => $meta_query, |
| 48 |
] |
| 49 |
); |
| 50 |
|
| 51 |
return $query->posts; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get an array of code for the given object. |
| 56 |
* @return array |
| 57 |
*/ |
| 58 |
public function get_active_code() { |
| 59 |
return $this->get_by_meta( |
| 60 |
[ |
| 61 |
'_fl_asst_enable' => '1', |
| 62 |
] |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
public function evalute_code_rules( $view ) { |
| 67 |
|
| 68 |
$code_data = $this->get_active_code(); |
| 69 |
|
| 70 |
$results = []; |
| 71 |
if( ! empty( $code_data ) ) { |
| 72 |
foreach( $code_data as $post ) { |
| 73 |
$id = $post->ID; |
| 74 |
$code = get_post_meta( $id, '_fl_asst_code', true ); |
| 75 |
$code_type = get_post_meta( $id, '_fl_asst_code_type', true ); |
| 76 |
$locations = get_post_meta( $id, '_fl_asst_code_locations', true ); |
| 77 |
$locations = maybe_unserialize( $locations ); |
| 78 |
|
| 79 |
if( ! empty( $locations ) ) { |
| 80 |
|
| 81 |
foreach( $locations as $rule ) { |
| 82 |
|
| 83 |
extract( $rule ); |
| 84 |
|
| 85 |
switch ( $type ) { |
| 86 |
|
| 87 |
case 'post_type': |
| 88 |
|
| 89 |
$result = false; |
| 90 |
if( 'equals' === $operator ) { |
| 91 |
$result = $value === $view['type_name']; |
| 92 |
} |
| 93 |
if( 'does_not_equal' === $operator ) { |
| 94 |
$result = $value !== $view['type_name']; |
| 95 |
} |
| 96 |
break; |
| 97 |
|
| 98 |
case 'entire_site': |
| 99 |
|
| 100 |
$result = true; |
| 101 |
break; |
| 102 |
|
| 103 |
case 'all_singular': |
| 104 |
|
| 105 |
$result = true === $view['isSingular']; |
| 106 |
break; |
| 107 |
|
| 108 |
case 'all_archives': |
| 109 |
|
| 110 |
$result = true === ( true === $view['isPostTypeArchive'] || true === $view['isAuthor'] || true === $view['isDate'] || true === $view['isSearch'] ); |
| 111 |
break; |
| 112 |
|
| 113 |
case 'author_archives': |
| 114 |
|
| 115 |
$result = true === $view['isAuthor']; |
| 116 |
break; |
| 117 |
|
| 118 |
case 'date_archives': |
| 119 |
|
| 120 |
$result = true === $view['isDate']; |
| 121 |
break; |
| 122 |
|
| 123 |
case 'search_results': |
| 124 |
|
| 125 |
$result = true === $view['isSearch']; |
| 126 |
break; |
| 127 |
|
| 128 |
case '404_page': |
| 129 |
|
| 130 |
$result = true === $view['is404']; |
| 131 |
break; |
| 132 |
|
| 133 |
case 'default': |
| 134 |
$result = false; |
| 135 |
break; |
| 136 |
} |
| 137 |
|
| 138 |
if( $result ) { |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if( $result ) { |
| 144 |
|
| 145 |
if( 'CSS' === $code_type ) { |
| 146 |
$results[ $code_type ] = isset( $results[ $code_type ] ) ? $results[ $code_type ] . $code : $code; |
| 147 |
} else { |
| 148 |
$results[ $code_type ][] = $code; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
return $results; |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|