PluginProbe
ZIP AI – AI Website Builder & AI Agent (Beta) / 0.0.7
ZIP AI – AI Website Builder & AI Agent (Beta) v0.0.7
0.0.10 0.0.9 trunk 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8
zip-ai / classes / core / context-detector.php

context-detector.php in ZIP AI – AI Website Builder & AI Agent (Beta) 0.0.7, at classes/core/context-detector.php

215 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WordPress Context Detector.
4 *
5 * @package zip-ai
6 */
7
8 namespace ZipAI\MCP\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 1.0.0
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 1.0.0
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', 'zipwpMcpContext', $context_data );
67 }
68
69 /**
70 * Get current WordPress screen information
71 *
72 * @since 1.0.0
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 1.0.0
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 1.0.0
128 * @return string Current URL
129 */
130 public function get_current_url() {
131 $protocol = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https://' : 'http://';
132 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- HTTP_HOST is used only for URL construction.
133 $host = $_SERVER['HTTP_HOST'] ?? 'localhost';
134 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- REQUEST_URI is used only for URL construction.
135 $uri = $_SERVER['REQUEST_URI'] ?? '';
136
137 return $protocol . $host . $uri;
138 }
139
140 /**
141 * Get current post ID on frontend
142 *
143 * @since 1.0.0
144 * @return int|null Frontend post ID or null
145 */
146 public function get_frontend_post_id() {
147 global $post;
148
149 // Check if we're on a single post or page.
150 if ( is_single() || is_page() ) {
151 return get_queried_object_id();
152 }
153
154 // Fallback to global post object.
155 if ( $post && isset( $post->ID ) ) {
156 return (int) $post->ID;
157 }
158
159 return null;
160 }
161
162 /**
163 * Get frontend post data
164 *
165 * @since 1.0.0
166 * @return array|null Frontend post data or null
167 */
168 public function get_frontend_post_data() {
169 $post_id = $this->get_frontend_post_id();
170
171 if ( ! $post_id ) {
172 return null;
173 }
174
175 $post = get_post( $post_id );
176
177 if ( ! $post ) {
178 return null;
179 }
180
181 return array(
182 'id' => $post->ID,
183 'title' => $post->post_title,
184 'type' => $post->post_type,
185 'status' => $post->post_status,
186 'slug' => $post->post_name,
187 'url' => get_permalink( $post->ID ),
188 'edit_link' => get_edit_post_link( $post->ID ),
189 );
190 }
191
192 /**
193 * Get frontend context type
194 *
195 * @since 1.0.0
196 * @return string Frontend context type
197 */
198 public function get_frontend_context_type() {
199 if ( is_single() ) {
200 return 'frontend_post';
201 } elseif ( is_page() ) {
202 return 'frontend_page';
203 } elseif ( is_home() || is_front_page() ) {
204 return 'frontend_home';
205 } elseif ( is_archive() ) {
206 return 'frontend_archive';
207 } elseif ( is_search() ) {
208 return 'frontend_search';
209 }
210
211 return 'frontend';
212 }
213
214 }
215