-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-projects.php
More file actions
656 lines (590 loc) · 25.9 KB
/
json-projects.php
File metadata and controls
656 lines (590 loc) · 25.9 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<?php
/*
Plugin Name: JSON Projects
description: Keep your projects up to date. Add new projects to your website using a pre-default JSON template. This plugin has been built for educational purposes.
Version: 1.0
Author: Alex Chera
Author URI: http://alexchera.com
License: JSON Projects is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
*/
// Enque the CSS in order to stylize the buttons
function add_my_stylesheet()
{
wp_enqueue_style( 'json_import_style', plugins_url( '/css/json_import_style.css', __FILE__ ) );
}
// Enque the JavaScript
function add_my_script()
{
wp_enqueue_script('json_import_script', plugins_url( '/js/json_import_script.js', __FILE__ ), array('jquery') );
}
add_action('admin_print_styles', 'add_my_stylesheet');
add_action('admin_print_scripts', 'add_my_script');
/* ---------------------------------------------- */
//The import button
add_action('admin_head-edit.php', 'importprojectsFromJsonButton');
if (!function_exists('importprojectsFromJsonButton')) {
function importprojectsFromJsonButton()
{
global $current_screen;
if('project' == $current_screen->post_type) {
echo '<form id="form_json" action="'.admin_url('admin-post.php').'" method="post">';
wp_nonce_field('json_button_clicked'); // Wordpress security
echo '<input type="hidden" name="importSingle" />';
echo '<input type="hidden" value="json_button" name="action" />';
if (!file_exists(plugin_dir_path( __FILE__ )."projects.json")) {
echo '<div class="tooltip">';
echo '<input type="submit" value="Import projects" class="page-title-action disabled" disabled>';
echo '<span class="tooltiptext">You need the <strong>projects.json</strong> file of this site</span>';
echo '</div>';
} else {
echo '<div class="tooltip">';
echo '<input type="submit" value="Import projects" class="page-title-action">';
echo '<span class="tooltiptext">Will generate projects from <strong>projects.json</strong></span>';
echo '</div>';
}
echo '</form>';
} else {
return;
}
}
}
/* ---------------------------------------------- */
// Import all buttons. This button is dedicated to import all projects from the export-all.json.
if (is_multisite() ) {
add_action('admin_head-edit.php', 'importAllprojectsFromJsonButton');
}
if (!function_exists('importAllprojectsFromJsonButton')) {
function importAllprojectsFromJsonButton()
{
global $current_screen;
if('project' == $current_screen->post_type) {
echo '<form id="formAll_json" action="'.admin_url('admin-post.php').'" method="post">';
wp_nonce_field('jsonAll_button_clicked'); // Wordpress security
echo '<input type="hidden" name="importAll" />';
echo '<input type="hidden" value="jsonAll_button" name="action" />';
if (!file_exists(plugin_dir_path( __FILE__ )."export-all.json")) {
echo '<div class="tooltip">';
echo '<input type="submit" value="Multisite import" class="page-title-action disabled" disabled>';
echo '<span class="tooltiptext">You need the <strong>export-all.json</strong> file of this site</span>';
echo '</div>';
} else {
echo '<div class="tooltip">';
echo '<input type="submit" value="Multisite import" class="page-title-action">';
echo '<span class="tooltiptext">Will generate projects from <strong>export-all.json</strong> file of this site</span>';
echo '</div>';
}
echo '</form>';
} else {
return;
}
}
}
/* ---------------------------------------------- */
// Standalone import button. This button will import custom posts (projects) from a different JSON file
add_action('admin_head-edit.php', 'importOriginalprojectsFromJsonButton');
if (!function_exists('importOriginalprojectsFromJsonButton')) {
function importOriginalprojectsFromJsonButton()
{
global $current_screen;
if('project' == $current_screen->post_type) {
echo '<form id="formOriginal_json" action="'.admin_url('admin-post.php').'" method="post">';
wp_nonce_field('jsonOriginal_button_clicked'); // Wordpress security
echo '<input type="hidden" name="importOriginal" />';
echo '<input type="hidden" value="jsonOriginal_button" name="action" />';
if (!file_exists(plugin_dir_path( __FILE__ )."standalone.json")) {
echo '<div class="tooltip">';
echo '<input type="submit" value="Import from standalone file" class="page-title-action disabled" disabled>';
echo '<span class="tooltiptext"><strong>standalone.json</strong> file missing</span>';
echo '</div>';
} else {
echo '<div class="tooltip">';
echo '<input type="submit" value="Import from standalone file" class="page-title-action">';
echo '<span class="tooltiptext">Will generate projects from <strong>standalone.json</strong> standalone file of this site</span>';
echo '</div>';
}
echo '</form>';
} else {
return;
}
}
}
/* ---------------------------------------------- */
// Export all buttons. This button is dedicated to export all projects from every subsite
if (is_multisite() ) {
add_action('admin_head-edit.php', 'exportAllprojectsToJsonButton');
}
if (!function_exists('exportAllprojectsToJsonButton')) {
function exportAllprojectsToJsonButton() {
echo '<form id="exportAll_json" action="'.admin_url('admin-post.php').'" method="post">';
wp_nonce_field('json_exportAll_button_clicked'); // Wordpress security
echo '<input type="hidden" name="exportAll" />';
echo '<input type="hidden" value="json_exportAll_button" name="action" />';
echo '<input type="submit" value="Multisite export" class="page-title-action" id="exportAllButton">';
echo '</form>';
}
}
/* ---------------------------------------------- */
// Export button. This button will export the existing projects to projects.json file
add_action('admin_head-edit.php', 'exportprojectsToJsonButton');
if (!function_exists('exportprojectsToJsonButton')) {
function exportprojectsToJsonButton() {
global $current_screen;
if('project' == $current_screen->post_type) {
echo '<form id="export_json" action="'.admin_url('admin-post.php').'" method="post">';
wp_nonce_field('json_export_button_clicked'); // Wordpress security
echo '<input type="hidden" name="exportSingle" />';
echo '<input type="hidden" value="json_export_button" name="action" />';
if (!file_exists(plugin_dir_path( __FILE__ )."projects.json")) {
echo '<div class="tooltip">';
echo '<input type="submit" value="Export projects" class="page-title-action disabled" disabled>';
echo '<span class="tooltiptext">You need the <strong>projects.json</strong> file of this site</span>';
echo '</div>';
} else {
echo '<div class="tooltip">';
echo '<input type="submit" value="Export projects" class="page-title-action">';
echo '<span class="tooltiptext">Will exports projects to <strong>projects.json</strong></span>';
echo '</div>';
}
echo '</form>';
} else {
return;
}
}
}
/* ---------------------------------------------- */
// This function shows where the JSON files are located.
add_action('admin_head-edit.php', 'pluginInfo');
if (!function_exists('pluginInfo')) {
function pluginInfo() {
echo '<div id="info">';
echo '<p class="info-location">';
echo 'The JSON files are located at <strong><a href="'.plugin_dir_url( __FILE__ ).$GLOBALS['file_name'].'">'
.plugin_dir_url( __FILE__ ).'
</strong>';
echo '</p>';
echo '</div>';
}
}
/* ---------------------------------------------- */
// if any of the export buttons are triggered, the $file_name variable will have assigned a new value; each value
// representing the name of the JSON file
$protocol = isset($_SERVER["HTTPS"]) ? 'https' : 'http';
$current_url = $protocol.'://'.$_SERVER['HTTP_HOST'];
global $current_url;
if (isset($_POST['exportAll']) || isset($_POST['importAll'])) {
$file_name = 'export-all.json';
} elseif (isset($_POST['exportSingle'])) {
$file_name = 'projects.json';
} else {
$file_name = 'projects.json';
}
global $file_name;
/* ---------------------------------------------- */
// this function is dedicated for multisite export. Stores the projects from each subsite as a multidimensional array
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
/* ---------------------------------------------- */
// retrieve all the projects from every subsite
function get_all_multisite_posts($args){
$args = array(
'post_type' => 'project',
'post_status' => array('publish', 'draft'),
'nopaging' => true,
'meta_input' => [
'project_id' => get_post_meta(get_the_ID(),'project_id', true),
],
);
if(!$args){
return false;
}
$blog_posts = array();
$sites = get_sites();
if($sites){
foreach ($sites as $site) {
switch_to_blog($site->id);
// $posts = get_posts($args);
$query = new WP_Query( $args );
while ($query->have_posts()): $query->the_post();
$posts[] = array(
'project_id' => get_post_meta( get_the_ID(), 'project_id', true),
'publish_state' => get_post_status( get_the_ID()),
'date' => get_post_datetime( get_the_ID() ),
'title' => get_the_title(),
'content' => get_the_content(),
'image' => get_the_post_thumbnail_url( get_the_ID()),
'module' => get_post_meta(get_the_ID(),'module', true),
'skill' => get_post_meta( get_the_ID(), 'skill', true),
'software' => get_post_meta( get_the_ID(), 'software', true),
// any extra field you might need
);
$posts = unique_multidim_array($posts, 'project_id');
endwhile;
// $blog_posts[$site->blogname] = $posts;
}
}
restore_current_blog();
if($posts) {
return $posts;
}
// if($blog_posts){
// return $blog_posts;
// }
}
/* ----------------------------------- */
$terms = get_terms('module');
foreach ( $terms as $term ) {
echo $term->slug.' ';
}
// retrieve all the projects
function get_all_singlesite_posts($args) {
$args = array(
'post_type' => 'project',
'post_status' => array('publish', 'draft'),
'nopaging' => true
);
if(!$args){
return false;
}
$query = new WP_Query( $args );
$posts = array();
while ($query->have_posts()): $query->the_post();
$posts[] = array(
'project_id' => get_post_meta( get_the_ID(), 'project_id', true),
'publish_state' => get_post_status( get_the_ID()),
'date' => get_post_datetime( get_the_ID() ),
'title' => get_the_title(),
'content' => get_the_content(),
'image' => get_the_post_thumbnail_url( get_the_ID()),
'module' => get_post_meta(get_the_ID(),'module', true),
'skill' => get_post_meta( get_the_ID(), 'skill', true),
'software' => get_post_meta( get_the_ID(), 'software', true),
// any extra field you might need
);
endwhile;
if($posts) {
return $posts;
}
}
global $posts;
// writes and saves the info gathered from get_all_singlesite_posts or get_all_multisite_posts into a JSON file
function export_projects_to_json() {
if (isset($_POST['exportAll']) && check_admin_referer('json_exportAll_button_clicked')) {
$posts = get_all_multisite_posts($args);
}
elseif (isset($_POST['exportSingle']) && check_admin_referer('json_export_button_clicked')) {
$posts = get_all_singlesite_posts($args);
}
wp_reset_query();
$data = json_encode($posts);
$upload_dir = ABSPATH.'wp-content/plugins/json-projects';
$save_path = $upload_dir.'/'.$GLOBALS['file_name'];
$f = fopen($save_path, "w");
fwrite($f, $data);
fclose($f);
$exportIds = [];
$exportIds[] = $checkId;
$redirectTo = $current_url.'/wp-admin/edit.php?
post_type=project&
json_export=true';
if (count($exportIds) > 0) {
$exports = implode(';', $exportIds);
$redirectTo .= "&json_export_successes=$exports";
}
wp_redirect($redirectTo);
}
if (is_admin()) {
add_action('admin_post_json_export_button', 'export_projects_to_json');
add_action('admin_post_json_exportAll_button', 'export_projects_to_json');
if (isset($_GET['json_export'])) {
if (isset($_GET['json_export_successes'])) {
$successes = explode(';', urldecode($_GET['json_export_successes']));
} else {
$successes = [];
}
if (count($successes) > 0) {
add_action('admin_notices', function () {
global $pagenow;
return json_projects_notice('Projects exported succesfully', 'success');
});
add_action('admin_notices', function () {
return json_projects_notice('Exported projects (JSON file) can be
found at this location:
<strong><a href="'.plugin_dir_url( __FILE__ ).$GLOBALS['file_name'].'">'
.plugin_dir_url( __FILE__ ).$GLOBALS['file_name'].'
</strong>', 'info');
});
}
}
}
/* ---------------------------------------------- */
//shows information / errors
function json_projects_notice($message, $type = 'info') {
?>
<div class="notice notice-<?php _e($type, 'sample-text-domain'); ?> is-dismissible">
<p><?php _e($message, 'sample-text-domain'); ?></p>
</div>
<?php
}
/* ---------------------------------------------- */
// updates / inserts projects to single / multisite
function update_projects_from_json() {
if (isset($_POST['importAll']) && check_admin_referer('jsonAll_button_clicked')) {
$json = wp_remote_get(plugin_dir_url( __FILE__ )."export-all.json");
}
elseif(isset($_POST['importSingle']) && check_admin_referer('json_button_clicked')) {
$json = wp_remote_get(plugin_dir_url( __FILE__ )."projects.json");
}
elseif(isset($_POST['importOriginal']) && check_admin_referer('jsonOriginal_button_clicked')) {
$json = wp_remote_get(plugin_dir_url( __FILE__ )."standalone.json");
}
$body = wp_remote_retrieve_body($json);
$mydecode = json_decode($body, true);
$successIds = [];
$errorIds = [];
$duplicateIds = [];
foreach ($mydecode as $project) {
$title = sanitize_text_field($project['title']);
$image = $project['image'];
$content = $project['content'];
$module = $project['module'];
$skills = $project['skill'];
$software = $project['software'];
$id = $project['project_id'];
$wp_filetype = wp_check_filetype( $image, null );
$attachment_data = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $image ),
'post_content' => '',
'post_status' => 'inherit'
);
if ($project['publish_state'] == 'published') {
$publishState = 'publish';
} elseif ($project['publish_state'] == 'publish') {
$publishState = 'publish';
} else {
$publishState = 'draft';
}
$get_page = new WP_Query([
'post_type' => 'project',
'post_status' => $publishState,
'meta_key' => 'project_id',
'meta_value' => $id,
]);
$pages = $get_page->get_posts();
$pageCount = count($pages);
$get_terms_default_attributes = array (
'taxonomy' => 'category');
// var_dump($get_terms_default_attributes);
$terms = get_terms( array(
'taxonomy' => 'category',
'parent' => 0,
'hide_empty' => false
) );
$category = get_term_by('slug', $terms[0]->slug, 'category');
// Check if already exists
// if no then generate
if ($pageCount === 0) {
$post_data = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => $publishState,
'post_author' => 1,
'post_date' => date('Y-m-d H:i:s'),
'post_type' => 'project',
);
$post_data['tax_input'] = [
'category' => [$category->term_id],
];
$post_data['meta_input'] = [
'project_id' => $id,
'version' => 1,
'module' => $module,
'image' => $image,
'skill' => $skills,
'software' => $software,
'publish_state' => $publishState,
];
$result = wp_insert_post($post_data, true);
$attach_id = wp_insert_attachment( $attachment_data, $image, $result );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $image );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $result, $attach_id );
if ($result === 0 || is_wp_error($result)) {
$errorIds[] = $id;
} else {
$successIds[] = $id;
}
//if exists then update
}
if ($pageCount === 1) {
$post_data['tax_input'] = [
'category' => [$category->term_id],
];
$version = get_post_meta($pages[0]->ID, 'version', true);
$post_data = array();
$post_data['ID'] = $pages[0]->ID;
$post_data['post_title'] = $title;
$post_data['post_content'] = $content;
$post_data['post_status'] = $publishState;
$post_data['meta_input'] = [
'version' => $version++,
'date' => date('Y-m-d H:i:s'),
'module' => $module,
'skill' => $skills,
'software' => $software,
];
$result = wp_update_post($post_data);
if ($result === 0 || is_wp_error($result)) {
$errorIds[] = $id;
} else {
$successIds[] = $id;
}
} else {
$duplicateIds[] = $id;
}
}
$redirectTo = $current_url.'/wp-admin/edit.php?
post_type=project&
project_import=true';
if (count($successIds) > 0) {
$successes = implode(';', $successIds);
$redirectTo .= "&project_import_successes=$successes";
}
if (count($errorIds) > 0) {
$errors = implode(';', $errorIds);
$redirectTo .= "&project_import_errors=$errors";
}
if (count($duplicateIds) > 0) {
$duplicates = implode(';', $duplicateIds);
$redirectTo .= "&project_import_duplicates=$duplicates";
}
wp_redirect($redirectTo);
/* ---------------------------------------------- */
}
if (is_admin()) {
add_action( 'admin_post_json_button', 'update_projects_from_json');
add_action( 'admin_post_jsonAll_button', 'update_projects_from_json');
add_action( 'admin_post_jsonOriginal_button', 'update_projects_from_json');
if (isset($_GET['project_import'])) {
if (isset($_GET['project_import_successes'])) {
$successes = explode(';', urldecode($_GET['project_import_successes']));
} else {
$successes = [];
}
if (isset($_GET['project_import_errors'])) {
$errors = explode(';', urldecode($_GET['project_import_errors']));
} else {
$errors = [];
}
if (isset($_GET['project_import_duplicates'])) {
$duplicates = explode(';', urldecode($_GET['project_import_duplicates']));
} else {
$duplicates = [];
}
if (count($errors) > 0 || count($duplicates) > 0) {
add_action('admin_notices', function () {
return json_projects_notice('Import finished with errors, see below:', 'warning');
});
foreach ($errors as $error) {
add_action('admin_notices', function () use ($error) {
return json_projects_notice("Unable to import project with ID: $error", 'error');
});
}
foreach ($duplicates as $duplicate) {
add_action('admin_notices', function () use ($duplicate) {
return json_projects_notice("Duplicate project ID: $duplicate", 'error');
});
}
} elseif (count($successes) > 0) {
add_action('admin_notices', function () {
return json_projects_notice('Projects imported successfully', 'success');
});
} else {
add_action('admin_notices', function () {
return json_projects_notice('Nothing to import', 'info');
});
}
}
}
/* ---------------------------------------------- */
if (! function_exists('custom_post_type')) {
// Register Custom Post Type
function custom_post_type()
{
$labels = array(
'name' => _x( 'Projects', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Project', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Projects', 'text_domain' ),
'name_admin_bar' => __( 'Project', 'text_domain' ),
'archives' => __( 'Project Archives', 'text_domain' ),
'attributes' => __( 'Project Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Projects', 'text_domain' ),
'add_new_item' => __( 'Add New Project', 'text_domain' ),
'add_new' => __( 'Add Project', 'text_domain' ),
'new_item' => __( 'New Project', 'text_domain' ),
'edit_item' => __( 'Edit Project', 'text_domain' ),
'update_item' => __( 'Update Project', 'text_domain' ),
'view_item' => __( 'View Project', 'text_domain' ),
'view_items' => __( 'View Projects', 'text_domain' ),
'search_items' => __( 'Search Projects', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);
$args = array(
'label' => __( 'Project', 'text_domain' ),
'description' => __( 'Projects From JSON', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
// 'taxonomies' => array( 'module', 'skills', 'software' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-format-aside',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => 'projects',
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
);
register_post_type( 'project', $args );
}
add_action( 'init', 'custom_post_type', 0 );
}
/**
* Removes some menus by page.
*/
function wpdocs_remove_menus(){
remove_menu_page( 'edit.php' ); //Posts
}
add_action( 'admin_init', 'wpdocs_remove_menus' );
?>