| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || exit; // Prevent direct access. |
| 3 |
|
| 4 |
/** |
| 5 |
* Featherweight's bridge to the shared Folium UI frame. |
| 6 |
* |
| 7 |
* Registers the plugin under the single "Folium" admin menu, enqueues its JS |
| 8 |
* app, and handles the app's save/reset ajax. The pre-Folium standalone |
| 9 |
* dashboard and the cross-plugin add-on installer were removed in 2.2.0 — |
| 10 |
* Folium UI owns the admin shell and the JS app owns the settings screen. |
| 11 |
*/ |
| 12 |
class Optimisationio_Dashboard { |
| 13 |
|
| 14 |
private static $instance = null; |
| 15 |
|
| 16 |
public function __construct() { |
| 17 |
|
| 18 |
// folium-ui is vendored and loaded unconditionally from the main plugin |
| 19 |
// file (plugins_loaded:4), so Folium_UI is always defined by the time this |
| 20 |
// constructs (plugins_loaded:10). The class_exists guard is belt-and-braces. |
| 21 |
if ( ! class_exists( 'Folium_UI' ) ) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
// Nest Featherweight under the single shared "Folium" admin menu and open |
| 26 |
// in-frame at admin.php?page=wp-disable. No own top-level item. The slug |
| 27 |
// stays wp-disable (the permanent wp.org slug); only the brand changes. |
| 28 |
Folium_UI::register_plugin( array( |
| 29 |
'slug' => 'wp-disable', |
| 30 |
'name' => __( 'Featherweight', 'wp-disable' ), |
| 31 |
'tagline' => __( 'Bloat & request removal', 'wp-disable' ), |
| 32 |
'icon' => 'F', |
| 33 |
'icon_url' => plugin_dir_url( dirname( __FILE__ ) ) . 'images/featherweight-icon.png', |
| 34 |
) ); |
| 35 |
add_action( 'folium_ui_enqueue', array( $this, 'on_folium_enqueue' ) ); |
| 36 |
add_action( 'wp_ajax_wp_disable_app_save', array( $this, 'ajax_app_save' ) ); |
| 37 |
add_action( 'wp_ajax_wp_disable_app_reset', array( $this, 'ajax_app_reset' ) ); |
| 38 |
// Redirect the legacy top-level slug to the in-frame Folium page. |
| 39 |
add_action( 'admin_init', array( $this, 'redirect_legacy_slug' ) ); |
| 40 |
} |
| 41 |
|
| 42 |
public static function init() { |
| 43 |
if ( null === self::$instance ) { |
| 44 |
self::$instance = new self(); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Enqueue the Featherweight Folium app (JS + CSS) and inject real settings |
| 50 |
* when the shared frame renders the wp-disable screen. Hooked to |
| 51 |
* folium_ui_enqueue. |
| 52 |
* |
| 53 |
* @param string $slug Plugin slug being rendered by Folium UI. |
| 54 |
*/ |
| 55 |
public function on_folium_enqueue( $slug ) { |
| 56 |
if ( 'wp-disable' !== $slug ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
$url = plugin_dir_url( dirname( __FILE__ ) ); |
| 60 |
$base = plugin_dir_path( dirname( __FILE__ ) ); |
| 61 |
$css = $base . 'assets/css/wp-disable-app.css'; |
| 62 |
$js = $base . 'assets/js/wp-disable-app.js'; |
| 63 |
|
| 64 |
// filemtime versioning so CDN/browser caches never mask a pushed edit |
| 65 |
// (switch to the version constant at wp.org release — see PUBLISHING.md). |
| 66 |
$css_ver = file_exists( $css ) ? (string) filemtime( $css ) : ( defined( 'WP_DISABLE_VERSION' ) ? WP_DISABLE_VERSION : '2.2.0' ); |
| 67 |
$js_ver = file_exists( $js ) ? (string) filemtime( $js ) : ( defined( 'WP_DISABLE_VERSION' ) ? WP_DISABLE_VERSION : '2.2.0' ); |
| 68 |
|
| 69 |
wp_enqueue_style( 'wp-disable-app', $url . 'assets/css/wp-disable-app.css', array( 'folium-ui' ), $css_ver ); |
| 70 |
wp_enqueue_script( 'wp-disable-app', $url . 'assets/js/wp-disable-app.js', array( 'folium-ui', 'folium-app' ), $js_ver, true ); |
| 71 |
wp_localize_script( 'wp-disable-app', 'WPDisableData', $this->app_data() ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Real data injected into the Featherweight Folium app. |
| 76 |
* |
| 77 |
* @return array |
| 78 |
*/ |
| 79 |
private function app_data() { |
| 80 |
$settings = get_option( WpPerformance::OPTION_KEY . '_settings', array() ); |
| 81 |
if ( ! is_array( $settings ) ) { |
| 82 |
$settings = array(); |
| 83 |
} |
| 84 |
|
| 85 |
$post_types = array(); |
| 86 |
foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $pt ) { |
| 87 |
$label = isset( $pt->labels->singular_name ) && $pt->labels->singular_name ? $pt->labels->singular_name : $pt->name; |
| 88 |
$post_types[] = array( 'name' => $pt->name, 'label' => $label ); |
| 89 |
} |
| 90 |
|
| 91 |
return array( |
| 92 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 93 |
'nonce' => wp_create_nonce( 'wp_disable_app' ), |
| 94 |
'actions' => array( |
| 95 |
'save' => 'wp_disable_app_save', |
| 96 |
'reset' => 'wp_disable_app_reset', |
| 97 |
), |
| 98 |
'settings' => $settings, |
| 99 |
'postTypes' => $post_types, |
| 100 |
'flags' => array( |
| 101 |
'woo' => WpPerformance::is_woocommerce_enabled(), |
| 102 |
'seo' => WpPerformance::should_show_seo_tab(), |
| 103 |
), |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Ajax: persist settings from the Folium app. Reuses the exact same |
| 109 |
* sanitisation as the legacy form (WpPerformance_Admin::persist_settings). |
| 110 |
*/ |
| 111 |
public function ajax_app_save() { |
| 112 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 113 |
wp_send_json_error( array( 'message' => 'forbidden' ), 403 ); |
| 114 |
} |
| 115 |
check_ajax_referer( 'wp_disable_app', 'nonce' ); |
| 116 |
|
| 117 |
$raw = isset( $_POST['data'] ) ? json_decode( wp_unslash( $_POST['data'] ), true ) : array(); // phpcs:ignore WordPress.Security.ValidationSanitization.InputNotValidated |
| 118 |
if ( ! is_array( $raw ) ) { |
| 119 |
$raw = array(); |
| 120 |
} |
| 121 |
WpPerformance_Admin::persist_settings( $raw ); |
| 122 |
wp_send_json_success(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Ajax: reset settings to defaults (an empty request writes every default). |
| 127 |
*/ |
| 128 |
public function ajax_app_reset() { |
| 129 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 130 |
wp_send_json_error( array( 'message' => 'forbidden' ), 403 ); |
| 131 |
} |
| 132 |
check_ajax_referer( 'wp_disable_app', 'nonce' ); |
| 133 |
|
| 134 |
WpPerformance_Admin::persist_settings( array() ); |
| 135 |
wp_send_json_success(); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Redirect the retired top-level slug (admin.php?page=optimisationio-dashboard) |
| 140 |
* to the in-frame Folium page so old bookmarks / the plugin row link resolve. |
| 141 |
*/ |
| 142 |
public function redirect_legacy_slug() { |
| 143 |
if ( isset( $_GET['page'] ) && 'optimisationio-dashboard' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 144 |
wp_safe_redirect( admin_url( 'admin.php?page=wp-disable' ) ); |
| 145 |
exit; |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|