PluginProbe
FluentSMTP – WP Mail SMTP Plugin with Amazon SES, SendGrid, Mailgun, Postmark, Cloudflare, toSend, Gmail and Any SMTP / trunk
FluentSMTP – WP Mail SMTP Plugin with Amazon SES, SendGrid, Mailgun, Postmark, Cloudflare, toSend, Gmail and Any SMTP vtrunk
2.4.0 trunk 1.0.1 1.1.0 1.1.1 1.2.0 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.71 2.2.72 2.2.73 2.2.80 2.2.81 All 32 releases
fluent-smtp / includes / Core / CoreTrait.php

CoreTrait.php in FluentSMTP – WP Mail SMTP Plugin with Amazon SES, SendGrid, Mailgun, Postmark, Cloudflare, toSend, Gmail and Any SMTP trunk, at includes/Core/CoreTrait.php

165 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentMail\Includes\Core;
4
5 use FluentMail\Includes\Support\ForbiddenException;
6
7 trait CoreTrait
8 {
9 public function get($action, $handler, $isAdmin = true)
10 {
11 $action = $this->getAjaxAction($action, 'get', $isAdmin);
12
13 return add_action($action, $this->parseAjaxHandler($handler));
14 }
15
16 public function getPublic($action, $handler)
17 {
18 $this->get($action, $handler, false);
19 }
20
21 public function post($action, $handler, $isAdmin = true)
22 {
23 $action = $this->getAjaxAction($action, 'post', $isAdmin);
24
25 return add_action($action, function() use ($handler) {
26 try {
27 $slug = FLUENTMAIL;
28
29 if (check_ajax_referer($slug, 'nonce', false)) {
30 $method = $this->parseAjaxHandler($handler);
31 return $method();
32 }
33
34 throw new ForbiddenException(esc_html__('Forbidden!', 'fluent-smtp'), 401);
35
36 } catch (ForbiddenException $e) {
37 return $this->docustomAction('handle_exception', $e);
38 }
39 });
40 }
41
42 public function postPublic($action, $handler)
43 {
44 $this->post($action, $handler, false);
45 }
46
47 public function getAjaxAction($action, $method, $isAdmin)
48 {
49 $context = $isAdmin ? 'wp_ajax_' : 'wp_ajax_nopriv_';
50 $action = $action == '/' ? $action : ltrim($action, '/');
51 return $context.$this->hook($method.'-'.$action);
52 }
53
54 public function hook($hook)
55 {
56 return FLUENTMAIL . '-' . $hook;
57 }
58
59 public function parseAjaxHandler($handler)
60 {
61 if (!$handler) return;
62
63 if (is_string($handler)) {
64 $handler = $this->controllerNamespace . '\\' . $handler;
65 } else if (is_array($handler)) {
66 list($class, $method) = $handler;
67 if (is_string($class)) {
68 $handler = $this->controllerNamespace . '\\' . $class . '::' . $method;
69 }
70 }
71
72 return function() use ($handler) {
73 return $this->call($handler);
74 };
75 }
76
77 public function addAction($action, $handler, $priority = 10, $numOfArgs = 1)
78 {
79 return add_action(
80 $action,
81 $this->parseHookHandler($handler),
82 $priority,
83 $numOfArgs
84 );
85 }
86
87 public function addCustomAction($action, $handler, $priority = 10, $numOfArgs = 1)
88 {
89 return $this->addAction($this->hook($action), $handler, $priority, $numOfArgs);
90 }
91
92 public function doAction()
93 {
94 return call_user_func_array('do_action', func_get_args());
95 }
96
97 public function doCustomAction()
98 {
99 $args = func_get_args();
100 $args[0] = $this->hook($args[0]);
101 return call_user_func_array('do_action', $args);
102 }
103
104 public function addFilter($action, $handler, $priority = 10, $numOfArgs = 1)
105 {
106 return add_filter(
107 $action,
108 $this->parseHookHandler($handler),
109 $priority,
110 $numOfArgs
111 );
112 }
113
114 public function addCustomFilter($action, $handler, $priority = 10, $numOfArgs = 1)
115 {
116 return $this->addFilter($this->hook($action), $handler, $priority, $numOfArgs);
117 }
118
119 public function applyFilters()
120 {
121 return call_user_func_array('apply_filters', func_get_args());
122 }
123
124 public function applyCustomFilters()
125 {
126 $args = func_get_args();
127 $args[0] = $this->hook($args[0]);
128 return call_user_func_array('apply_filters', $args);
129 }
130
131 public function parseHookHandler($handler)
132 {
133 if (is_string($handler)) {
134 list($class, $method) = preg_split('/::|@/', $handler);
135
136 if ($this->hasNamespace($handler)) {
137 $class = $this->make($class);
138 } else {
139 $class = $this->make($this->handlerNamespace . '\\' . $class);
140 }
141 return [$class, $method];
142
143 } else if (is_array($handler)) {
144 list($class, $method) = $handler;
145 if (is_string($class)) {
146 if ($this->hasNamespace($handler)) {
147 $class = $this->make($class);
148 } else {
149 $class = $this->make($this->handlerNamespace . '\\' . $class);
150 }
151 }
152
153 return [$class, $method];
154 }
155
156 return $handler;
157 }
158
159 public function hasNamespace($handler)
160 {
161 $parts = explode('\\', $handler);
162 return count($parts) > 1;
163 }
164 }
165