| 1 |
<?php |
| 2 |
/** |
| 3 |
* WordPress Context Detector. |
| 4 |
* |
| 5 |
* @package zip-ai |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ZipAI\Classes\Core; |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* The Context_Detector Class. |
| 17 |
* Detects WordPress admin context for MCP integration. |
| 18 |
*/ |
| 19 |
class Context_Detector { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* Constructor of this class. |
| 25 |
* |
| 26 |
* @since 0.0.1 |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
// Add hooks to detect WordPress context. |
| 31 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_context_scripts' ) ); |
| 32 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_context_scripts' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Enqueue scripts for context detection |
| 37 |
* |
| 38 |
* @since 0.0.1 |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function enqueue_context_scripts() { |
| 42 |
// Check if user has permission to use the assistant. |
| 43 |
if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
$context_data = array( |
| 48 |
'adminUrl' => admin_url(), |
| 49 |
'siteUrl' => home_url(), |
| 50 |
'currentUrl' => $this->get_current_url(), |
| 51 |
'isAdmin' => is_admin(), |
| 52 |
'isFrontend' => ! is_admin(), |
| 53 |
); |
| 54 |
|
| 55 |
if ( is_admin() ) { |
| 56 |
// Admin context. |
| 57 |
$context_data['currentScreen'] = $this->get_current_screen_info(); |
| 58 |
$context_data['currentPostId'] = $this->get_current_post_id(); |
| 59 |
} else { |
| 60 |
// Frontend context. |
| 61 |
$context_data['currentPostId'] = $this->get_frontend_post_id(); |
| 62 |
$context_data['postData'] = $this->get_frontend_post_data(); |
| 63 |
$context_data['contextType'] = $this->get_frontend_context_type(); |
| 64 |
} |
| 65 |
|
| 66 |
wp_localize_script( 'zip-ai-tool-hooks', 'zipAiMcpContext', $context_data ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get current WordPress screen information |
| 71 |
* |
| 72 |
* @since 0.0.1 |
| 73 |
* @return array Screen information |
| 74 |
*/ |
| 75 |
public function get_current_screen_info() { |
| 76 |
$screen = get_current_screen(); |
| 77 |
|
| 78 |
if ( ! $screen ) { |
| 79 |
return array(); |
| 80 |
} |
| 81 |
|
| 82 |
return array( |
| 83 |
'id' => $screen->id, |
| 84 |
'base' => $screen->base, |
| 85 |
'post_type' => $screen->post_type ?? null, |
| 86 |
'action' => $screen->action ?? null, |
| 87 |
'parent_base' => $screen->parent_base ?? null, |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get current post ID if editing a post |
| 93 |
* |
| 94 |
* @since 0.0.1 |
| 95 |
* @return int|null Current post ID or null |
| 96 |
*/ |
| 97 |
public function get_current_post_id() { |
| 98 |
global $post, $pagenow; |
| 99 |
|
| 100 |
// Check if we're on post edit screen. |
| 101 |
if ( in_array( $pagenow, array( 'post.php', 'post-new.php' ), true ) ) { |
| 102 |
// Get post ID from URL parameter. |
| 103 |
if ( isset( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 104 |
return (int) $_GET['post']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 105 |
} |
| 106 |
|
| 107 |
// Get from global $post object. |
| 108 |
if ( $post && isset( $post->ID ) ) { |
| 109 |
return (int) $post->ID; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// Check for block editor (Gutenberg). |
| 114 |
if ( function_exists( 'get_current_screen' ) ) { |
| 115 |
$screen = get_current_screen(); |
| 116 |
if ( $screen && $screen->is_block_editor() && $post && isset( $post->ID ) ) { |
| 117 |
return (int) $post->ID; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return null; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Get current URL (admin or frontend) |
| 126 |
* |
| 127 |
* @since 0.0.1 |
| 128 |
* @return string Current URL |
| 129 |
*/ |
| 130 |
public function get_current_url() { |
| 131 |
$protocol = ( isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ) ? 'https://' : 'http://'; |
| 132 |
$host = isset( $_SERVER['HTTP_HOST'] ) |
| 133 |
? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) |
| 134 |
: 'localhost'; |
| 135 |
$uri = isset( $_SERVER['REQUEST_URI'] ) |
| 136 |
? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) |
| 137 |
: ''; |
| 138 |
|
| 139 |
return $protocol . $host . $uri; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get current post ID on frontend |
| 144 |
* |
| 145 |
* @since 0.0.1 |
| 146 |
* @return int|null Frontend post ID or null |
| 147 |
*/ |
| 148 |
public function get_frontend_post_id() { |
| 149 |
global $post; |
| 150 |
|
| 151 |
// Check if we're on a single post or page. |
| 152 |
if ( is_single() || is_page() ) { |
| 153 |
return get_queried_object_id(); |
| 154 |
} |
| 155 |
|
| 156 |
// Fallback to global post object. |
| 157 |
if ( $post && isset( $post->ID ) ) { |
| 158 |
return (int) $post->ID; |
| 159 |
} |
| 160 |
|
| 161 |
return null; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Get frontend post data |
| 166 |
* |
| 167 |
* @since 0.0.1 |
| 168 |
* @return array|null Frontend post data or null |
| 169 |
*/ |
| 170 |
public function get_frontend_post_data() { |
| 171 |
$post_id = $this->get_frontend_post_id(); |
| 172 |
|
| 173 |
if ( ! $post_id ) { |
| 174 |
return null; |
| 175 |
} |
| 176 |
|
| 177 |
$post = get_post( $post_id ); |
| 178 |
|
| 179 |
if ( ! $post ) { |
| 180 |
return null; |
| 181 |
} |
| 182 |
|
| 183 |
return array( |
| 184 |
'id' => $post->ID, |
| 185 |
'title' => $post->post_title, |
| 186 |
'type' => $post->post_type, |
| 187 |
'status' => $post->post_status, |
| 188 |
'slug' => $post->post_name, |
| 189 |
'url' => get_permalink( $post->ID ), |
| 190 |
'edit_link' => get_edit_post_link( $post->ID ), |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Get frontend context type |
| 196 |
* |
| 197 |
* @since 0.0.1 |
| 198 |
* @return string Frontend context type |
| 199 |
*/ |
| 200 |
public function get_frontend_context_type() { |
| 201 |
if ( is_single() ) { |
| 202 |
return 'frontend_post'; |
| 203 |
} elseif ( is_page() ) { |
| 204 |
return 'frontend_page'; |
| 205 |
} elseif ( is_home() || is_front_page() ) { |
| 206 |
return 'frontend_home'; |
| 207 |
} elseif ( is_archive() ) { |
| 208 |
return 'frontend_archive'; |
| 209 |
} elseif ( is_search() ) { |
| 210 |
return 'frontend_search'; |
| 211 |
} |
| 212 |
|
| 213 |
return 'frontend'; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Get context type based on current WordPress screen |
| 218 |
* |
| 219 |
* @since 0.0.1 |
| 220 |
* @return string Context type |
| 221 |
*/ |
| 222 |
public function get_context_type() { |
| 223 |
$screen = get_current_screen(); |
| 224 |
|
| 225 |
if ( ! $screen ) { |
| 226 |
return 'unknown'; |
| 227 |
} |
| 228 |
|
| 229 |
// Post editor contexts. |
| 230 |
if ( in_array( $screen->base, array( 'post', 'edit' ), true ) ) { |
| 231 |
if ( 'add' === $screen->action ) { |
| 232 |
return 'post_create'; |
| 233 |
} else { |
| 234 |
return 'post_edit'; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
// Other admin contexts. |
| 239 |
switch ( $screen->base ) { |
| 240 |
case 'edit': |
| 241 |
return 'post_list'; |
| 242 |
case 'dashboard': |
| 243 |
return 'dashboard'; |
| 244 |
case 'themes': |
| 245 |
return 'themes'; |
| 246 |
case 'plugins': |
| 247 |
return 'plugins'; |
| 248 |
default: |
| 249 |
return $screen->base ?? 'admin'; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|