admin.php
4 months ago
helpers.php
6 months ago
issues.php
7 months ago
news.php
7 months ago
ratings.php
7 months ago
releases.txt
2 years ago
rest.php
7 months ago
admin.php
510 lines
| 1 | <?php |
| 2 | |
| 3 | if ( !class_exists( 'MeowKit_MWAI_Admin' ) ) { |
| 4 | |
| 5 | class MeowKit_MWAI_Admin { |
| 6 | public static $loaded = false; |
| 7 | public static $version = '4.0'; |
| 8 | public static $admin_version = '4.0'; |
| 9 | public static $network_license_modal_added = false; |
| 10 | public static $network_license_plugins = []; |
| 11 | |
| 12 | /** |
| 13 | * Storage for instances that need deferred initialization. |
| 14 | * |
| 15 | * WordPress Loading Sequence Problem: |
| 16 | * 1. Load all plugin files |
| 17 | * 2. Fire 'plugins_loaded' hook ← Most plugins instantiate Admin here |
| 18 | * 3. Load wp-includes/pluggable.php ← current_user_can() defined here |
| 19 | * 4. Fire 'init' hook ← Safe to use pluggable functions |
| 20 | * |
| 21 | * When plugins instantiate during 'plugins_loaded', the pluggable functions |
| 22 | * (current_user_can, wp_get_current_user) don't exist yet. This array stores |
| 23 | * instances until 'init' when we can safely call those functions. |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | private static $deferred_instances = array(); |
| 28 | |
| 29 | public $prefix; // prefix used for actions, filters (mfrh) |
| 30 | public $mainfile; // plugin main file (media-file-renamer.php) |
| 31 | public $domain; // domain used for translation (media-file-renamer) |
| 32 | public $isPro = false; |
| 33 | |
| 34 | // Store constructor params that affect per-instance setup |
| 35 | private $disableReview = false; |
| 36 | |
| 37 | public static $logo = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNjQiIGhlaWdodD0iNDYiIHZpZXdCb3g9IjAgMCA2NCA0NiIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICA8ZyBjbGlwUGF0aD0idXJsKCNjbGlwMF8zMTBfMjI5KSI+CiAgICA8cGF0aCBkPSJNNjQgMzAuNjQwOEM2NCAyNy43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4KICAgIDxwYXRoIGQ9Ik00MS43MzQyIDMzLjEzNjFDNDIuOTcwOSAzMy4xMzYxIDQzLjk3MzUgMzEuMjgwNSA0My45NzM1IDI4Ljk5MTVDNDMuOTczNSAyNi43MDI0IDQyLjk3MDkgMjQuODQ2OCA0MS43MzQyIDI0Ljg0NjhDNDAuNDk3NSAyNC44NDY4IDM5LjQ5NSAyNi43MDI0IDM5LjQ5NSAyOC45OTE1QzM5LjQ5NSAzMS4yODA1IDQwLjQ5NzUgMzMuMTM2MSA0MS43MzQyIDMzLjEzNjFaIiBmaWxsPSIjM0M2RThCIi8+CiAgPC9nPgogIDxkZWZzPgogICAgPGNsaXBQYXRoIGlkPSJjbGlwMF8zMTBfMjI5Ij4KICAgICAgPHJlY3Qgd2lkdGg9IjY0IiBoZWlnaHQ9IjQ1Ljk2MTciIGZpbGw9IndoaXRlIiB0cmFuc2Zvcm09InRyYW5zbGF0ZSgwIDAuMDE5MTY1KSIvPgogICAgPC9jbGlwUGF0aD4KICA8L2RlZnM+Cjwvc3ZnPgo='; |
| 38 | |
| 39 | public function __construct( $prefix, $mainfile, $domain, $isPro = false, $disableReview = false, $freeOnly = false ) { |
| 40 | |
| 41 | // ALWAYS set instance properties first - these are needed regardless of when setup runs |
| 42 | $this->prefix = $prefix; |
| 43 | $this->mainfile = $mainfile; |
| 44 | $this->domain = $domain; |
| 45 | $this->isPro = $isPro; |
| 46 | $this->disableReview = $disableReview; |
| 47 | |
| 48 | if ( is_admin() ) { |
| 49 | |
| 50 | // Skip AJAX and REST requests to avoid unnecessary processing |
| 51 | if ( MeowKit_MWAI_Helpers::is_asynchronous_request() ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // Check if WordPress pluggable functions are available yet. |
| 56 | // These are defined in wp-includes/pluggable.php, which WordPress loads |
| 57 | // AFTER the 'plugins_loaded' hook but BEFORE the 'init' hook. |
| 58 | if ( !function_exists( 'current_user_can' ) || !function_exists( 'wp_get_current_user' ) ) { |
| 59 | // Functions don't exist yet - defer admin setup until 'init' hook |
| 60 | // This is NORMAL behavior when plugins instantiate on 'plugins_loaded' |
| 61 | $this->defer_admin_setup(); |
| 62 | // Continue to rest of constructor (filters, license checks, etc.) |
| 63 | } else { |
| 64 | // Functions already exist - safe to run admin setup immediately |
| 65 | // This happens when plugins instantiate on 'init' or later |
| 66 | $this->run_admin_setup(); |
| 67 | } |
| 68 | |
| 69 | // License-related admin notices (doesn't require pluggable functions) |
| 70 | $license = get_option( $this->prefix . '_license', '' ); |
| 71 | if ( !empty( $license ) && !$this->isPro ) { |
| 72 | add_action( 'admin_notices', [ $this, 'admin_notices_licensed_free' ] ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // ALWAYS register these filters (they work at any time) |
| 77 | add_filter( 'plugin_row_meta', [ $this, 'custom_plugin_row_meta' ], 10, 2 ); |
| 78 | add_filter( 'edd_sl_api_request_verify_ssl', [ $this, 'request_verify_ssl' ], 10, 0 ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Defer admin setup until WordPress 'init' hook. |
| 83 | * |
| 84 | * This method stores the current instance and registers a one-time |
| 85 | * 'init' hook callback that will process all deferred instances. |
| 86 | * |
| 87 | * Why defer? Because we need current_user_can() to check permissions, |
| 88 | * and that function doesn't exist until after 'plugins_loaded'. |
| 89 | */ |
| 90 | private function defer_admin_setup() { |
| 91 | // Add this instance to the queue for processing on 'init' |
| 92 | self::$deferred_instances[] = $this; |
| 93 | |
| 94 | // Register the 'init' hook only once (for the first deferred instance) |
| 95 | if ( count( self::$deferred_instances ) === 1 ) { |
| 96 | add_action( 'init', array( __CLASS__, 'process_deferred_instances' ) ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Static callback for 'init' hook - processes all deferred instances. |
| 102 | * |
| 103 | * By the time 'init' fires, WordPress has loaded pluggable.php and |
| 104 | * current_user_can() is guaranteed to exist. We process all instances |
| 105 | * that were created during 'plugins_loaded' or earlier. |
| 106 | * |
| 107 | * This is called as a static method because it processes multiple instances. |
| 108 | */ |
| 109 | public static function process_deferred_instances() { |
| 110 | // Belt-and-suspenders check: pluggable functions should ALWAYS exist by 'init' |
| 111 | // If they somehow don't, log a warning and bail (this should never happen) |
| 112 | if ( !function_exists( 'current_user_can' ) || !function_exists( 'wp_get_current_user' ) ) { |
| 113 | trigger_error( |
| 114 | 'MeowKit_MWAI_Admin: Pluggable functions still unavailable on init hook. ' . |
| 115 | 'This should never happen and indicates a serious WordPress core issue.', |
| 116 | E_USER_WARNING |
| 117 | ); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | // Process each deferred instance's admin setup |
| 122 | foreach ( self::$deferred_instances as $instance ) { |
| 123 | $instance->run_admin_setup(); |
| 124 | } |
| 125 | |
| 126 | // Clear the array to free memory (we won't need these references anymore) |
| 127 | self::$deferred_instances = array(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Run admin setup - both shared (once) and per-instance (each plugin). |
| 132 | * |
| 133 | * SHARED SETUP (once for all plugins): |
| 134 | * - Issues detection |
| 135 | * - Meow Apps menu creation |
| 136 | * - Admin footer customization |
| 137 | * |
| 138 | * PER-INSTANCE SETUP (once per plugin): |
| 139 | * - Ratings system |
| 140 | * - News system |
| 141 | * |
| 142 | * This method is called either immediately (if pluggable functions exist) |
| 143 | * or deferred until 'init' (if they don't). Either way, it's safe to call |
| 144 | * current_user_can() here. |
| 145 | */ |
| 146 | private function run_admin_setup() { |
| 147 | // SHARED SETUP: Only run once for all Meow Apps plugins |
| 148 | if ( !MeowKit_MWAI_Admin::$loaded ) { |
| 149 | // Check for potential issues with WordPress install, other plugins, etc. |
| 150 | new MeowKit_MWAI_Issues( $this->prefix, $this->mainfile, $this->domain ); |
| 151 | |
| 152 | // Create the unified Meow Apps menu (priority 5 to ensure early creation) |
| 153 | add_action( 'admin_menu', [ $this, 'admin_menu_start' ], 5 ); |
| 154 | |
| 155 | // Customize admin footer on Meow Apps pages |
| 156 | $page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : null; |
| 157 | if ( $page === 'meowapps-main-menu' ) { |
| 158 | add_filter( 'admin_footer_text', [ $this, 'admin_footer_text' ], 100000, 1 ); |
| 159 | } |
| 160 | |
| 161 | MeowKit_MWAI_Admin::$loaded = true; |
| 162 | } |
| 163 | |
| 164 | // PER-INSTANCE SETUP: Run for each plugin that uses this library |
| 165 | // Only admins get ratings prompts and news |
| 166 | if ( $this->is_user_admin() ) { |
| 167 | if ( !$this->disableReview ) { |
| 168 | new MeowKit_MWAI_Ratings( $this->prefix, $this->mainfile, $this->domain ); |
| 169 | } |
| 170 | new MeowKit_MWAI_News( $this->domain ); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Check if current user is a site administrator. |
| 176 | * |
| 177 | * This method is only called from run_admin_setup(), which guarantees |
| 178 | * that pluggable functions exist. No error logging needed - if the |
| 179 | * functions don't exist, we simply return false as a defensive fallback. |
| 180 | * |
| 181 | * @return bool True if user can manage options, false otherwise |
| 182 | */ |
| 183 | public function is_user_admin() { |
| 184 | // Defensive check (should never fail if called from run_admin_setup) |
| 185 | if ( !function_exists( 'current_user_can' ) || !function_exists( 'wp_get_current_user' ) ) { |
| 186 | return false; |
| 187 | } |
| 188 | return current_user_can( 'manage_options' ); |
| 189 | } |
| 190 | |
| 191 | public function custom_plugin_row_meta( $links, $file ) { |
| 192 | $path = pathinfo( $file ); |
| 193 | $pathName = basename( $path['dirname'] ); |
| 194 | $thisPath = pathinfo( $this->mainfile ); |
| 195 | $thisPathName = basename( $thisPath['dirname'] ); |
| 196 | $isActive = is_plugin_active( $file ); |
| 197 | if ( !$isActive ) { |
| 198 | return $links; |
| 199 | } |
| 200 | $isIssue = $this->isPro && !$this->is_registered(); |
| 201 | if ( strpos( $pathName, $thisPathName ) !== false ) { |
| 202 | // In network admin, handle differently (no settings page available) |
| 203 | if ( is_network_admin() ) { |
| 204 | if ( $this->isPro && !$this->is_registered() ) { |
| 205 | // Show "Register License" link for unregistered Pro plugins |
| 206 | $new_links = [ |
| 207 | 'license' => sprintf( |
| 208 | '<a href="#" class="meowapps-network-license-link" data-prefix="%s" data-plugin="%s" style="color: #d63638;">%s</a>', |
| 209 | esc_attr( $this->prefix ), |
| 210 | esc_attr( $this->nice_name_from_file( $this->mainfile ) ), |
| 211 | __( 'Register License', $this->domain ) |
| 212 | ), |
| 213 | ]; |
| 214 | // Track this plugin for the modal |
| 215 | self::$network_license_plugins[ $this->prefix ] = $this->nice_name_from_file( $this->mainfile ); |
| 216 | // Add modal output hook (only once) |
| 217 | if ( !self::$network_license_modal_added ) { |
| 218 | add_action( 'admin_footer', [ __CLASS__, 'output_network_license_modal' ] ); |
| 219 | self::$network_license_modal_added = true; |
| 220 | } |
| 221 | } |
| 222 | elseif ( $this->isPro && $this->is_registered() ) { |
| 223 | // Pro plugin is registered |
| 224 | $new_links = [ |
| 225 | 'license' => '<span style="color: #a75bd6;">' . __( 'Pro Version', $this->domain ) . '</span>', |
| 226 | ]; |
| 227 | } |
| 228 | else { |
| 229 | // Free plugin |
| 230 | $new_links = [ |
| 231 | 'license' => sprintf( '<span>' . __( '<a target="_blank" href="https://meowapps.com">Get the <u>Pro Version</u></a>', $this->domain ), $this->prefix ) . '</span>', |
| 232 | ]; |
| 233 | } |
| 234 | } |
| 235 | else { |
| 236 | // Regular admin - show settings and license status |
| 237 | $new_links = [ |
| 238 | 'settings' => |
| 239 | sprintf( __( '<a href="admin.php?page=%s_settings">Settings</a>', $this->domain ), $this->prefix ), |
| 240 | 'license' => |
| 241 | $this->is_registered() ? |
| 242 | ( '<span style="color: #a75bd6;">' . __( 'Pro Version', $this->domain ) . '</span>' ) : |
| 243 | ( $isIssue ? ( sprintf( '<span style="color: #ff3434;">' . __( 'License Issue', $this->domain ), $this->prefix ) . '</span>' ) : ( sprintf( '<span>' . __( '<a target="_blank" href="https://meowapps.com">Get the <u>Pro Version</u></a>', $this->domain ), $this->prefix ) . '</span>' ) ), |
| 244 | ]; |
| 245 | } |
| 246 | $links = array_merge( $new_links, $links ); |
| 247 | } |
| 248 | return $links; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Output the network license registration modal. |
| 253 | * Called via admin_footer hook in network admin. |
| 254 | */ |
| 255 | public static function output_network_license_modal() { |
| 256 | $rest_url = esc_url( rest_url() ); |
| 257 | $nonce = wp_create_nonce( 'wp_rest' ); |
| 258 | ?> |
| 259 | <div id="meowapps-network-license-modal" style="display:none; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.6); z-index:100000; align-items:center; justify-content:center;"> |
| 260 | <div style="background:#fff; padding:24px; border-radius:8px; max-width:450px; width:90%; box-shadow:0 4px 20px rgba(0,0,0,0.3);"> |
| 261 | <h2 style="margin:0 0 8px 0; font-size:18px;">Register License</h2> |
| 262 | <p id="meowapps-license-plugin-name" style="margin:0 0 16px 0; color:#666;"></p> |
| 263 | <input type="text" id="meowapps-license-key-input" placeholder="Enter your license key" style="width:100%; padding:10px; font-size:14px; border:1px solid #8c8f94; border-radius:4px; box-sizing:border-box;" /> |
| 264 | <p id="meowapps-license-message" style="margin:12px 0 0 0; padding:10px; border-radius:4px; display:none;"></p> |
| 265 | <div style="margin-top:16px; display:flex; gap:10px; justify-content:flex-end;"> |
| 266 | <button type="button" id="meowapps-license-cancel" class="button">Cancel</button> |
| 267 | <button type="button" id="meowapps-license-submit" class="button button-primary">Validate & Register</button> |
| 268 | </div> |
| 269 | </div> |
| 270 | </div> |
| 271 | <script> |
| 272 | (function() { |
| 273 | var modal = document.getElementById('meowapps-network-license-modal'); |
| 274 | var input = document.getElementById('meowapps-license-key-input'); |
| 275 | var message = document.getElementById('meowapps-license-message'); |
| 276 | var pluginName = document.getElementById('meowapps-license-plugin-name'); |
| 277 | var submitBtn = document.getElementById('meowapps-license-submit'); |
| 278 | var cancelBtn = document.getElementById('meowapps-license-cancel'); |
| 279 | var currentPrefix = ''; |
| 280 | |
| 281 | function showMessage(text, isError) { |
| 282 | message.textContent = text; |
| 283 | message.style.display = 'block'; |
| 284 | message.style.background = isError ? '#fcf0f1' : '#edfaef'; |
| 285 | message.style.color = isError ? '#d63638' : '#1e7e34'; |
| 286 | message.style.border = '1px solid ' + (isError ? '#d63638' : '#1e7e34'); |
| 287 | } |
| 288 | |
| 289 | function hideMessage() { |
| 290 | message.style.display = 'none'; |
| 291 | } |
| 292 | |
| 293 | function openModal(prefix, plugin) { |
| 294 | currentPrefix = prefix; |
| 295 | pluginName.textContent = plugin; |
| 296 | input.value = ''; |
| 297 | hideMessage(); |
| 298 | submitBtn.disabled = false; |
| 299 | submitBtn.textContent = 'Validate & Register'; |
| 300 | modal.style.display = 'flex'; |
| 301 | input.focus(); |
| 302 | } |
| 303 | |
| 304 | function closeModal() { |
| 305 | modal.style.display = 'none'; |
| 306 | currentPrefix = ''; |
| 307 | } |
| 308 | |
| 309 | // Handle click on "Register License" links |
| 310 | document.addEventListener('click', function(e) { |
| 311 | if (e.target.classList.contains('meowapps-network-license-link')) { |
| 312 | e.preventDefault(); |
| 313 | var prefix = e.target.getAttribute('data-prefix'); |
| 314 | var plugin = e.target.getAttribute('data-plugin'); |
| 315 | openModal(prefix, plugin); |
| 316 | } |
| 317 | }); |
| 318 | |
| 319 | // Close modal on cancel or clicking outside |
| 320 | cancelBtn.addEventListener('click', closeModal); |
| 321 | modal.addEventListener('click', function(e) { |
| 322 | if (e.target === modal) closeModal(); |
| 323 | }); |
| 324 | |
| 325 | // Handle escape key |
| 326 | document.addEventListener('keydown', function(e) { |
| 327 | if (e.key === 'Escape' && modal.style.display === 'flex') { |
| 328 | closeModal(); |
| 329 | } |
| 330 | }); |
| 331 | |
| 332 | // Handle enter key in input |
| 333 | input.addEventListener('keydown', function(e) { |
| 334 | if (e.key === 'Enter') { |
| 335 | submitBtn.click(); |
| 336 | } |
| 337 | }); |
| 338 | |
| 339 | // Submit license |
| 340 | submitBtn.addEventListener('click', function() { |
| 341 | var licenseKey = input.value.trim(); |
| 342 | if (!licenseKey) { |
| 343 | showMessage('Please enter a license key.', true); |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | submitBtn.disabled = true; |
| 348 | submitBtn.textContent = 'Validating...'; |
| 349 | hideMessage(); |
| 350 | |
| 351 | var restUrl = '<?php echo $rest_url; ?>meow-licenser/' + currentPrefix + '/v1/set_license/'; |
| 352 | |
| 353 | fetch(restUrl, { |
| 354 | method: 'POST', |
| 355 | headers: { |
| 356 | 'Content-Type': 'application/json', |
| 357 | 'X-WP-Nonce': '<?php echo $nonce; ?>' |
| 358 | }, |
| 359 | body: JSON.stringify({ serialKey: licenseKey }) |
| 360 | }) |
| 361 | .then(function(response) { return response.json(); }) |
| 362 | .then(function(data) { |
| 363 | if (data.success && data.data && !data.data.issue) { |
| 364 | showMessage('License registered successfully! Reloading...', false); |
| 365 | setTimeout(function() { location.reload(); }, 1500); |
| 366 | } else { |
| 367 | var errorMsg = 'License validation failed.'; |
| 368 | if (data.data && data.data.issue) { |
| 369 | errorMsg = 'License issue: ' + data.data.issue; |
| 370 | } |
| 371 | showMessage(errorMsg, true); |
| 372 | submitBtn.disabled = false; |
| 373 | submitBtn.textContent = 'Validate & Register'; |
| 374 | } |
| 375 | }) |
| 376 | .catch(function(error) { |
| 377 | showMessage('Error: ' + error.message, true); |
| 378 | submitBtn.disabled = false; |
| 379 | submitBtn.textContent = 'Validate & Register'; |
| 380 | }); |
| 381 | }); |
| 382 | })(); |
| 383 | </script> |
| 384 | <?php |
| 385 | } |
| 386 | |
| 387 | public function request_verify_ssl() { |
| 388 | return get_option( 'force_sslverify', false ); |
| 389 | } |
| 390 | |
| 391 | public function nice_name_from_file( $file ) { |
| 392 | $info = pathinfo( $file ); |
| 393 | if ( !empty( $info ) ) { |
| 394 | if ( $info['filename'] == 'wplr-sync' ) { |
| 395 | return 'WP/LR Sync'; |
| 396 | } |
| 397 | $info['filename'] = str_replace( '-', ' ', $info['filename'] ); |
| 398 | $file = ucwords( $info['filename'] ); |
| 399 | } |
| 400 | return $file; |
| 401 | } |
| 402 | |
| 403 | public function admin_notices_licensed_free() { |
| 404 | if ( isset( $_POST[$this->prefix . '_reset_sub'] ) ) { |
| 405 | delete_option( $this->prefix . '_pro_serial' ); |
| 406 | delete_option( $this->prefix . '_license' ); |
| 407 | return; |
| 408 | } |
| 409 | $html = '<div class="notice notice-error">'; |
| 410 | $html .= sprintf( |
| 411 | __( '<p>It looks like you are using the free version of the plugin (<b>%s</b>) but a license for the Pro version was also found. The Pro version might have been replaced by the Free version during an update (might be caused by a temporarily issue). If it is the case, <b>please download it again</b> from the <a target="_blank" href="https://meowapps.com">Meow Store</a>. If you wish to continue using the free version and clear this message, click on this button.', $this->domain ), |
| 412 | $this->nice_name_from_file( $this->mainfile ) |
| 413 | ); |
| 414 | $html .= '<p> |
| 415 | <form method="post" action=""> |
| 416 | <input type="hidden" name="' . $this->prefix . '_reset_sub" value="true"> |
| 417 | <input type="submit" name="submit" id="submit" class="button" value="' |
| 418 | . __( 'Remove the license', $this->domain ) . '"> |
| 419 | </form> |
| 420 | </p>'; |
| 421 | $html .= '</div>'; |
| 422 | wp_kses_post( $html ); |
| 423 | } |
| 424 | |
| 425 | public function admin_menu_start() { |
| 426 | // Hide the admin if user doesn't like Meow much |
| 427 | if ( get_option( 'meowapps_hide_meowapps', false ) ) { |
| 428 | register_setting( 'general', 'meowapps_hide_meowapps' ); |
| 429 | add_settings_field( 'meowapps_hide_ads', 'Meow Apps Menu', [ $this, 'meowapps_hide_dashboard_callback' ], 'general' ); |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | // Create standard menu if it does not already exist |
| 434 | global $submenu; |
| 435 | if ( !isset( $submenu[ 'meowapps-main-menu' ] ) ) { |
| 436 | add_menu_page( |
| 437 | 'Meow Apps', |
| 438 | '<img alt="Meow Apps" style="width: 21px; margin-left: -28px; position: absolute; margin-top: 2px;" src="' . MeowKit_MWAI_Admin::$logo . '" />Meow Apps', |
| 439 | 'manage_options', |
| 440 | 'meowapps-main-menu', |
| 441 | [ $this, 'admin_meow_apps' ], |
| 442 | '', |
| 443 | 82 |
| 444 | ); |
| 445 | add_submenu_page( |
| 446 | 'meowapps-main-menu', |
| 447 | __( 'Dashboard', $this->domain ), |
| 448 | __( 'Dashboard', $this->domain ), |
| 449 | 'manage_options', |
| 450 | 'meowapps-main-menu', |
| 451 | [ $this, 'admin_meow_apps' ] |
| 452 | ); |
| 453 | } |
| 454 | |
| 455 | // Add CSS to hide the default icon |
| 456 | add_action( 'admin_head', function () { |
| 457 | echo '<style> |
| 458 | #toplevel_page_meowapps-main-menu .wp-menu-image { |
| 459 | display: none; |
| 460 | } |
| 461 | </style>'; |
| 462 | } ); |
| 463 | } |
| 464 | |
| 465 | public function meowapps_hide_dashboard_callback() { |
| 466 | $html = '<input type="checkbox" id="meowapps_hide_meowapps" name="meowapps_hide_meowapps" value="1" ' . |
| 467 | checked( 1, get_option( 'meowapps_hide_meowapps' ), false ) . '/>'; |
| 468 | $html .= __( '<label>Hide <b>Meow Apps</b> Menu</label><br /><small>Hide Meow Apps menu and all its components, for a cleaner admin. This option will be reset if a new Meow Apps plugin is installed.<br /><b>Once activated, an option will be added in your General settings to display it again.</b></small>', $this->domain ); |
| 469 | echo MeowKit_MWAI_Helpers::wp_kses( $html ); |
| 470 | } |
| 471 | |
| 472 | public function is_registered() { |
| 473 | $is_registered = apply_filters( $this->prefix . '_meowapps_is_registered', false, $this->prefix ); |
| 474 | return $is_registered; |
| 475 | } |
| 476 | |
| 477 | public function get_phpinfo() { |
| 478 | if ( !$this->is_user_admin() || !function_exists( 'phpinfo' ) ) { |
| 479 | return; |
| 480 | } |
| 481 | ob_start(); |
| 482 | // phpcs:disable WordPress.PHP.DevelopmentFunctions |
| 483 | phpinfo( INFO_GENERAL | INFO_CONFIGURATION | INFO_MODULES ); |
| 484 | // phpcs:enable |
| 485 | $html = ob_get_contents(); |
| 486 | ob_end_clean(); |
| 487 | $html = preg_replace( '%^.*<body>(.*)</body>.*$%ms', '$1', $html ); |
| 488 | return $html; |
| 489 | } |
| 490 | |
| 491 | public function admin_meow_apps() { |
| 492 | $html = "<div id='meow-common-dashboard'></div>"; |
| 493 | $html .= "<div style='height: 0; width: 0; overflow: hidden;' id='meow-common-phpinfo'>"; |
| 494 | $html .= $this->get_phpinfo(); |
| 495 | $html .= '</div>'; |
| 496 | $html = preg_replace( "/<img[^>]+\>/i", '', $html ); |
| 497 | echo wp_kses_post( $html ); |
| 498 | } |
| 499 | |
| 500 | public function admin_footer_text( $current ) { |
| 501 | return sprintf( |
| 502 | // translators: %1$s is the version of the interface; %2$s is a file path. |
| 503 | __( 'Thanks for using <a href="https://meowapps.com">Meow Apps</a>! This is the Meow Admin %1$s <br /><i>Loaded from %2$s </i>', $this->domain ), |
| 504 | MeowKit_MWAI_Admin::$version, |
| 505 | __FILE__ |
| 506 | ); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 |