PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.9.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.9.1
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 2.9.1, at includes/Frontend/CustomRoutes.php

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