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

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