| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Shortcodes; |
| 4 |
|
| 5 |
use YayMail\Utils\SingletonTrait; |
| 6 |
use YayMail\Abstracts\BaseShortcode; |
| 7 |
|
| 8 |
/** |
| 9 |
* @method: static HookShortcodes get_instance() |
| 10 |
*/ |
| 11 |
class HookShortcodes extends BaseShortcode { |
| 12 |
use SingletonTrait; |
| 13 |
|
| 14 |
public function get_shortcodes() { |
| 15 |
$shortcodes = []; |
| 16 |
$shortcodes[] = [ |
| 17 |
'name' => 'yaymail_custom_hook', |
| 18 |
'description' => __( 'YayMail custom hook', 'yaymail' ), |
| 19 |
'group' => 'none', |
| 20 |
'callback' => [ $this, 'main_callback' ], |
| 21 |
]; |
| 22 |
|
| 23 |
return $shortcodes; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Main callback function to process a custom hook shortcode. |
| 28 |
* |
| 29 |
* @param array $data The data associated with the shortcode execution, typically template data. |
| 30 |
* Example: ['template' => YayMail\YayMailTemplate, 'render_data' => [], 'settings' => []]. |
| 31 |
* @param string[] $attr_strings An array of attribute strings that need to be parsed into attribute tuples. |
| 32 |
* Example: ["hook="yaydp_on_sale_products"", "limit="6"", "sale_price_color="#ec4770""]. |
| 33 |
* |
| 34 |
* @return string The HTML content generated by processing the custom hook shortcode. |
| 35 |
*/ |
| 36 |
public function main_callback( $data, $attr_strings ) { |
| 37 |
// $attr_tuples = $this->get_attributes_from_strings( $attr_strings ); |
| 38 |
return $this->yaymail_handle_custom_hook_shortcode( $data, $attr_strings ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Process a custom hook shortcode and generate HTML content. |
| 43 |
* |
| 44 |
* @param array $data The data associated with the shortcode execution, typically template data. |
| 45 |
* Example: ['template' => YayMail\YayMailTemplate, 'render_data' => [], 'settings' => []]. |
| 46 |
* @param array $attr_tuples An array of attribute tuples containing hook name and display settings. |
| 47 |
* Example: ['hook' => 'yaydp_on_sale_products', 'limit' => '6']. |
| 48 |
* @return string The HTML content generated by processing the custom hook shortcode. |
| 49 |
*/ |
| 50 |
public function yaymail_handle_custom_hook_shortcode( $data, $attr_tuples ) { |
| 51 |
$hook_name = isset( $attr_tuples['hook'] ) ? $attr_tuples['hook'] : ''; |
| 52 |
|
| 53 |
switch ( $hook_name ) { |
| 54 |
case 'yaydp_on_sale_products': |
| 55 |
return $this->yaymail_render_yaydp_on_sale_products( $attr_tuples ); |
| 56 |
case 'woocommerce_email_before_order_table': |
| 57 |
case 'woocommerce_email_after_order_table': |
| 58 |
return $this->yaymail_woocommerce_email_order_table( $data, $hook_name ); |
| 59 |
default: |
| 60 |
return $this->yaymail_render_custom_hook_shortcode( $data, $hook_name ); |
| 61 |
} |
| 62 |
|
| 63 |
return ''; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Generate HTML content for a custom hook related to WooCommerce email order tables. |
| 68 |
* |
| 69 |
* @param array $data The data associated with the shortcode execution, typically template data. |
| 70 |
* Example: ['template' => YayMail\YayMailTemplate, 'render_data' => [], 'settings' => []]. |
| 71 |
* @param string $hook_name The name of the hook associated with the WooCommerce email order table. |
| 72 |
* Example: "woocommerce_email_before_order_table". |
| 73 |
* |
| 74 |
* @return string The HTML content for the custom hook shortcode. |
| 75 |
*/ |
| 76 |
private function yaymail_woocommerce_email_order_table( $data, $hook_name ) { |
| 77 |
if ( empty( $data['render_data']['order'] ) ) { |
| 78 |
return '[' . $hook_name . ']'; |
| 79 |
} |
| 80 |
|
| 81 |
$order = isset( $data['render_data']['order'] ) ? $data['render_data']['order'] : []; |
| 82 |
|
| 83 |
$sent_to_admin = isset( $data['send_to_admin'] ) ? $data['send_to_admin'] : false; |
| 84 |
$plain_text = isset( $data['plain_text'] ) ? $data['plain_text'] : false; |
| 85 |
$email = isset( $data['render_data']['email'] ) ? $data['render_data']['email'] : []; |
| 86 |
|
| 87 |
$hook_name = ! empty( $hook_name ) ? $hook_name : 'woocommerce_email_before_order_table'; |
| 88 |
|
| 89 |
ob_start(); |
| 90 |
do_action( $hook_name, $order, $sent_to_admin, $plain_text, $email ); |
| 91 |
|
| 92 |
$content = ob_get_clean(); |
| 93 |
|
| 94 |
return $content; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Render HTML content for a custom hook shortcode. |
| 99 |
* |
| 100 |
* @param array $data The data associated with the shortcode execution, containing template, render data and settings. |
| 101 |
* @param string $hook_name The name of the custom hook shortcode to execute. |
| 102 |
* @return string The HTML content generated by processing the custom hook shortcode. |
| 103 |
*/ |
| 104 |
private function yaymail_render_custom_hook_shortcode( $data, $hook_name ) { |
| 105 |
if ( empty( $hook_name ) || ! is_string( $hook_name ) ) { |
| 106 |
return ''; |
| 107 |
} |
| 108 |
|
| 109 |
ob_start(); |
| 110 |
|
| 111 |
do_action( $hook_name, $data ); |
| 112 |
|
| 113 |
$content = ob_get_clean(); |
| 114 |
|
| 115 |
return $content; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Render HTML content for the 'yaydp_on_sale_products' hook. |
| 120 |
* |
| 121 |
* @param array $attr_tuples Attribute tuples for customizing on-sale products display. |
| 122 |
* Example: [ |
| 123 |
* 'limit' => '6', |
| 124 |
* 'color' => 'inherit', |
| 125 |
* 'background_color' => 'inherit', |
| 126 |
* 'sale_price_color' => '#ec4770', |
| 127 |
* 'regular_price_color' => '#808080', |
| 128 |
* 'product_name_color' => '#636363', |
| 129 |
* 'button_background_color' => '#ec4770', |
| 130 |
* 'button_text_color' => '#ffffff' |
| 131 |
* ] |
| 132 |
* @return string Generated HTML content. |
| 133 |
*/ |
| 134 |
private function yaymail_render_yaydp_on_sale_products( $attr_tuples ) { |
| 135 |
if ( is_callable( 'yaydp_get_on_sale_products' ) ) { |
| 136 |
$products = yaydp_get_on_sale_products( $attr_tuples['limit'] ); |
| 137 |
$args = [ |
| 138 |
'products' => $products, |
| 139 |
'color' => esc_attr( $this->get_attribute( $attr_tuples, 'color' ) ), |
| 140 |
'background_color' => esc_attr( $this->get_attribute( $attr_tuples, 'background_color' ) ), |
| 141 |
'sale_price_color' => esc_attr( $this->get_attribute( $attr_tuples, 'sale_price_color', 'color' ) ), |
| 142 |
'regular_price_color' => esc_attr( $this->get_attribute( $attr_tuples, 'regular_price_color', 'color' ) ), |
| 143 |
'product_name_color' => esc_attr( $this->get_attribute( $attr_tuples, 'product_name_color', 'color' ) ), |
| 144 |
'button_background_color' => esc_attr( $this->get_attribute( $attr_tuples, 'button_background_color' ) ), |
| 145 |
'button_text_color' => esc_attr( $this->get_attribute( $attr_tuples, 'button_text_color', 'color' ) ), |
| 146 |
]; |
| 147 |
$content = yaymail_get_content( 'templates/shortcodes/yaydp-on-sale-products/main.php', $args ); |
| 148 |
// We escape the content via yaymail_kses_post_e() in the template file |
| 149 |
return $content; // nosemgrep |
| 150 |
} |
| 151 |
return 'yaydp_on_sale_products'; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Extracts attributes from an array of strings in the format 'key="value"'. |
| 156 |
* |
| 157 |
* @param array $strings An array of strings containing attributes. |
| 158 |
* |
| 159 |
* @return array An array of attribute arrays, where each attribute array is associative ('key' => 'value'). |
| 160 |
*/ |
| 161 |
private function get_attributes_from_strings( $strings ) { |
| 162 |
$result = []; |
| 163 |
|
| 164 |
if ( ! is_array( $strings ) ) { |
| 165 |
return $result; |
| 166 |
} |
| 167 |
foreach ( $strings as $str ) { |
| 168 |
preg_match_all( '/(\w+)="([^"]*?)"/', $str, $matches, PREG_SET_ORDER ); |
| 169 |
|
| 170 |
foreach ( $matches as $match ) { |
| 171 |
$result[ $match[1] ] = $match[2]; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return $result; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Get an attribute value with fallback. |
| 180 |
* |
| 181 |
* @param array $attribute_tuples Attribute key-value pairs |
| 182 |
* @param string $attribute Primary attribute key |
| 183 |
* @param string|null $fallback_attribute Optional fallback attribute key |
| 184 |
* @return string Attribute value, fallback value, or 'inherit' |
| 185 |
*/ |
| 186 |
private function get_attribute( $attribute_tuples, $attribute, $fallback_attribute = null ) { |
| 187 |
$result = isset( $attribute_tuples[ $attribute ] ) ? $attribute_tuples[ $attribute ] : ''; |
| 188 |
|
| 189 |
if ( empty( $result ) && isset( $fallback_attribute ) ) { |
| 190 |
$result = isset( $attribute_tuples[ $fallback_attribute ] ) ? $attribute_tuples[ $fallback_attribute ] : ''; |
| 191 |
} |
| 192 |
|
| 193 |
if ( empty( $result ) ) { |
| 194 |
return 'inherit'; |
| 195 |
} |
| 196 |
return $result; |
| 197 |
} |
| 198 |
} |
| 199 |
|