-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInit.php
212 lines (177 loc) · 7.1 KB
/
Init.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
namespace SeoAudit;
class Init
{
public function init(): void
{
if (is_admin()) {
$this->add_options_page();
add_action('admin_menu', [$this, 'add_menu_links']);
}
$this->setup_hooks();
$this->setup_api_routes();
}
public static function add_settings_link_to_plugin_list($links)
{
$links[] = '<a href="' . self::get_settings_page_url() . '">Settings</a>';
return $links;
}
public static function get_settings_page_url()
{
return esc_url(get_admin_url(null, 'options-general.php?page=' . self::get_settings_page_relative_path()));
}
public static function get_settings_page_relative_path()
{
return 'chat-gpt-seo-settings';
}
public function setup_api_routes(): void
{
add_action('rest_api_init', function () {
register_rest_route('seo-audit/v1', '/audit-item/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'SeoAudit\RestRoutes::audit_item_request',
'args' => array(
'id' => array(
'validate_callback' => function ($param, $request, $key) {
return is_numeric($param);
}
),
),
'permission_callback' => function () {
return current_user_can('edit_others_posts');
}
));
register_rest_route('seo-audit/v1', '/force-audit-item/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'SeoAudit\RestRoutes::force_audit_item_request',
'args' => array(
'id' => array(
'validate_callback' => function ($param, $request, $key) {
return is_numeric($param);
}
),
),
'permission_callback' => function () {
return current_user_can('edit_others_posts');
}
));
register_rest_route('seo-audit/v1', '/clear-audit-data', array(
'methods' => 'GET',
'callback' => 'SeoAudit\RestRoutes::clear_audit_data',
'permission_callback' => function () {
return current_user_can('edit_others_posts');
}
));
register_rest_route('seo-audit/v1', '/update-meta-description/(?P<id>\d+)', array(
'methods' => 'POST',
'callback' => 'SeoAudit\RestRoutes::update_meta_description',
'args' => array(
'id' => array(
'validate_callback' => function ($param, $request, $key) {
return is_numeric($param);
}
),
),
'permission_callback' => function () {
return current_user_can('edit_others_posts');
}
));
register_rest_route('seo-audit/v1', '/generate-meta-description/(?P<id>\d+)', array(
'methods' => 'POST',
'callback' => 'SeoAudit\RestRoutes::generate_meta_description',
'args' => array(
'id' => array(
'validate_callback' => function ($param, $request, $key) {
return is_numeric($param);
}
),
),
'permission_callback' => function () {
return current_user_can('edit_others_posts');
}
));
});
}
public function add_settings_link($links)
{
$settings_link = '<a href="admin.php?page=chat-gpt-seo-logs">' . __('Settings', 'chat-gpt-seo') . '</a>';
array_unshift($links, $settings_link);
return $links;
}
private function setup_hooks(): void
{
if ((isset($_GET['page']) && $_GET['page'] === 'chat-gpt-seo-audit') || (isset($_GET['page']) && $_GET['page'] === 'chat-gpt-keyword-audit')) {
add_action('admin_enqueue_scripts', [$this, 'enqueue_plugin_styles']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_plugin_scripts']);
add_action('admin_enqueue_scripts', [$this, 'my_custom_rest_api_nonce']);
}
}
public function my_custom_rest_api_nonce() {
$nonce = wp_create_nonce('my_custom_nonce_action');
wp_localize_script('chat-gpt-seo-js', 'chatGptSeoNonce', ['chatGptSeoNonce' => $nonce ]);
}
public function enqueue_plugin_styles(): void
{
wp_enqueue_style('chat-gpt-seo-css', CHAT_GPT_SEO_PLUGIN_URL . '/assets/css/main.css');
wp_enqueue_style('chat-gpt-seo-lib-table-styles', CHAT_GPT_SEO_PLUGIN_URL . '/assets/lib/jquery.dataTables.css');
}
public function enqueue_plugin_scripts(): void
{
wp_enqueue_script('chat-gpt-seo-lib-table-js', CHAT_GPT_SEO_PLUGIN_URL . '/assets/lib/jquery.dataTables.js.js', array('jquery'), CHAT_GPT_SEO_VERSION, true);
wp_enqueue_script('chat-gpt-seo-js', CHAT_GPT_SEO_PLUGIN_URL . '/assets/js/main.js', array('jquery', 'chat-gpt-seo-lib-table-js'), CHAT_GPT_SEO_VERSION, true);
}
public function add_menu_links(): void
{
add_menu_page(
'SEO Audit',
'SEO Audit',
'manage_options',
'chat-gpt-seo-audit',
[$this, 'seo_audit'],//'Init\Init::seo_audit',
'dashicons-chart-bar', // You can change the icon
85 // Adjust the position as needed
);
add_submenu_page(
'chat-gpt-seo-audit',
'Keyword Audit',
'Keyword Audit',
'manage_options',
'chat-gpt-keyword-audit',
[$this, 'keyword_audit']
);
}
public function keyword_audit(): void
{
?>
<div class="wrap">
<?php include(CHAT_GPT_SEO_PLUGIN_DIR . "/templates/keywords.php"); ?>
</div>
<?php
}
public function seo_audit(): void
{
?>
<div class="wrap">
<?php include(CHAT_GPT_SEO_PLUGIN_DIR . "/templates/seo.php"); ?>
</div>
<?php
}
private function add_options_page(): void
{
if (function_exists('acf_add_options_page')) {
$settingsPage = array(
'page_title' => 'Settings',
'menu_title' => 'Settings',
'menu_slug' => 'chat-gpt-seo-settings',
'capability' => 'edit_posts',
'redirect' => false,
'parent_slug' => 'chat-gpt-seo-audit',
);
acf_add_options_page($settingsPage);
$field_group = json_decode(file_get_contents(CHAT_GPT_SEO_PLUGIN_DIR . '/acf_json/group_62f0bc7465155.json'), TRUE);
acf_add_local_field_group($field_group);
$field_group = json_decode(file_get_contents(CHAT_GPT_SEO_PLUGIN_DIR . '/acf_json/group_65536c2771900.json'), TRUE);
acf_add_local_field_group($field_group);
}
}
}