| 1 |
<?php |
| 2 |
/** |
| 3 |
* Insertion locations |
| 4 |
* |
| 5 |
* @package Woody_Code_Snippets |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class WINP_Insertion_Locations { |
| 14 |
|
| 15 |
public $locations = []; |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor. |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
$locations = [ |
| 22 |
'everywhere' => [ |
| 23 |
'header' => [ |
| 24 |
__( 'Header', 'insert-php' ), |
| 25 |
__( 'Snippet will be placed in the source code before </head>.', 'insert-php' ), |
| 26 |
[ |
| 27 |
'requiresLocationNumber' => false, |
| 28 |
], |
| 29 |
], |
| 30 |
'footer' => [ |
| 31 |
__( 'Footer', 'insert-php' ), |
| 32 |
__( 'Snippet will be placed in the source code before </body>.', 'insert-php' ), |
| 33 |
[ |
| 34 |
'requiresLocationNumber' => false, |
| 35 |
], |
| 36 |
], |
| 37 |
], |
| 38 |
'posts' => [ |
| 39 |
'before_post' => [ |
| 40 |
__( 'Insert Before Post', 'insert-php' ), |
| 41 |
__( 'Snippet will be placed before the title of the post/page.', 'insert-php' ), |
| 42 |
[ |
| 43 |
'requiresLocationNumber' => false, |
| 44 |
], |
| 45 |
], |
| 46 |
'before_content' => [ |
| 47 |
__( 'Insert Before Content', 'insert-php' ), |
| 48 |
__( 'Snippet will be placed before the content of the post/page.', 'insert-php' ), |
| 49 |
[ |
| 50 |
'requiresLocationNumber' => false, |
| 51 |
], |
| 52 |
], |
| 53 |
'before_paragraph' => [ |
| 54 |
__( 'Insert Before Paragraph', 'insert-php' ), |
| 55 |
__( 'Snippet will be placed before the specified paragraph number.', 'insert-php' ), |
| 56 |
[ |
| 57 |
'requiresLocationNumber' => true, |
| 58 |
], |
| 59 |
], |
| 60 |
'after_paragraph' => [ |
| 61 |
__( 'Insert After Paragraph', 'insert-php' ), |
| 62 |
__( 'Snippet will be placed after the specified paragraph number.', 'insert-php' ), |
| 63 |
[ |
| 64 |
'requiresLocationNumber' => true, |
| 65 |
], |
| 66 |
], |
| 67 |
'after_content' => [ |
| 68 |
__( 'Insert After Content', 'insert-php' ), |
| 69 |
__( 'Snippet will be placed after the content of the post/page.', 'insert-php' ), |
| 70 |
[ |
| 71 |
'requiresLocationNumber' => false, |
| 72 |
], |
| 73 |
], |
| 74 |
'after_post' => [ |
| 75 |
__( 'Insert After Post', 'insert-php' ), |
| 76 |
__( 'Snippet will be placed in the very end of the post/page.', 'insert-php' ), |
| 77 |
[ |
| 78 |
'requiresLocationNumber' => false, |
| 79 |
], |
| 80 |
], |
| 81 |
], |
| 82 |
'pages' => [ |
| 83 |
'before_excerpt' => [ |
| 84 |
__( 'Insert Before Excerpt', 'insert-php' ), |
| 85 |
__( 'Snippet will be placed before the excerpt of the post/page.', 'insert-php' ), |
| 86 |
[ |
| 87 |
'requiresLocationNumber' => false, |
| 88 |
], |
| 89 |
], |
| 90 |
'after_excerpt' => [ |
| 91 |
__( 'Insert After Excerpt', 'insert-php' ), |
| 92 |
__( 'Snippet will be placed after the excerpt of the post/page.', 'insert-php' ), |
| 93 |
[ |
| 94 |
'requiresLocationNumber' => false, |
| 95 |
], |
| 96 |
], |
| 97 |
'between_posts' => [ |
| 98 |
__( 'Between Posts', 'insert-php' ), |
| 99 |
__( 'Snippet will be placed between each post.', 'insert-php' ), |
| 100 |
[ |
| 101 |
'requiresLocationNumber' => false, |
| 102 |
], |
| 103 |
], |
| 104 |
'before_posts' => [ |
| 105 |
__( 'Before post', 'insert-php' ), |
| 106 |
__( 'Snippet will be placed before the specified post number.', 'insert-php' ), |
| 107 |
[ |
| 108 |
'requiresLocationNumber' => true, |
| 109 |
], |
| 110 |
], |
| 111 |
'after_posts' => [ |
| 112 |
__( 'After post', 'insert-php' ), |
| 113 |
__( 'Snippet will be placed after the specified post number.', 'insert-php' ), |
| 114 |
[ |
| 115 |
'requiresLocationNumber' => true, |
| 116 |
], |
| 117 |
], |
| 118 |
], |
| 119 |
'woocommerce' => [ |
| 120 |
'woo_before_shop_loop' => [ |
| 121 |
__( 'Before the list of products', 'insert-php' ), |
| 122 |
__( 'Snippet will be placed before the list of products.', 'insert-php' ), |
| 123 |
[ |
| 124 |
'requiresLocationNumber' => false, |
| 125 |
], |
| 126 |
], |
| 127 |
'woo_after_shop_loop' => [ |
| 128 |
__( 'After the list of products', 'insert-php' ), |
| 129 |
__( 'Snippet will be placed after the list of products.', 'insert-php' ), |
| 130 |
[ |
| 131 |
'requiresLocationNumber' => false, |
| 132 |
], |
| 133 |
], |
| 134 |
'woo_before_single_product' => [ |
| 135 |
__( 'Before a single product', 'insert-php' ), |
| 136 |
__( 'Snippet will be placed before a single product.', 'insert-php' ), |
| 137 |
[ |
| 138 |
'requiresLocationNumber' => false, |
| 139 |
], |
| 140 |
], |
| 141 |
'woo_after_single_product' => [ |
| 142 |
__( 'After a single product', 'insert-php' ), |
| 143 |
__( 'Snippet will be placed after a single product.', 'insert-php' ), |
| 144 |
[ |
| 145 |
'requiresLocationNumber' => false, |
| 146 |
], |
| 147 |
], |
| 148 |
'woo_before_single_product_summary' => [ |
| 149 |
__( 'Before a single product summary', 'insert-php' ), |
| 150 |
__( 'Snippet will be placed before a single product summary.', 'insert-php' ), |
| 151 |
[ |
| 152 |
'requiresLocationNumber' => false, |
| 153 |
], |
| 154 |
], |
| 155 |
'woo_after_single_product_summary' => [ |
| 156 |
__( 'After a single product summary', 'insert-php' ), |
| 157 |
__( 'Snippet will be placed after a single product summary.', 'insert-php' ), |
| 158 |
[ |
| 159 |
'requiresLocationNumber' => false, |
| 160 |
], |
| 161 |
], |
| 162 |
'woo_single_product_summary_title' => [ |
| 163 |
__( 'After a product title', 'insert-php' ), |
| 164 |
__( 'Snippet will be placed after a product title.', 'insert-php' ), |
| 165 |
[ |
| 166 |
'requiresLocationNumber' => false, |
| 167 |
], |
| 168 |
], |
| 169 |
'woo_single_product_summary_price' => [ |
| 170 |
__( 'After a product price', 'insert-php' ), |
| 171 |
__( 'Snippet will be placed after a product price.', 'insert-php' ), |
| 172 |
[ |
| 173 |
'requiresLocationNumber' => false, |
| 174 |
], |
| 175 |
], |
| 176 |
'woo_single_product_summary_excerpt' => [ |
| 177 |
__( 'After a product excerpt', 'insert-php' ), |
| 178 |
__( 'Snippet will be placed after a product excerpt.', 'insert-php' ), |
| 179 |
[ |
| 180 |
'requiresLocationNumber' => false, |
| 181 |
], |
| 182 |
], |
| 183 |
], |
| 184 |
]; |
| 185 |
|
| 186 |
/** |
| 187 |
* Filters the insertion locations. |
| 188 |
* |
| 189 |
* [ |
| 190 |
* 'before_2' => [ |
| 191 |
* __( 'Before 2', 'insert-php' ), |
| 192 |
* __( 'Before 2 desc.', 'insert-php' ), |
| 193 |
* [ |
| 194 |
* 'requiresLocationNumber' => false, // true if location needs paragraph/post number input |
| 195 |
* ] |
| 196 |
* ] |
| 197 |
* ] |
| 198 |
* |
| 199 |
* @param array<string, array<string, array<int|string, mixed>>> $locations The array of custom location data. |
| 200 |
* |
| 201 |
* @since 2.4 |
| 202 |
*/ |
| 203 |
$custom_data = apply_filters( 'wbcr/woody/add_custom_location', [] ); |
| 204 |
|
| 205 |
if ( ! empty( $custom_data ) && is_array( $custom_data ) ) { |
| 206 |
$locations['custom'] = $custom_data; |
| 207 |
} |
| 208 |
|
| 209 |
$this->locations = $locations; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* @param $name |
| 214 |
* |
| 215 |
* @return array|array[] |
| 216 |
*/ |
| 217 |
public function __get( $name ) { |
| 218 |
return $this->getLocation( $name ); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* @param string $insertion |
| 223 |
* |
| 224 |
* @return array |
| 225 |
*/ |
| 226 |
public function getInsertion( $insertion ) { |
| 227 |
return $this->locations[ $insertion ] ?? []; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* @param string $location |
| 232 |
* @param string $insertion |
| 233 |
* |
| 234 |
* @return array |
| 235 |
*/ |
| 236 |
public function getLocation( $location, $insertion = '' ) { |
| 237 |
if ( ! empty( $insertion ) ) { |
| 238 |
return $this->locations[ $insertion ][ $location ] ?? []; |
| 239 |
} else { |
| 240 |
foreach ( $this->locations as $k => $loc ) { |
| 241 |
foreach ( $loc as $key => $item ) { |
| 242 |
if ( $key == $location ) { |
| 243 |
return [ $key => $item ]; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
return []; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* |
| 254 |
* @return array |
| 255 |
*/ |
| 256 |
public function getList() { |
| 257 |
$list = []; |
| 258 |
foreach ( $this->locations as $k => $loc ) { |
| 259 |
foreach ( $loc as $key => $item ) { |
| 260 |
$list[ $key ] = $item; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
return $list; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* @param string $insertion |
| 269 |
* |
| 270 |
* @return array |
| 271 |
*/ |
| 272 |
public function getInsertionForOptions( $insertion ) { |
| 273 |
$return = []; |
| 274 |
foreach ( $this->locations[ $insertion ] ?? [] as $key => $locations ) { |
| 275 |
$requires_number = false; |
| 276 |
if ( isset( $locations[2] ) && is_array( $locations[2] ) && isset( $locations[2]['requiresLocationNumber'] ) ) { |
| 277 |
$requires_number = $locations[2]['requiresLocationNumber']; |
| 278 |
} |
| 279 |
$return[] = [ $key, $locations[0] ?? '', $locations[1] ?? '', $requires_number ]; |
| 280 |
} |
| 281 |
|
| 282 |
return $return; |
| 283 |
} |
| 284 |
} |
| 285 |
|