These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Amarkal\Settings; |
||
4 | |||
5 | /** |
||
6 | * Implements a settings child page. |
||
7 | * Child pages are pages that appear and their parent's submenu (unless there is |
||
8 | * only one child, in which case the child page handles the parent page HTML output). |
||
9 | */ |
||
10 | class ChildPage |
||
11 | { |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
12 | private $config; |
||
13 | |||
14 | private $form; |
||
15 | |||
16 | public function __construct( array $args = array() ) |
||
17 | { |
||
18 | $this->config = array_merge($this->default_args(), $args); |
||
19 | $this->form = new \Amarkal\UI\Form($this->config['fields']); |
||
20 | |||
21 | \add_action('admin_menu', array($this,'add_submenu_page')); |
||
22 | \add_action('admin_enqueue_scripts', array($this,'enqueue_scripts')); |
||
23 | } |
||
24 | |||
25 | View Code Duplication | public function add_submenu_page() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
26 | { |
||
27 | \add_submenu_page( |
||
28 | $this->config['parent_slug'], |
||
29 | $this->config['title'], |
||
30 | $this->config['menu_title'], |
||
31 | $this->config['capability'], |
||
32 | $this->config['slug'], |
||
33 | array($this, 'render') |
||
34 | ); |
||
35 | } |
||
36 | |||
37 | public function enqueue_scripts() |
||
38 | { |
||
39 | // Only enqueue styles & scripts if this is a settings page |
||
40 | if($this->config['slug'] === filter_input(INPUT_GET, 'page')) |
||
41 | { |
||
42 | \wp_enqueue_style('amarkal-settings'); |
||
43 | \wp_enqueue_script('amarkal-settings'); |
||
44 | } |
||
45 | } |
||
46 | |||
47 | public function render() |
||
48 | { |
||
49 | $this->form->update($this->get_old_instance()); |
||
50 | include __DIR__.'/ChildPage.phtml'; |
||
51 | \add_filter('admin_footer_text', array($this, 'footer_credits')); |
||
52 | } |
||
53 | |||
54 | public function update( $new_instance ) |
||
55 | { |
||
56 | if($this->can_update()) |
||
57 | { |
||
58 | $old_instance = $this->get_old_instance(); |
||
59 | $final_instance = $this->form->update($new_instance, $old_instance); |
||
60 | foreach($final_instance as $name => $value) |
||
61 | { |
||
62 | \update_option($name,$value); |
||
63 | } |
||
64 | return $this->results_array( |
||
65 | $this->get_errors(), |
||
66 | $final_instance |
||
0 ignored issues
–
show
$final_instance is of type array , but the function expects a string .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() |
|||
67 | ); |
||
68 | } |
||
69 | return $this->results_array( |
||
70 | array("You don't have permission to manage options on this site") |
||
71 | ); |
||
72 | } |
||
73 | |||
74 | public function reset() |
||
75 | { |
||
76 | if($this->can_update()) |
||
77 | { |
||
78 | foreach($this->config['fields'] as $field) |
||
79 | { |
||
80 | \delete_option($field['name']); |
||
81 | } |
||
82 | return $this->results_array( |
||
83 | array(), |
||
84 | $this->form->reset() |
||
0 ignored issues
–
show
$this->form->reset() is of type array , but the function expects a string .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() |
|||
85 | ); |
||
86 | } |
||
87 | return $this->results_array( |
||
88 | array("You don't have permission to manage options on this site") |
||
89 | ); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Renders Amarkal's credits on the page's footer. |
||
94 | */ |
||
95 | public function footer_credits() |
||
96 | { |
||
97 | echo '<span id="footer-thankyou">Created with <a href="https://github.com/askupasoftware/amarkal-settings">amarkal-settings</a>, a module within the <a href="https://github.com/askupasoftware/amarkal">Amarkal Framework</a></span>'; |
||
98 | } |
||
99 | |||
100 | private function get_errors() |
||
101 | { |
||
102 | $errors = array(); |
||
103 | foreach($this->form->get_errors() as $name => $error) |
||
104 | { |
||
105 | $component = $this->form->get_component($name); |
||
106 | $errors[] = "<strong>{$component->title}</strong> $error"; |
||
107 | } |
||
108 | return $errors; |
||
109 | } |
||
110 | |||
111 | private function results_array( $errors = array(), $values = '' ) |
||
112 | { |
||
113 | return array( |
||
114 | 'values' => $values, |
||
115 | 'errors' => $errors |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | private function can_update() |
||
120 | { |
||
121 | return \current_user_can($this->config['capability']); |
||
122 | } |
||
123 | |||
124 | private function get_old_instance() |
||
125 | { |
||
126 | $old_instance = array(); |
||
127 | foreach($this->form->get_components() as $component) |
||
128 | { |
||
129 | $old_instance[$component->name] = \get_option($component->name, $component->default); |
||
130 | } |
||
131 | return $old_instance; |
||
132 | } |
||
133 | |||
134 | private function default_args() |
||
135 | { |
||
136 | return array( |
||
137 | 'parent_slug' => '', |
||
138 | 'slug' => '', |
||
139 | 'title' => '', |
||
140 | 'subtitle' => '', |
||
141 | 'menu_title' => '', |
||
142 | 'capability' => 'manage_options', |
||
143 | 'footer_html' => '', |
||
144 | 'subfooter_html' => '', |
||
145 | 'fields' => array() |
||
146 | ); |
||
147 | } |
||
148 | } |