| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Detective Wp Admin. |
| 4 |
* |
| 5 |
* @since 0.0.0 |
| 6 |
* @package Plugin_Detective |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Plugin Detective Wp Admin. |
| 11 |
* |
| 12 |
* @since 0.0.0 |
| 13 |
*/ |
| 14 |
class PD_Wp_Admin { |
| 15 |
/** |
| 16 |
* Parent plugin class. |
| 17 |
* |
| 18 |
* @since 0.0.0 |
| 19 |
* |
| 20 |
* @var Plugin_Detective |
| 21 |
*/ |
| 22 |
protected $plugin = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor. |
| 26 |
* |
| 27 |
* @since 0.0.0 |
| 28 |
* |
| 29 |
* @param Plugin_Detective $plugin Main plugin object. |
| 30 |
*/ |
| 31 |
public function __construct( $plugin ) { |
| 32 |
$this->plugin = $plugin; |
| 33 |
$this->hooks(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Initiate our hooks. |
| 38 |
* |
| 39 |
* @since 0.0.0 |
| 40 |
*/ |
| 41 |
public function hooks() { |
| 42 |
add_filter( 'plugin_action_links_' . $this->plugin->basename, array( $this, 'add_action_links' ) ); |
| 43 |
add_action( 'admin_menu', array( $this, 'register_tools_page' ), 1 ); |
| 44 |
add_action( 'admin_init', array( $this, 'redirect_tools_page' ), 1 ); |
| 45 |
add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 1000 ); |
| 46 |
} |
| 47 |
|
| 48 |
public function register_tools_page() { |
| 49 |
add_management_page( 'Plugin Detective', 'Plugin Detective', 'manage_options', 'plugin-detective', array( $this, 'render_tools_page' ) ); |
| 50 |
} |
| 51 |
|
| 52 |
public function add_action_links ( $links ) { |
| 53 |
$mylinks = array( |
| 54 |
'<a href="' . $this->get_tools_page_url() . '"><strong>Troubleshoot Plugin Conflicts</strong></a>', |
| 55 |
); |
| 56 |
|
| 57 |
return array_merge( $links, $mylinks ); |
| 58 |
} |
| 59 |
|
| 60 |
public function get_app_url( $troubleshoot_url='', $authenticated = true ) { |
| 61 |
$url = $this->plugin->url( 'troubleshoot/' ); |
| 62 |
|
| 63 |
if ( !empty( $authenticated ) ) { |
| 64 |
require_once $this->plugin->dir( 'troubleshoot/includes/class-auth.php' ); |
| 65 |
$nonce = PDT_Auth::create_nonce( 'pd_api' ); |
| 66 |
$url = add_query_arg( array( |
| 67 |
'session' => get_current_user_id(), |
| 68 |
'nonce' => $nonce, |
| 69 |
), $url ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( !empty( $troubleshoot_url ) ) { |
| 73 |
$url = add_query_arg( array( |
| 74 |
'url' => urlencode( $troubleshoot_url ) |
| 75 |
), $url ); |
| 76 |
} |
| 77 |
|
| 78 |
return $url; |
| 79 |
} |
| 80 |
|
| 81 |
public function render_tools_page() { |
| 82 |
wp_safe_redirect( $this->get_app_url() ); |
| 83 |
exit(); |
| 84 |
} |
| 85 |
|
| 86 |
public function get_tools_page_url( $troubleshoot_url = '' ) { |
| 87 |
$url = admin_url( 'tools.php?page=plugin-detective' ); |
| 88 |
if ( !empty( $troubleshoot_url ) ) { |
| 89 |
$url = add_query_arg( array( |
| 90 |
'url' => $troubleshoot_url, |
| 91 |
), $url ); |
| 92 |
} |
| 93 |
|
| 94 |
return $url; |
| 95 |
} |
| 96 |
|
| 97 |
public function redirect_tools_page() { |
| 98 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only routing check on admin_init; no state change. |
| 99 |
if ( empty( $_GET['page'] ) || $_GET['page'] !== 'plugin-detective' ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
// Defense in depth: this handler mints an authenticated pd_api token, so gate |
| 104 |
// it on the same capability the API enforces. WordPress's admin-menu access |
| 105 |
// check already blocks low-privileged users from this page, but don't rely on |
| 106 |
// that ordering — never mint a token for a user who couldn't use it anyway. |
| 107 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
if ( class_exists( 'ITSEC_Core' ) && $itsec_storage = get_option( 'itsec-storage' ) ) { |
| 112 |
if ( !empty( $itsec_storage['system-tweaks']['plugins_php'] ) ) { |
| 113 |
echo '<h1>iThemes Security is preventing Plugin Detective from operating properly</h1>'; |
| 114 |
echo '<h3>To fix this: <code>Go to Security > Settings > System Tweaks</code> and <strong>uncheck</strong> the checkbox setting for <code>Disable PHP in Plugins</code></h3>'; |
| 115 |
echo '<h3><a href="'. esc_url( admin_url( 'admin.php?page=itsec&module=system-tweaks&module_type=recommended' ) ).'">Go there now</a></h3>'; |
| 116 |
exit(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
$troubleshoot_url = ''; |
| 121 |
if ( ! empty( $_GET['url'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only redirect parameter; no state change. |
| 122 |
$troubleshoot_url = sanitize_text_field( wp_unslash( $_GET['url'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only redirect parameter. |
| 123 |
} |
| 124 |
|
| 125 |
wp_safe_redirect( $this->get_app_url( $troubleshoot_url ) ); |
| 126 |
exit(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Add the admin bar menu |
| 131 |
*/ |
| 132 |
public function admin_bar_menu() { |
| 133 |
global $wp_admin_bar; |
| 134 |
|
| 135 |
$current_url = ''; |
| 136 |
if ( ! empty( $_SERVER['REQUEST_URI'] ) ) { |
| 137 |
$desired_relative_path = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 138 |
$relative_path_to_wp_directory = (string) wp_parse_url( site_url(), PHP_URL_PATH ); |
| 139 |
|
| 140 |
if ( ! empty( $relative_path_to_wp_directory ) && strpos( $desired_relative_path, $relative_path_to_wp_directory ) === 0 ) { |
| 141 |
$desired_relative_path = substr( $desired_relative_path, strlen( $relative_path_to_wp_directory ) ); |
| 142 |
} |
| 143 |
|
| 144 |
$current_url = site_url( $desired_relative_path ); |
| 145 |
} |
| 146 |
|
| 147 |
// Add top menu |
| 148 |
if ( current_user_can( 'activate_plugins' ) ) { |
| 149 |
$wp_admin_bar->add_menu( array( |
| 150 |
'id' => 'plugin-detective-bar', |
| 151 |
'parent' => '', |
| 152 |
'title' => __( 'Troubleshoot', 'plugin-detective' ), |
| 153 |
'href' => $this->get_tools_page_url( $current_url ) |
| 154 |
) ); |
| 155 |
} |
| 156 |
|
| 157 |
wp_enqueue_style( 'plugin_detective', $this->plugin->url( 'assets/admin.css' ), array(), $this->plugin->version ); |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|