| 1 |
<?php |
| 2 |
|
| 3 |
namespace Emailkit\Admin; |
| 4 |
use EmailKit\Admin\Emails\Helpers\Utils; |
| 5 |
|
| 6 |
defined("ABSPATH") || exit; |
| 7 |
|
| 8 |
class Hooks |
| 9 |
{ |
| 10 |
|
| 11 |
public function __construct() |
| 12 |
{ |
| 13 |
add_filter('manage_emailkit_posts_columns', [$this, 'set_columns']); |
| 14 |
add_action('manage_emailkit_posts_custom_column', [$this, 'add_column_content'], 10, 2); |
| 15 |
add_action('admin_init', [$this, 'add_author_support'], 10); |
| 16 |
add_action('admin_footer', [$this, 'modal_view']); |
| 17 |
add_filter('emailkit_shortcode_filter', [$this, 'replace']); |
| 18 |
add_filter('post_row_actions', [$this, 'custom_post_row_actions'], 10, 2); |
| 19 |
add_filter('emailkit_shortcode_filter', [$this, 'mail_shortcode_replace'], 10, 1); |
| 20 |
} |
| 21 |
function custom_post_row_actions($actions, $post) { |
| 22 |
|
| 23 |
// Remove the Quick Edit link |
| 24 |
if ( isset( $actions['inline hide-if-no-js'] ) ) { |
| 25 |
unset( $actions['inline hide-if-no-js'] ); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
if ($post->post_type === 'emailkit') { |
| 30 |
$edit_url = admin_url("post.php?post={$post->ID}&action=emailkit-builder"); |
| 31 |
$actions['emailkit-builder'] = "<a href='$edit_url'>Edit with Emailkit</a>"; |
| 32 |
// unset($actions['edit']); |
| 33 |
} |
| 34 |
return $actions; |
| 35 |
} |
| 36 |
|
| 37 |
public function replace($input){ |
| 38 |
|
| 39 |
// Define the regular expression pattern |
| 40 |
$pattern = '/<span data-shortcode="{{([\w]+)}}">([\w]+)<\/span>/'; |
| 41 |
|
| 42 |
// Use preg_match_all to find all matches in the input |
| 43 |
preg_match_all($pattern, $input, $matches, PREG_SET_ORDER); |
| 44 |
|
| 45 |
// Loop through each match and replace the content inside the span tag |
| 46 |
foreach ($matches as $match) { |
| 47 |
$shortcode = $match[1]; // Content inside the data-shortcode attribute |
| 48 |
|
| 49 |
// Create the replacement string and replace the match in the input |
| 50 |
$replacement = '<span data-shortcode="{{' . $shortcode . '}}">{{' . $shortcode . '}}</span>'; |
| 51 |
$input = str_replace($match[0], $replacement, $input); |
| 52 |
} |
| 53 |
|
| 54 |
return $input; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
public function add_author_support() |
| 59 |
{ |
| 60 |
remove_post_type_support('emailkit', 'author'); |
| 61 |
remove_post_type_support('emailkit', 'title'); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
public function set_columns($columns) |
| 66 |
{ |
| 67 |
|
| 68 |
$date_column = $columns['date']; |
| 69 |
unset($columns['title']); |
| 70 |
unset($columns['date']); |
| 71 |
unset($columns['author']); |
| 72 |
|
| 73 |
$columns['title'] = esc_html__('Template Title', 'emailkit'); |
| 74 |
$columns['type'] = esc_html__('Templates Type', 'emailkit'); |
| 75 |
$columns['status'] = esc_html__('Status', 'emailkit'); |
| 76 |
$columns['author'] = esc_html__('Author', 'emailkit'); |
| 77 |
$columns['date'] = esc_html($date_column); |
| 78 |
|
| 79 |
return $columns; |
| 80 |
} |
| 81 |
|
| 82 |
public function add_column_content($col, $post_id) |
| 83 |
{ |
| 84 |
|
| 85 |
$type = get_post_meta($post_id, 'emailkit_template_type', true); |
| 86 |
$status = get_post_meta($post_id, 'emailkit_template_status', true); |
| 87 |
|
| 88 |
switch ($col) { |
| 89 |
case 'type': |
| 90 |
echo esc_html($type); |
| 91 |
break; |
| 92 |
|
| 93 |
case 'status': |
| 94 |
|
| 95 |
echo esc_html(ucfirst($status)); |
| 96 |
break; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
function mail_shortcode_replace($mail_content){ |
| 101 |
return Utils::mail_shortcode_filter($mail_content); |
| 102 |
} |
| 103 |
public function modal_view(){ |
| 104 |
|
| 105 |
$screen = get_current_screen(); |
| 106 |
|
| 107 |
if($screen->id == 'edit-emailkit' ){ |
| 108 |
|
| 109 |
include_once EMAILKIT_DIR . 'includes/views/modal-add-new-email.php'; |
| 110 |
include_once EMAILKIT_DIR . 'includes/views/modal-editor.php'; |
| 111 |
} |
| 112 |
} |
| 113 |
} |