| @@ -24,20 +24,107 @@ | ||
| 24 | 24 | * @return array<string,mixed> |
| 25 | 25 | */ |
| 26 | 26 | public static function normalize($raw): array |
| 27 | 27 | { |
| 28 | + $data = []; | |
| 29 | + | |
| 28 | 30 | if (is_array($raw)) { |
| 29 | - return $raw; | |
| 30 | - } | |
| 31 | - | |
| 32 | - if (is_string($raw) && !empty($raw)) { | |
| 31 | + $data = $raw; | |
| 32 | + } elseif (is_string($raw) && $raw !== '') { | |
| 33 | 33 | $decoded = json_decode($raw, true); |
| 34 | 34 | if (is_array($decoded)) { |
| 35 | - return $decoded; | |
| 35 | + $data = $decoded; | |
| 36 | + } else { | |
| 37 | + $unserialized = maybe_unserialize($raw); | |
| 38 | + $data = is_array($unserialized) ? $unserialized : []; | |
| 36 | 39 | } |
| 37 | 40 | } |
| 38 | 41 | |
| 42 | + if (isset($data['groups']) && is_array($data['groups'])) { | |
| 43 | + return $data; | |
| 44 | + } | |
| 45 | + | |
| 39 | 46 | return ['groups' => []]; |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * Default condition groups for a Theme Builder sub-location. | |
| 51 | + * | |
| 52 | + * Legacy templates stored "include all" without groups. New templates | |
| 53 | + * created from the Theme Builder modal get an explicit post_type rule. | |
| 54 | + * Filling the same default here keeps both at equal specificity so a | |
| 55 | + * later stub cannot hide a fully built Single template. | |
| 56 | + * | |
| 57 | + * @param string $sub_location Sub-location value. | |
| 58 | + * | |
| 59 | + * @return array<string,mixed> | |
| 60 | + */ | |
| 61 | + public static function defaults_for_sub_location(string $sub_location): array | |
| 62 | + { | |
| 63 | + $groups = []; | |
| 64 | + | |
| 65 | + switch ($sub_location) { | |
| 66 | + case 'single_post': | |
| 67 | + $groups[] = [ | |
| 68 | + 'relation' => 'AND', | |
| 69 | + 'rules' => [ | |
| 70 | + [ | |
| 71 | + 'type' => 'include', | |
| 72 | + 'target' => 'post_type', | |
| 73 | + 'operator' => 'in', | |
| 74 | + 'value' => ['post'], | |
| 75 | + ], | |
| 76 | + ], | |
| 77 | + ]; | |
| 78 | + break; | |
| 79 | + case 'single_page': | |
| 80 | + $groups[] = [ | |
| 81 | + 'relation' => 'AND', | |
| 82 | + 'rules' => [ | |
| 83 | + [ | |
| 84 | + 'type' => 'include', | |
| 85 | + 'target' => 'post_type', | |
| 86 | + 'operator' => 'in', | |
| 87 | + 'value' => ['page'], | |
| 88 | + ], | |
| 89 | + ], | |
| 90 | + ]; | |
| 91 | + break; | |
| 92 | + case 'search_results': | |
| 93 | + $groups[] = [ | |
| 94 | + 'relation' => 'AND', | |
| 95 | + 'rules' => [ | |
| 96 | + [ | |
| 97 | + 'type' => 'include', | |
| 98 | + 'target' => 'search', | |
| 99 | + 'operator' => 'in', | |
| 100 | + 'value' => ['search'], | |
| 101 | + ], | |
| 102 | + ], | |
| 103 | + ]; | |
| 104 | + break; | |
| 105 | + case 'not_found': | |
| 106 | + $groups[] = [ | |
| 107 | + 'relation' => 'AND', | |
| 108 | + 'rules' => [ | |
| 109 | + [ | |
| 110 | + 'type' => 'include', | |
| 111 | + 'target' => '404', | |
| 112 | + 'operator' => 'in', | |
| 113 | + 'value' => ['404'], | |
| 114 | + ], | |
| 115 | + ], | |
| 116 | + ]; | |
| 117 | + break; | |
| 118 | + default: | |
| 119 | + $groups[] = [ | |
| 120 | + 'relation' => 'AND', | |
| 121 | + 'rules' => [], | |
| 122 | + ]; | |
| 123 | + break; | |
| 124 | + } | |
| 125 | + | |
| 126 | + return ['groups' => $groups]; | |
| 40 | 127 | } |
| 41 | 128 | |
| 42 | 129 | /** |
| 43 | 130 | * Check whether a conditions set requires Pro. |