-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedsdk-n1ed.php
191 lines (164 loc) · 5.24 KB
/
edsdk-n1ed.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
<?php
/*
Plugin Name: N1ED WYSIWYG editor
Plugin URI: https://n1ed.com
Description: Build modern HTML pages with editor based on CKEditor or TinyMCE
Version: 0.0.1
Requires at least: 5.6
Tested up to: 5.6
Requires PHP: 5.6
Author: Dmitriy Komarov, Ilia Kandaurov
Author URI: https://n1ed.com
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: edsdk-n1ed
Create stunning landing pages, design news and media sites with easy!
Widgets • Bootstrap grid • gallery of templates • custom blocks & templates
File Manager • Image Editor • and other features
*/
/**
* Main plugin file.
* @package edsdk-n1ed
*/
require __DIR__ . '/vendor/autoload.php';
use EdSDK\FlmngrServer\FlmngrServer;
class N1ED
{
private $plugin_url;
public function __construct()
{
add_filter('use_block_editor_for_post_type', '__return_false', 1000);
$this->plugin_url = plugins_url('', __FILE__);
add_action('admin_menu', [$this, 'add_settings_page']);
add_filter('plugin_action_links', [$this, 'add_settings_link'], 10, 2);
add_action(
'admin_enqueue_scripts',
[$this, 'edsdk_enqueue_editor'],
10,
1
);
add_action('rest_api_init', function () {
register_rest_route('edsdk-n1ed/v1', '/flmngr', [
'methods' => 'POST, GET',
'callback' => [$this, 'flmngrProcess'],
'permission_callback' => function () {
return wp_validate_auth_cookie('', 'logged_in');
},
'permission_callback' => '__return_true',
]);
});
add_action('rest_api_init', function () {
register_rest_route('edsdk-n1ed/v1', '/setApiKey', [
'methods' => 'POST',
'callback' => [$this, 'saveApi'],
'permission_callback' => function () {
return wp_validate_auth_cookie('', 'logged_in');
},
]);
});
}
public function logout(WP_REST_Request $request)
{
update_option('n1edApiKey', 'N1WPDFLT');
update_option('n1edToken', '');
return true;
}
public function tiny_mce_plugins()
{
return [];
}
public function edsdk_enqueue_editor($hook)
{
global $post;
if ($hook == 'post-new.php' || $hook == 'post.php') {
wp_enqueue_script(
'n1ed',
$this->plugin_url . '/js/n1edLoader.js',
[],
false,
true
);
$apiKey = get_option('n1edApiKey');
if (!$apiKey) {
$apiKey = 'N1WPDFLT';
update_option('n1edApiKey', $apiKey);
}
$defaultUploadDir = '/uploads/2021/02';
wp_localize_script('n1ed', 'n1ed_ajax_object', [
'apiKey' => $apiKey,
'token' => get_option('n1edToken'),
'urlFiles' => wp_upload_dir()['baseurl'],
'defaultUploadDir' => $defaultUploadDir,
]);
}
}
public function flmngrProcess(WP_REST_Request $request)
{
$files = wp_upload_dir()['basedir'];
$tmp = get_temp_dir();
$cache = plugin_dir_path(__FILE__) . '/flmngr-cache/';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['data'])) {
$_POST['data'] = stripslashes($_POST['data']);
}
FlmngrServer::flmngrRequest([
'dirFiles' => $files,
'dirTmp' => $tmp,
'dirCache' => $cache,
]);
}
public function saveApi(WP_REST_Request $request)
{
update_option('n1edApiKey', $_REQUEST['n1edApiKey']);
update_option('n1edToken', $_REQUEST['n1edToken']);
return true;
}
public function getApi(WP_REST_Request $request)
{
$apiKey = get_option('n1edApiKey');
if (!$apiKey) {
$apiKey = 'N1WPDFLT';
update_option('n1edApiKey', $apiKey);
}
return json_encode([
'apiKey' => $apiKey,
'token' => get_option('n1edToken'),
]);
}
public function add_settings_page()
{
add_options_page(
'N1ED Settings',
'N1ED',
'manage_options',
'edsdk-n1ed',
[$this, 'render_plugin_settings_page']
);
}
public function render_plugin_settings_page()
{
if (!defined('N1ED_SETTINGS')) {
define('N1ED_SETTINGS', true);
}
include_once plugin_dir_path(__FILE__) . 'n1ed_settings.php';
}
public function add_settings_link($links, $file)
{
if (
$file === 'n1ed/edsdk-n1ed.php' &&
current_user_can('manage_options')
) {
if (current_filter() === 'plugin_action_links') {
$url = admin_url('options-general.php?page=edsdk-n1ed');
}
// Prevent warnings in PHP 7.0+ when a plugin uses this filter incorrectly.
$links = (array) $links;
$links[] = sprintf(
'<a href="%s">%s</a>',
$url,
__('Settings', 'edsdk-n1ed')
);
}
return $links;
}
}
new N1ED();