| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Shortcodes; |
| 4 |
|
| 5 |
use YayMail\Utils\SingletonTrait; |
| 6 |
|
| 7 |
/** |
| 8 |
* LegacyCustomShortcodes - Handles legacy custom shortcodes |
| 9 |
* |
| 10 |
* @method static LegacyCustomShortcodes get_instance() |
| 11 |
*/ |
| 12 |
class LegacyCustomShortcodes { |
| 13 |
|
| 14 |
use SingletonTrait; |
| 15 |
|
| 16 |
private $yaymail_settings; |
| 17 |
|
| 18 |
private function __construct() { |
| 19 |
|
| 20 |
$this->yaymail_settings = yaymail_settings(); |
| 21 |
|
| 22 |
add_filter( 'yaymail_extra_shortcodes', [ $this, 'merge_legacy_custom_shortcodes' ], 10, 2 ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Get legacy custom shortcodes |
| 27 |
* |
| 28 |
* @param array $data The data array. |
| 29 |
* @return array Array of migrated shortcodes. |
| 30 |
*/ |
| 31 |
public function merge_legacy_custom_shortcodes( $shortcodes, $data ) { |
| 32 |
$args = $this->get_shortcode_args( $data ); |
| 33 |
$yaymail_information = $this->get_yaymail_information( $data ); |
| 34 |
|
| 35 |
$legacy_custom_shortcodes = array_merge( |
| 36 |
/** |
| 37 |
* @deprecated 4.0.0 |
| 38 |
*/ |
| 39 |
apply_filters( 'yaymail_customs_shortcode', [], $yaymail_information, $args ), |
| 40 |
apply_filters( 'yaymail_custom_shortcode', [], $yaymail_information, $args ) |
| 41 |
); |
| 42 |
|
| 43 |
$migrated_shortcodes = array_map( |
| 44 |
function( $shortcode_name, $shortcoded_content ) { |
| 45 |
$formatted_name = $this->format_shortcode_name( $shortcode_name ); |
| 46 |
return [ |
| 47 |
'name' => $formatted_name, |
| 48 |
'description' => $this->get_shortcode_description( $formatted_name ), |
| 49 |
'group' => 'custom_shortcodes', |
| 50 |
'callback' => $this->get_mockup_callback( $shortcode_name, $shortcoded_content ), |
| 51 |
'callback_args' => [ |
| 52 |
'shortcode_name' => $shortcode_name, |
| 53 |
'shortcoded_content' => $shortcoded_content, |
| 54 |
], |
| 55 |
]; |
| 56 |
}, |
| 57 |
array_keys( $legacy_custom_shortcodes ), |
| 58 |
array_values( $legacy_custom_shortcodes ) |
| 59 |
); |
| 60 |
|
| 61 |
return array_merge( $shortcodes, $migrated_shortcodes ); |
| 62 |
} |
| 63 |
|
| 64 |
private function get_shortcode_args( $data ) { |
| 65 |
// Bring the render_data to the top level, as it was in the legacy version of YayMail |
| 66 |
return array_merge( $data, $data['render_data'] ?? [] ); |
| 67 |
} |
| 68 |
|
| 69 |
private function get_mockup_callback( $shortcode_name, $shortcoded_content ) { |
| 70 |
return function( $data, $args ) use ( $shortcode_name, $shortcoded_content ) { |
| 71 |
return $shortcoded_content; |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get shortcode description |
| 77 |
* |
| 78 |
* @param string $shortcode_name The shortcode name. |
| 79 |
* @return string The formatted description. |
| 80 |
*/ |
| 81 |
private function get_shortcode_description( $shortcode_name ) { |
| 82 |
// Get the part after 'yaymail_custom_shortcode_' |
| 83 |
$name_part = str_replace( 'yaymail_custom_shortcode_', '', $shortcode_name ); |
| 84 |
|
| 85 |
// Convert underscores to spaces and capitalize words |
| 86 |
$formatted_name = ucwords( str_replace( '_', ' ', $name_part ) ); |
| 87 |
|
| 88 |
/* translators: %s: The formatted shortcode name */ |
| 89 |
return sprintf( __( 'Custom shortcode for %s', 'yaymail' ), $formatted_name ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Format shortcode name |
| 94 |
* |
| 95 |
* @param string $shortcode_name The raw shortcode name. |
| 96 |
* @return string The formatted shortcode name. |
| 97 |
*/ |
| 98 |
private function format_shortcode_name( $shortcode_name ) { |
| 99 |
return str_replace( [ '[', ']' ], '', $shortcode_name ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get YayMail information from data |
| 104 |
* |
| 105 |
* @param array $data The data array. |
| 106 |
* @return array The YayMail information. |
| 107 |
*/ |
| 108 |
private function get_yaymail_information( $data ) { |
| 109 |
$template = $data['template'] ?? null; |
| 110 |
|
| 111 |
return [ |
| 112 |
'post_id' => isset( $template ) ? $template->get_id() : '', |
| 113 |
'template' => $template, |
| 114 |
'order' => $data['render_data']['order'] ?? null, |
| 115 |
'yaymail_elements' => $template ? $template->get_data()['elements'] : [], |
| 116 |
'general_settings' => array_merge( |
| 117 |
$this->yaymail_settings, |
| 118 |
[ |
| 119 |
'tableWidth' => $this->yaymail_settings['container_width'], |
| 120 |
'emailBackgroundColor' => $template ? $template->get_background_color() : '#ECECEC', |
| 121 |
'textLinkColor' => $template ? $template->get_text_link_color() : esc_attr( YAYMAIL_COLOR_WC_DEFAULT ), |
| 122 |
] |
| 123 |
), |
| 124 |
|
| 125 |
]; |
| 126 |
} |
| 127 |
} |
| 128 |
|