These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * This file contains only the Menus class |
||
4 | * |
||
5 | * @package Tabulate |
||
6 | */ |
||
7 | |||
8 | namespace WordPress\Tabulate; |
||
9 | |||
10 | use Exception; |
||
11 | use WordPress\Tabulate\DB\Exception as TabulateException; |
||
12 | use WordPress\Tabulate\DB\Table; |
||
13 | use WP_Admin_Bar; |
||
14 | use WP_Filesystem_Base; |
||
15 | use wpdb; |
||
16 | |||
17 | /** |
||
18 | * This class is an attempt to group all functionality around managing the menus |
||
19 | * in the Admin Area in one place. It includes adding scripts and stylesheets. |
||
20 | */ |
||
21 | class Menus { |
||
22 | |||
23 | /** |
||
24 | * The global wpdb object. |
||
25 | * |
||
26 | * @var wpdb |
||
27 | */ |
||
28 | protected $wpdb; |
||
29 | |||
30 | /** |
||
31 | * The global filesystem object |
||
32 | * |
||
33 | * @var WP_Filesystem_Base |
||
34 | */ |
||
35 | protected $filesystem; |
||
36 | |||
37 | /** |
||
38 | * The page output is stored between being called/created in |
||
39 | * self::dispatch() and output in self::add_menu_pages() |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $output; |
||
44 | |||
45 | /** |
||
46 | * Create a new Menus object, supplying it with the database so that it |
||
47 | * doesn't have to use a global. |
||
48 | * |
||
49 | * @param wpdb $wpdb The global wpdb object. |
||
50 | * @param WP_Filesystem_Base $filesystem The global filesystem object. |
||
51 | */ |
||
52 | public function __construct( $wpdb, WP_Filesystem_Base $filesystem ) { |
||
53 | $this->wpdb = $wpdb; |
||
54 | $this->filesystem = $filesystem; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Set up all required hooks. This is called from the top level of tabulate.php |
||
59 | * |
||
60 | * @return void |
||
61 | */ |
||
62 | public function init() { |
||
63 | add_action( 'init', array( $this, 'dispatch' ) ); |
||
64 | add_action( 'admin_menu', array( $this, 'add_menu_pages' ) ); |
||
65 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); |
||
66 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) ); |
||
67 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ) ); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Add Tabulate's menu items to the main admin menu. |
||
72 | * |
||
73 | * @return void |
||
74 | */ |
||
75 | public function add_menu_pages() { |
||
76 | $dispatch_callback = array( $this, 'output' ); |
||
77 | |||
78 | // Home page (also change the first submenu item's title). |
||
79 | add_menu_page( 'Tabulate', 'Tabulate', 'read', TABULATE_SLUG, $dispatch_callback ); |
||
80 | $page_title = ( isset( $_GET['table'] ) ) ? Text::titlecase( $_GET['table'] ) : 'Tabulate'; |
||
81 | add_submenu_page( TABULATE_SLUG, $page_title, 'Overview', 'read', TABULATE_SLUG, $dispatch_callback ); |
||
82 | |||
83 | // Add submenu pages. |
||
84 | if ( Util::is_plugin_active( 'tfo-graphviz/tfo-graphviz.php' ) ) { |
||
85 | add_submenu_page( TABULATE_SLUG, 'Tabulate ERD', 'ERD', 'read', TABULATE_SLUG . '_erd', $dispatch_callback ); |
||
86 | } |
||
87 | add_submenu_page( TABULATE_SLUG, 'Tabulate Reports', 'Reports', 'promote_users', TABULATE_SLUG . '_reports', $dispatch_callback ); |
||
88 | add_submenu_page( TABULATE_SLUG, 'Tabulate Grants', 'Grants', 'promote_users', TABULATE_SLUG . '_grants', $dispatch_callback ); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Add all tables in which the user is allowed to create records to the |
||
93 | * Admin Bar new-content menu. If there are more than ten, none are added |
||
94 | * because the menu would get too long. Not sure how this should be fixed. |
||
95 | * |
||
96 | * @global WP_Admin_Bar $wp_admin_bar |
||
97 | * @global wpdb $wpdb |
||
98 | */ |
||
99 | public function admin_bar_menu() { |
||
100 | global $wp_admin_bar, $wpdb; |
||
101 | $db = new DB\Database( $wpdb ); |
||
102 | $tables = $db->get_tables(); |
||
103 | if ( count( $tables ) > 10 ) { |
||
104 | return; |
||
105 | } |
||
106 | foreach ( $tables as $table ) { |
||
107 | if ( ! DB\Grants::current_user_can( DB\Grants::CREATE, $table->get_name() ) ) { |
||
108 | continue; |
||
109 | } |
||
110 | $wp_admin_bar->add_menu( array( |
||
111 | 'parent' => 'new-content', |
||
112 | 'id' => TABULATE_SLUG . '-' . $table->get_name(), |
||
113 | 'title' => $table->get_title(), |
||
114 | 'href' => $table->get_url( 'index', null, 'record' ), |
||
0 ignored issues
–
show
|
|||
115 | ) ); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Print the currently-stored output; this is the callback for all the menu items. |
||
121 | */ |
||
122 | public function output() { |
||
123 | echo $this->output; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Create and dispatch the controller, capturing its output for use later |
||
128 | * in the callback for the menu items. |
||
129 | * |
||
130 | * @return string The HTML to display. |
||
131 | */ |
||
132 | public function dispatch() { |
||
133 | $request = $_REQUEST; |
||
134 | |||
135 | // Only dispatch when it's our page. |
||
136 | $slug_lenth = strlen( TABULATE_SLUG ); |
||
137 | if ( ! isset( $request['page'] ) || substr( $request['page'], 0, $slug_lenth ) !== TABULATE_SLUG ) { |
||
138 | return; |
||
139 | } |
||
140 | |||
141 | // Discern the controller name, based on an explicit request parameter, or |
||
142 | // the trailing part of the page slug (i.e. after 'tabulate_'). |
||
143 | $controller_name = 'home'; |
||
144 | if ( isset( $request['controller'] ) && strlen( $request['controller'] ) > 0 ) { |
||
145 | $controller_name = $request['controller']; |
||
146 | } elseif ( isset( $request['page'] ) && strlen( $request['page'] ) > $slug_lenth ) { |
||
147 | $controller_name = substr( $request['page'], $slug_lenth + 1 ); |
||
148 | } |
||
149 | |||
150 | // Create the controller and run the action. |
||
151 | $controller_classname = '\\WordPress\\Tabulate\\Controllers\\' . ucfirst( $controller_name ) . 'Controller'; |
||
152 | if ( ! class_exists( $controller_classname ) ) { |
||
153 | TabulateException::wp_die( "Controller '$controller_name' not found", 'Error', "Class doesn't exist: $controller_classname" ); |
||
154 | } |
||
155 | $controller = new $controller_classname( $this->wpdb ); |
||
156 | $controller->set_filesystem( $this->filesystem ); |
||
157 | $action = ! empty( $request['action'] ) ? $request['action'] : 'index'; |
||
158 | unset( $request['page'], $request['controller'], $request['action'] ); |
||
159 | try { |
||
160 | $this->output = $controller->$action( $request ); |
||
161 | } catch ( Exception $e ) { |
||
162 | $this->output = '<h1>An error occured</h1><div class="error"><p>' . $e->getMessage() . '</p></div>'; |
||
163 | if ( WP_DEBUG ) { |
||
164 | $this->output .= '<h2>Stack trace</h2><pre>' . $e->getTraceAsString() . '</pre>'; |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * This is the callback method used in self::init() to add scripts and |
||
171 | * styles to the Tabulate admin pages and everywhere the shortcode is used. |
||
172 | * |
||
173 | * @param string $page The current page name. |
||
174 | * @return void |
||
175 | */ |
||
176 | public function enqueue( $page ) { |
||
177 | // Make sure we only enqueue on Tabulate pages. |
||
178 | $allowed_pages = array( |
||
179 | 'index.php', // For the Dashboard widget. |
||
180 | 'tabulate_shortcode', // Not really a page. |
||
181 | 'toplevel_page_tabulate', |
||
182 | 'tabulate_page_tabulate_erd', |
||
183 | 'tabulate_page_tabulate_reports', |
||
184 | 'tabulate_page_tabulate_grants', |
||
185 | 'tabulate_page_tabulate_schema', |
||
186 | ); |
||
187 | if ( ! ( empty( $page ) || in_array( $page, $allowed_pages, true ) ) ) { |
||
188 | return; |
||
189 | } |
||
190 | |||
191 | // Register dependency scripts. |
||
192 | $maskedinput_url = plugins_url( TABULATE_SLUG ) . '/assets/jquery.maskedinput.min.js'; |
||
193 | wp_register_script( 'tabulate-maskedinput', $maskedinput_url, array( 'jquery' ), '1.4.1', true ); |
||
194 | $timepicker_url = plugins_url( TABULATE_SLUG ) . '/assets/jquery-ui-timepicker-addon.min.js'; |
||
195 | wp_register_script( 'tabulate-timepicker', $timepicker_url, array( 'jquery-ui-datepicker' ), TABULATE_VERSION, true ); |
||
196 | $onmivore_url = plugins_url( TABULATE_SLUG ) . '/assets/leaflet/leaflet-omnivore.min.js'; |
||
197 | wp_register_script( 'tabulate-onmivore', $onmivore_url, array( 'tabulate-leaflet' ), TABULATE_VERSION, true ); |
||
198 | $leaflet_url = plugins_url( TABULATE_SLUG ) . '/assets/leaflet/leaflet.js'; |
||
199 | wp_register_script( 'tabulate-leaflet', $leaflet_url, null, TABULATE_VERSION, true ); |
||
200 | |||
201 | // Enqueue Tabulate's scripts. |
||
202 | $script_url = plugins_url( TABULATE_SLUG ) . '/assets/scripts.js'; |
||
203 | $deps = array( 'jquery-ui-autocomplete', 'tabulate-leaflet', 'tabulate-maskedinput', 'tabulate-timepicker' ); |
||
204 | if ( Util::is_plugin_active( 'rest-api/plugin.php' ) ) { |
||
205 | $deps[] = 'wp-api'; |
||
206 | } |
||
207 | wp_enqueue_script( 'tabulate-scripts', $script_url, $deps, TABULATE_VERSION, true ); |
||
208 | |||
209 | // Javascript page variables. |
||
210 | $js_vars = array( |
||
211 | 'admin_url' => admin_url() . 'admin.php?page=' . TABULATE_SLUG, |
||
212 | ); |
||
213 | wp_localize_script( 'tabulate-scripts', 'tabulate', $js_vars ); |
||
214 | |||
215 | // Add stylesheets. |
||
216 | $timepicker_url = plugins_url( TABULATE_SLUG ) . '/assets/jquery-ui-timepicker-addon.css'; |
||
217 | wp_enqueue_style( 'tabulate-timepicker', $timepicker_url, null, TABULATE_VERSION ); |
||
218 | $leaflet_css_url = plugins_url( TABULATE_SLUG ) . '/assets/leaflet/leaflet.css'; |
||
219 | wp_enqueue_style( 'tabulate-leaflet', $leaflet_css_url, null, TABULATE_VERSION ); |
||
220 | $jqueryui_url = plugins_url( TABULATE_SLUG ) . '/assets/jquery-ui/jquery-ui.min.css'; |
||
221 | wp_enqueue_style( 'tabulate-jquery-ui', $jqueryui_url, null, TABULATE_VERSION ); |
||
222 | $jqueryui_theme_url = plugins_url( TABULATE_SLUG ) . '/assets/jquery-ui/jquery-ui.theme.min.css'; |
||
223 | wp_enqueue_style( 'tabulate-jquery-ui-theme', $jqueryui_theme_url, null, TABULATE_VERSION ); |
||
224 | $style_url = plugins_url( TABULATE_SLUG ) . '/assets/style.css'; |
||
225 | wp_enqueue_style( 'tabulate-styles', $style_url, null, TABULATE_VERSION ); |
||
226 | } |
||
227 | } |
||
228 |
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: