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
|
<?php
// Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) {exit; }
class Mediamatic_Category{ public function __construct() { add_action( 'add_attachment', array( $this, 'mediamaticAddAttachmentCategory' ) ); add_action( 'edit_attachment', array( $this, 'mediamaticSetAttachmentCategory' ) ); add_filter( 'ajax_query_attachments_args', array( $this, 'mediamaticQueryAttachmentsArgs' ) ); add_action( 'wp_ajax_save-attachment-compat', array( $this, 'mediamaticSaveAttachmentCompat' ), 0 ); } public function mediamaticAddAttachmentCategory( $postID ) { $mediamaticFolder = isset($_REQUEST["ccFolder"]) ? sanitize_text_field($_REQUEST["ccFolder"]) : null; if(is_null($mediamaticFolder)){ $mediamaticFolder = isset($_REQUEST["cc_mediamatic_folder"]) ? sanitize_text_field($_REQUEST["cc_mediamatic_folder"]) : null; } if($mediamaticFolder !== null){ $mediamaticFolder = (int)$mediamaticFolder; if($mediamaticFolder > 0){ wp_set_object_terms($postID, $mediamaticFolder, MEDIAMATIC_FOLDER, false); } } }
public function mediamaticSetAttachmentCategory($postID) { $taxonomy = apply_filters( 'mediamatic_taxonomy', MEDIAMATIC_FOLDER );
// остановить здесь, если медиа уже прикрепилена к категории if (wp_get_object_terms($postID, $taxonomy)) { return; }
// получить категорию по умолчанию, если меда не прикреплена к какой-либо категории $postCategory = array(get_option( 'default_category' ));
// прикрепить к категории, если категория по умолчанию установлена if($postCategory){ wp_set_post_categories($postID, $postCategory); } } public static function mediamaticGetTermsValues( $keys = 'ids' ) { $mediaTerms = get_terms( MEDIAMATIC_FOLDER, array( 'hide_empty' => 0, 'fields' => 'id=>slug', )); $mediaValues = array(); foreach ( $mediaTerms as $key => $value ){ $mediaValues[] = ( $keys === 'ids' ) ? $key : $value; }
return $mediaValues; } public function mediamaticQueryAttachmentsArgs( $query = array() ) { $taxquery = array(); $taxonomies = get_object_taxonomies( 'attachment', 'names' ); $taxquery = array_intersect_key( $taxquery, array_flip( $taxonomies ) ); $query = array_merge( $query, $taxquery ); $query['tax_query'] = array( 'relation' => 'AND' );
foreach($taxonomies as $taxonomy){ if(isset($query[$taxonomy]) && is_numeric($query[$taxonomy])){ if($query[ $taxonomy ] > 0){ array_push( $query['tax_query'], array( 'taxonomy' => $taxonomy, 'field' => 'id', 'terms' => $query[$taxonomy], 'include_children' => false )); }else{ $allTermsIDs = self::mediamaticGetTermsValues('ids'); array_push( $query[ 'tax_query' ], array( 'taxonomy' => $taxonomy, 'field' => 'id', 'terms' => $allTermsIDs, 'operator' => 'NOT IN', ) ); } } unset($query[$taxonomy]); }
return $query; }
public function mediamaticSaveAttachmentCompat() { // continue if has ID if(!isset($_REQUEST['id'])){ wp_send_json_error(); } // continue if this ID is absolute integer if(!$id = absint($_REQUEST['id'])){ wp_send_json_error(); } // continue if have attachments and their ids if(empty($_REQUEST['attachments']) || empty($_REQUEST['attachments'][$id])){ wp_send_json_error(); } $attachmentData = sanitize_text_field($_REQUEST['attachments'][$id]); if(current_user_can('edit_post', $id)){ check_ajax_referer('update-post_'.$id, 'nonce'); } if(!current_user_can('edit_post', $id)){ wp_send_json_error(); }
$post = get_post($id, ARRAY_A);
if('attachment' != $post['post_type']){ wp_send_json_error(); }
$post = apply_filters('attachment_fields_to_save', $post, $attachmentData);
if(isset($post['errors'])){ $errors = $post['errors']; unset($post['errors']); }
wp_update_post($post);
foreach(get_attachment_taxonomies($post) as $taxonomy){
if(isset($attachmentData[$taxonomy])){ wp_set_object_terms($id, array_map('trim', preg_split('/,+/', $attachmentData[$taxonomy])), $taxonomy, false); }else if(isset($_REQUEST['tax_input']) && isset($_REQUEST['tax_input'][$taxonomy])){ wp_set_object_terms($id, sanitize_text_field($_REQUEST['tax_input'][$taxonomy]), $taxonomy, false); }else{ wp_set_object_terms($id, '', $taxonomy, false); } }
if(!$attachment = wp_prepare_attachment_for_js($id)){ wp_send_json_error(); }
wp_send_json_success($attachment); } }
new Mediamatic_Category();
|