PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.2
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Frontend / CustomRoutes.php

CustomRoutes.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.2.2, at includes/Frontend/CustomRoutes.php

88 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Frontend;
4
5 if (!defined('ABSPATH')) {
6 exit;
7 }
8
9 use ReflectionMethod;
10
11 class CustomRoutes
12 {
13 protected $routes = [];
14
15 public function __construct()
16 {
17 add_action('parse_request', [$this, 'parseRequestAction']);
18 add_filter('rewrite_rules_array', [$this, 'rewriteRulesArrayFilter']);
19 }
20
21 public function parseRequestAction($query)
22 {
23 if ($query->matched_rule and isset($this->routes[$query->matched_rule])) {
24 $route = $this->routes[$query->matched_rule];
25 $this->methodExecution($route, $query);
26 exit;
27 }
28 }
29
30 public function add($match, $callback, $template = null, $query_vars = [])
31 {
32 $this->routes[$match] = compact('callback', 'template', 'query_vars');
33 }
34
35 public function rewriteRulesArrayFilter($rules)
36 {
37 $newRules = [];
38 foreach ($this->routes as $match => $route) {
39 $newRules[$match] = $this->makeRuleUrl($route);
40 }
41 return $newRules + $rules;
42 }
43
44 // public function addedQueryParmas($vars) {
45 // foreach ($this->routes as $route) {
46 // foreach ($route['query_vars'] as $key => $value) {
47 // $vars[] = $key;
48 // }
49 // }
50
51 // return $vars;
52 // }
53
54 protected function methodExecution($route, $query)
55 {
56 $params = [];
57
58 foreach ($route['query_vars'] as $name => $match) {
59 $params[] = $query->query_vars[$name];
60 }
61
62 if (is_array($route['callback']) && 2 === count($route['callback'])) {
63 $className = $route['callback'][0];
64 $method = $route['callback'][1];
65 if (class_exists($className) && method_exists($className, $method)) {
66 $reflectionMethod = new ReflectionMethod($className, $method);
67 $reflectionMethod->invoke($reflectionMethod->isStatic() ? null : new $className());
68 } else {
69 return 'Method or class not exist';
70 }
71 } else {
72 return 'Invalid invokeable';
73 }
74
75 // call_user_func_array($route['callback'], $params);
76 }
77
78 protected function makeRuleUrl($route)
79 {
80 $queryVars = [];
81 foreach ($route['query_vars'] as $name => $match) {
82 $queryVars[] = $name . '=$matches[' . $match . ']';
83 }
84
85 return 'index.php?' . implode('&', $queryVars);
86 }
87 }
88