C:\xampp\htdocs\landing\wp-content\plugins\mediamatic\inc\sidebar.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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
<?php

use Mediamatic\Helper;

class 
Mediamatic_Sidebar {

    public function 
__construct() 
    {
        
add_filter'restrict_manage_posts', array($this'mediamaticRestrictManagePosts'));
        
add_filter'posts_clauses', array($this'mediamaticPostsClauses'), 102);
        
        
add_action'admin_enqueue_scripts', array($this'mediamaticEnqueueStyles' ));                                     // load style files
        
add_action'admin_enqueue_scripts', array($this'mediamaticEnqueueScripts' ));                                    // load js files
        
        
add_action'init', array($this,'mediamaticAddFolderToAttachments' ));                                                // register MEDIAMATIC taxonomy
        
add_action'admin_footer-upload.php', array($this,'mediamaticInitSidebar'));                                        // get interface
        
        
add_action'wp_ajax_mediamaticAjaxAddCategory', array($this,'mediamaticAjaxAddCategory'));                            // ajax: add new category
        
add_action'wp_ajax_mediamaticAjaxDeleteCategory', array($this,'mediamaticAjaxDeleteCategory'));                    // ajax: delete existing category
        
add_action'wp_ajax_mediamaticAjaxClearCategory', array($this,'mediamaticAjaxClearCategory'));                        // ajax: delete existing category
        
add_action'wp_ajax_mediamaticAjaxRenameCategory', array($this,'mediamaticAjaxRenameCategory'));                    // ajax: rename existing category
        
        
add_action'wp_ajax_mediamaticAjaxUpdateSidebarWidth', array($this,'mediamaticAjaxUpdateSidebarWidth'));            // ajax: update sidebar width
        
        
add_action'wp_ajax_mediamaticAjaxMoveMultipleMedia', array($this,'mediamaticAjaxMoveMultipleMedia'));                // ajax: move multiple media
        
add_action'wp_ajax_mediamaticAjaxGetTermsByMedia', array($this,'mediamaticAjaxGetTermsByMedia'));                    // ajax: get terms by media for single media
        
add_action'wp_ajax_mediamaticAjaxMoveSingleMedia', array($this,'mediamaticAjaxMoveSingleMedia'));                    // ajax: move singe media
        
        
add_action'wp_ajax_mediamaticAjaxCheckDeletingMedia', array($this,'mediamaticAjaxCheckDeletingMedia'));            // ajax: check deleting media    
        
        
add_action'wp_ajax_mediamaticAjaxMoveCategory', array($this,'mediamaticAjaxMoveCategory'));                        // move category
        
add_action'wp_ajax_mediamaticAjaxUpdateFolderPosition', array($this,'mediamaticAjaxUpdateFolderPosition' ));        // update folder position
        
        
add_option'mediamatic_sidebar_width'280);                                                                    // add option for sidebar width
        
        
add_filter'pre-upload-ui', array($this'mediamaticPreUploadUserInterface'));                                    // upload uploader category to "Add new" 
        
        
        
if(MEDIAMATIC_PLUGIN_NAME != 'Mediamatic'){
            
add_action'admin_notices', [$this'pro_version_notice'] );
        }
        
//Support Elementor
        
if (defined('ELEMENTOR_VERSION')) {
            
add_action('elementor/editor/after_enqueue_scripts', [$this'mediamaticScripts']);
            
add_action('elementor/editor/after_enqueue_scripts', [$this'mediamaticStyles']);
        }
        
    }
    
    
    public function 
pro_version_notice(){
        global 
$pagenow;
        if ( 
$pagenow == 'upload.php' ) {
             echo 
'<div class="notice notice-warning is-dismissible">
                     <p>'
.esc_html__('Mediamatic PRO has more handy features. You could rename a folder, add subfolders easily, clear folders, and search for folders. It also enables folders panel on the media pop-up window.'MEDIAMATIC_TEXT_DOMAIN).' <a href="https://mediamatic.frenify.com/1/" target="_blank">Mediamatic PRO</a></p>
                 </div>'
;
        }
    }
    
    
    public function 
mediamaticEnqueueStyles(){
        
$this->mediamaticStyles();
    }
    
    
    public function 
mediamaticStyles()
    {
        
wp_enqueue_style'iaoalert'MEDIAMATIC_ASSETS_URL 'css/iaoalert.css', array(), MEDIAMATIC_PLUGIN_NAME'all' );
        
wp_enqueue_style'mediamatic-admin'MEDIAMATIC_ASSETS_URL 'css/core.css', array(), MEDIAMATIC_PLUGIN_NAME'all' );
        
wp_enqueue_style'mediamatic-front'MEDIAMATIC_ASSETS_URL 'css/front.css', array(), MEDIAMATIC_PLUGIN_NAME'all' );
        
wp_enqueue_style'mediamatic-rtl'MEDIAMATIC_ASSETS_URL 'css/rtl.css', array(), MEDIAMATIC_PLUGIN_NAME'all' );
        
        if(
MEDIAMATIC_PLUGIN_NAME == 'Mediamatic'){
            
$custom_css "#mediamatic-attachment-filters{display: none;}";
            
wp_add_inline_style'mediamatic-admin'$custom_css );
        }
        
    }
    

    public function 
mediamaticEnqueueScripts()
    {
        
$this->mediamaticScripts();
    }
    
    public function 
mediamaticScripts()
    {
        
        
$allFilesText        esc_html__('All Files'MEDIAMATIC_TEXT_DOMAIN);
        
$uncategorizedText    esc_html__('Uncategorized'MEDIAMATIC_TEXT_DOMAIN);
        
$taxonomy             apply_filters('mediamatic_taxonomy'MEDIAMATIC_FOLDER);
        
$dropdownOptions     = array(
            
'taxonomy'        => $taxonomy,
            
'hide_empty'      => false,
            
'hierarchical'    => true,
            
'orderby'         => 'name',
            
'show_count'      => true,
            
'walker'          => new Mediamatic_Walker_Category_Mediagridfilter(),
            
'value'           => 'id',
            
'echo'            => false
        
);
        
$attachmentTerms     wp_dropdown_categories$dropdownOptions );
        
$attachmentTerms     preg_replace( array( "/<select([^>]*)>/""/<\/select>/" ), ""$attachmentTerms );
        
        
$script                '';
        
$script                .= '<script type="text/javascript">';
        
$script                .= '/* <![CDATA[ */';
        
$script                .= 'var mediamaticFolders = [{"folderID":"all","folderName":"'$allFilesText .'"}, {"folderID":"-1","folderName":"'$uncategorizedText .'"},' substr($attachmentTerms2) . '];';
        
$script                .= '/* ]]> */';
        
$script                .= '</script>';
         
        echo 
$script;
        
        
        
wp_enqueue_script('jquery-ui-draggable');
        
wp_enqueue_script('jquery-ui-droppable');

        
wp_register_script('iaoalert'MEDIAMATIC_ASSETS_URL 'js/third-party-plugins/iaoalert.js',['jquery'], MEDIAMATIC_PLUGIN_NAMEfalse);
        
wp_register_script('nicescroll'MEDIAMATIC_ASSETS_URL 'js/third-party-plugins/nicescroll.js',['jquery'], MEDIAMATIC_PLUGIN_NAMEfalse);
        
wp_register_script('mediamatic-resizable'MEDIAMATIC_ASSETS_URL 'js/resizable.js',['jquery'], MEDIAMATIC_PLUGIN_NAMEfalse);
        
wp_register_script('mediamatic-core'MEDIAMATIC_ASSETS_URL 'js/core.js',['jquery'], MEDIAMATIC_PLUGIN_NAMEtrue);
        
wp_register_script('mediamatic-filter'MEDIAMATIC_ASSETS_URL 'js/filter.js',['jquery'], MEDIAMATIC_PLUGIN_NAMEfalse);
        
wp_register_script('mediamatic-select-filter'MEDIAMATIC_ASSETS_URL '/js/select-filter.js', ['media-views'], MEDIAMATIC_PLUGIN_NAMEtrue );
        
wp_register_script('mediamatic-upload'MEDIAMATIC_ASSETS_URL 'js/upload.js', ['jquery'], MEDIAMATIC_PLUGIN_NAMEfalse );

        
wp_localize_script(
            
'mediamatic-core',
            
'mediamaticConfig',
            [
                
'plugin'                         => MEDIAMATIC_PLUGIN_NAME,
                
'pluginURL'                     => MEDIAMATIC_URL,
                
'nonce'                         => wp_create_nonce'ajax-nonce' ),
                
'uploadURL'                     => admin_url'upload.php' ),
                
'ajaxUrl'                         => admin_url'admin-ajax.php' ),
                
'moveOneFile'                     => esc_html__'Move 1 file'MEDIAMATIC_TEXT_DOMAIN ),
                
'move'                             => esc_html__'Move'MEDIAMATIC_TEXT_DOMAIN ),
                
'files'                         => esc_html__'files'MEDIAMATIC_TEXT_DOMAIN ),
                
'newFolderText'                 => esc_html__'New Subfolder'MEDIAMATIC_TEXT_DOMAIN ),
                
'clearMediaText'                 => esc_html__'Clear Media'MEDIAMATIC_TEXT_DOMAIN ),
                
'renameText'                     => esc_html__'Rename Folder'MEDIAMATIC_TEXT_DOMAIN ),
                
'deleteText'                     => esc_html__'Delete Folder'MEDIAMATIC_TEXT_DOMAIN ),
                
'clearText'                     => esc_html__'Clear Folder'MEDIAMATIC_TEXT_DOMAIN ),
                
'cancelText'                     => esc_html__'Cancel'MEDIAMATIC_TEXT_DOMAIN ),
                
'confirmText'                     => esc_html__'Confirm'MEDIAMATIC_TEXT_DOMAIN ),
                
'areYouSure'                     => esc_html__'Are you confident?'MEDIAMATIC_TEXT_DOMAIN ),
                
'willBeMovedToUncategorized'    => esc_html__'All media inside this folder gets moved to "Uncategorized" folder.'MEDIAMATIC_TEXT_DOMAIN ),
                
'hasSubFolder'                    => esc_html__'This folder contains subfolders, you should delete the subfolders first!'MEDIAMATIC_TEXT_DOMAIN ),
                
'slugError'                     => esc_html__'Unfortunately, you already have a folder with that name.'MEDIAMATIC_TEXT_DOMAIN ),
                
'enterName'                     => esc_html__'Please, enter your folder name!'MEDIAMATIC_TEXT_DOMAIN ),
                
'item'                             => esc_html__'item'MEDIAMATIC_TEXT_DOMAIN ),
                
'items'                         => esc_html__'items'MEDIAMATIC_TEXT_DOMAIN ),
                
'currentFolder'                 => $this->getCurrentFolder(),
                
'noItemDOM'                     => $this->noItemForListMode(),
                
'mediamaticAllTitle'             => esc_html__('All categories'MEDIAMATIC_TEXT_DOMAIN),
            ]
        );
        
wp_localize_script(
            
'mediamatic-filter',
            
'mediamaticConfig2',
            [
                
'pluginURL'                     => MEDIAMATIC_URL,
                
'ajaxUrl'                         => admin_url'admin-ajax.php' ),
                
'nonce'                         => wp_create_nonce'ajax-nonce' ),
                
'moveOneFile'                     => esc_html__'Move 1 file'MEDIAMATIC_TEXT_DOMAIN ),
                
'move'                             => esc_html__'Move'MEDIAMATIC_TEXT_DOMAIN ),
                
'files'                         => esc_html__'files'MEDIAMATIC_TEXT_DOMAIN ),
            ]
        );
        
        
wp_localize_script(
            
'mediamatic-select-filter',
            
'mediamaticConfig',
            [
                
'mediamaticFolder'                 => MEDIAMATIC_FOLDER,
                
'mediamaticAllTitle'             => esc_html__('All categories'MEDIAMATIC_TEXT_DOMAIN),
                
'uploadURL'                     => admin_url'upload.php' ),
                
'assetsURL'                     => MEDIAMATIC_ASSETS_URL
            
]
        );
        
        
wp_localize_script(
            
'mediamatic-upload',
            
'mediamaticConfig',
            [
                
'nonce'                         => wp_create_nonce('ajax-nonce')
            ]
        );

        
wp_enqueue_script'iaoalert' );
        
wp_enqueue_script'nicescroll' );
        
wp_enqueue_script'mediamatic-resizable' );
        
wp_enqueue_script'mediamatic-core' );
        
wp_enqueue_script'mediamatic-filter' );
        
wp_enqueue_script'mediamatic-select-filter' );
        
wp_enqueue_script'mediamatic-upload' );
        
        
        
    }
    
    public function 
noItemForListMode()
    {
        return 
'<tr class="no-items"><td class="colspanchange" colspan="6">'.esc_html__('No media files found.'MEDIAMATIC_TEXT_DOMAIN).'</td></tr>';
    }
    
    public function 
getCurrentFolder()
    {
        if(isset(
$_GET['cc_mediamatic_folder'])){
            return 
sanitize_text_field($_GET['cc_mediamatic_folder']);
        }
        return 
'';
    }
    
    public function 
mediamaticRestrictManagePosts()
    {
        
$scr     get_current_screen();
        if(
$scr->base !== 'upload'){
            return;
        }
        echo 
'<select id="mediao-attachment-filters" class="wpmediacategory-filter attachment-filters" name="cc_mediamatic_folder"></select>';
    }

    public function 
getSidebarWidth()
    {
        
$sidebarWidth         = (int) get_option('mediamatic_sidebar_width'380);
        if(
$sidebarWidth 250 || $sidebarWidth 750){
            
$sidebarWidth     380;
        }
        return 
$sidebarWidth;
    }

    public function 
mediamaticInitSidebar()
    {
        
$output          '';
        
$helper             = new Helper;
        
$sidebarWidth     $this->getSidebarWidth().'px;';
        
        
$output .= '<div class="cc_mediamatic_temporary">';
            
$output .= '<div id="mediamatic_sidebar" class="cc_mediamatic_sidebar" style="width:'.$sidebarWidth.'">';
                
$output .= '<div class="cc_mediamatic_sidebar_in" style="width:'.$sidebarWidth.'">';
                    
$output .= $helper->getSidebarHeader();
                    
$output .= $helper->getSidebarContent();
                    
$output .= '<input type="hidden" id="mediamatic_hidden_terms">';
                
$output .= '</div>';
            
$output .= '</div>';
            
$output .= $this->splitter();
        
$output .= '</div>';
        
        
        echo 
$output;
    }
    
    public function 
splitter()
    {
        if(
MEDIAMATIC_PLUGIN_NAME == 'Mediamatic'){
            
$html '<div class="mediamatic_splitter active">
                    <span class="splitter_holder">
                        <span class="splitter_a"></span>
                        <span class="splitter_b"></span>
                        <span class="splitter_c"></span>
                    </span>
                </div>'
;
        }else{
            
$html '<div class="mediamatic_splitter"></div>';
        }
        return 
$html;
    }
    
    public function 
mediamaticPreUploadUserInterface() 
    {
        
$helper              = new Helper;
        
$terms              $helper->mediamaticTermTreeArray(MEDIAMATIC_FOLDER0);
        
$otherOptions     $helper->mediamaticTermTreeOption($terms);
        
$text              esc_html__("New files go to chosen category"MEDIAMATIC_TEXT_DOMAIN);
        
$output            '';
        
        
// top section
        
$output        .= '<p class="cc_upload_paragraph attachments-category">';
            
$output        .= $text;
        
$output        .= '</p>';
        
        
// select section
        
$output        .= '<p class="cc_upload_paragraph">';
            
$output        .= '<select name="ccFolder" class="mediamatic-editcategory-filter">';
                
$output        .= '<option value="-1">1.'.esc_html__('Uncategorized'MEDIAMATIC_TEXT_DOMAIN).'</option>';
                
$output        .= $otherOptions;
            
$output        .= '</select>';
        
$output        .= '</p>';
        
        
// echo result
        
echo $output;
    }
    
    public function 
mediamaticAjaxAddCategory()
    {
        
$categoryName     sanitize_text_field($_POST["categoryName"]);
        
$parent         sanitize_text_field($_POST["parent"]);
        
        
        
// check category name
        
$name             self::mediamaticCheckMetaName($categoryName$parent);
        
$newTerm         wp_insert_term($nameMEDIAMATIC_FOLDER, array(
            
'name'         => $name,
            
'parent'     => $parent
        
));

        if (
is_wp_error($newTerm)){
            echo 
'error';
        }else{
            
add_term_meta$newTerm["term_id"], 'folder_position'9999 );
            
            
            
$buffyArray = array(
                
'termID'             => $newTerm["term_id"],
                
'termName'             => $name,
            );

            die(
json_encode($buffyArray));
        }
        
    }
    
    public function 
mediamaticAjaxDeleteCategory()
    {
        
$categoryID         sanitize_text_field($_POST["categoryID"]);
        
$selectedTerm         get_term($categoryID MEDIAMATIC_FOLDER );
        
$count                 $selectedTerm->count $selectedTerm->count 0;
        
$deleteTerm            wp_delete_term$categoryIDMEDIAMATIC_FOLDER );
        
        
        if(
is_wp_error($deleteTerm)){
            
$error        'yes';
        }else{
            
$error        'no';
        }
        
$buffyArray     = array(
            
'error'     => $error,
            
'count'     => $count,
        );
        
        die(
json_encode($buffyArray));
        
    }
    
    public function 
mediamaticAjaxClearCategory()
    {
        global 
$wpdb;
        
$categoryID         sanitize_text_field($_POST["categoryID"]);
        
$selectedTerm         get_term($categoryID MEDIAMATIC_FOLDER );
        
$count                 $selectedTerm->count $selectedTerm->count 0;
        
        
$wpdb->query($wpdb->prepare"UPDATE {$wpdb->prefix}term_taxonomy SET count=%d WHERE term_id=%d AND taxonomy=%s"0$categoryIDMEDIAMATIC_FOLDER));
        
$wpdb->query($wpdb->prepare"DELETE FROM {$wpdb->prefix}term_relationships WHERE term_taxonomy_id=%d"$categoryID));
        
        
$buffyArray     = array(
            
'error'     => 'no',
            
'count'     => $count,
        );
        die(
json_encode($buffyArray));
        
    }
    
    public function 
mediamaticAjaxRenameCategory()
    {
        
$categoryID         sanitize_text_field($_POST["categoryID"]);
        
$categoryTitle        sanitize_text_field($_POST["categoryTitle"]);
        
$newSlug            $this->mediamaticSlugGenerator($categoryTitle,$categoryID);
        
$renameCategory        wp_update_term($categoryIDMEDIAMATIC_FOLDER, array(
            
'name'             => $categoryTitle,
            
'slug'             => $newSlug
        
));
        
        if(
is_wp_error($renameCategory)){
            
$error            'yes';
        }else{
            
$error            'no';
        }
        
$buffyArray         = array(
            
'error'         => $error,
            
'title'         => $categoryTitle,
        );
        die(
json_encode($buffyArray));
        
    }
    
    public function 
mediamaticAjaxUpdateSidebarWidth()
    {
        
$width     sanitize_text_field($_POST['width']);
        
$error    'yes';
        
        if(
update_option'mediamatic_sidebar_width'$width )){
            
$error            'no';
        }
        
        
$buffyArray         = array(
            
'error'         => $error,
        );
        die(
json_encode($buffyArray));
        
    }
    
    
    public function 
recursive_sanitize_text_field($array_or_string) {
        if( 
is_string($array_or_string) ){
            
$array_or_string sanitize_text_field($array_or_string);
        }elseif( 
is_array($array_or_string) ){
            foreach ( 
$array_or_string as $key => &$value ) {
                if ( 
is_array$value ) ) {
                    
$value recursive_sanitize_text_field($value);
                }
                else {
                    
$value sanitize_text_field$value );
                }
            }
        }

        return 
$array_or_string;
    }
    
    
    public function 
mediamaticAjaxMoveMultipleMedia()
    {
        
$IDs         $this->recursive_sanitize_text_field($_POST['IDs']);
        
$folderID    sanitize_text_field($_POST['folderID']);
        
$result     = array();

        foreach (
$IDs as $ID){
            
$termList     wp_get_post_termssanitize_text_field($ID), MEDIAMATIC_FOLDER, array( 'fields' => 'ids' ) );
            
$from         = -1;

            if(
count($termList)){
                
$from     $termList[0];
            }

            
$obj         = (object) array('id' => $ID'from' => $from'to' => $folderID);
            
$result[]     = $obj;

            
wp_set_object_terms$IDintval($folderID), MEDIAMATIC_FOLDERfalse );

        }

        
        
$buffyArray         = array(
            
'result'         => $result,
        );
        die(
json_encode($buffyArray));
        
    }
    
    public function 
mediamaticAjaxGetTermsByMedia()
    {
        
$error        'no';
        
$nonce         sanitize_text_field($_POST['nonce']);
        
$terms        = array();
        
        if(!
wp_verify_nonce($nonce'ajax-nonce')){
            
$error     'yes';
        }
        if(!isset(
$_POST['ID'])){
            
$error     'yes';
        }else{
            
$ID        = (int) sanitize_text_field($_POST['ID']);
            
$terms  get_the_terms($IDMEDIAMATIC_FOLDER);
        }
        
        
$buffyArray         = array(
            
'terms'         => $terms,
            
'error'         => $error,
            
'id'             => $ID,
        );
        die(
json_encode($buffyArray));
    }
    
    public function 
mediamaticAjaxMoveSingleMedia()
    {
        
$error                            'no';
        
        if (!isset(
$_POST['mediaID'])){
             
$error                     'yes';
        }else{
            
$mediaID                     absint(sanitize_text_field($_POST['mediaID']));
            
            if(empty(
$_POST['attachments']) || empty($_POST['attachments'][ $mediaID ])){
                 
$error                 'yes';
            }else{
                
$attachment_data         $_POST['attachments'][ $mediaID ];
                
$post                     get_post$mediaIDARRAY_A );
                if(
'attachment' != $post['post_type']){
                    
$error                 'yes';
                }else{
                    
$post                 apply_filters'attachment_fields_to_save'$post$attachment_data );

                    if(isset(
$post['errors'])){
                        
$errors         $post['errors']; 
                        unset( 
$post['errors'] );
                    }

                    
wp_update_post($post);

                    
wp_set_object_terms$mediaIDintval(sanitize_text_field($_POST['folderID'])), MEDIAMATIC_FOLDERfalse );
                    if (!
$attachment     wp_prepare_attachment_for_js($mediaID)){
                        
$error             'yes';
                    }
                }
            }
        }
        
        
        
$buffyArray         = array(
            
'attachment'         => $attachment,
            
'error'             => $error,
        );
        die(
json_encode($buffyArray));
        
    }
    
    
    public function 
mediamaticSlugGenerator($categoryName,$ID)
    {
        global 
$wpdb;
        
$categoryName     strtolower($categoryName);
           
$newSlug        preg_replace('/[^A-Za-z0-9-]+/''-'$categoryName);
        
        
$count             $wpdb->get_var"SELECT COUNT(*) FROM {$wpdb->prefix}terms WHERE slug='".$newSlug."' AND term_id<>".$ID );
        if(
$count 0){
            
$newSlug    $newSlug '1';
            
$newSlug    $this->mediamaticSlugGenerator($newSlug,$ID);
        }
        return 
$newSlug;
    }
    
    public function 
mediamaticAjaxUpdateFolderPosition()
    {
        
$results     sanitize_text_field($_POST["data"]);
        
$results     explode('#'$results);
        foreach (
$results as $result) {
            
$result explode(','$result);
            
update_term_meta($result[0], 'folder_position'$result[1]);
        }
        die();
    }
    
    public function 
mediamaticAjaxMoveCategory()
    {
        
$current         sanitize_text_field($_POST["current"]);
        
$parent         sanitize_text_field($_POST["parent"]);
        
        
        
$checkError     wp_update_term($currentMEDIAMATIC_FOLDER, array(
            
'parent'     => $parent
        
));
                

        if(
is_wp_error($checkError)){
            
$error        'yes';
        }else{
            
$error        'no';
        }
        
$buffyArray     = array(
            
'error'     => $error,
        );
        die(
json_encode($buffyArray));
        
    }
    
    public static function 
mediamaticCheckMetaName($name$parent)
    {
        if(!
$parent){ $parent 0; }
         
        
$terms     get_termsMEDIAMATIC_FOLDER, array('parent' => $parent'hide_empty' => false) );
        
$check     true;

        if(
count($terms)){
            foreach (
$terms as $term){
                if(
$term->name === $name){
                    
$check false;
                    break;
                }
            }
        }else{
            return 
$name;
        }

        
        if(
$check){
            return 
$name;            
        }

        
$arr explode('_'$name);    

        if(
$arr && count($arr) > 1){    
            
$suffix array_values(array_slice($arr, -1))[0];

            
array_pop($arr);

            
$originName implode($arr);

            if(
intval($suffix)){
                
$name $originName '_' . (intval($suffix)+1);
            }

        }else{
            
$name $name '_1';
        }        

        
$name self::mediamaticCheckMetaName($name$parent);

        return 
$name;

    }
    
    public function 
mediamaticAddFolderToAttachments()
    {
        
register_taxonomy(    MEDIAMATIC_FOLDER
             array( 
"attachment" ), 
              array( 
"hierarchical"                 => true
                    
"labels"                    => array(), 
                    
'show_ui'                     => true,
                    
'show_in_menu'                 => false,
                    
'show_in_nav_menus'            => false,
                    
'show_in_quick_edit'        => false,
                    
'update_count_callback'     => '_update_generic_term_count',
                    
'show_admin_column'            => false,
                    
"rewrite"                     => false 
            
)
        );
    }
    
    
    public function 
mediamaticPostsClauses($clauses$query)
    {
        global 
$wpdb;
        
        if (isset(
$_GET['cc_mediamatic_folder'])){
            
            
$folder         sanitize_text_field($_GET['cc_mediamatic_folder']);
            
            if (!empty(
$folder) != ''){
                
$folder     = (int)$folder;
                
$wpdbPrefix    $wpdb->prefix;
                
                if(
$folder 0){
                    
$clauses['where']     .= ' AND ('.$wpdbPrefix.'term_relationships.term_taxonomy_id = '.$folder.')';
                    
$clauses['join']     .= ' LEFT JOIN '.$wpdbPrefix.'term_relationships ON ('.$wpdbPrefix.'posts.ID = '.$wpdbPrefix.'term_relationships.object_id)';
                }else{
                    
                    
$folders get_terms(MEDIAMATIC_FOLDER, array(
                        
'hide_empty' => false
                    
));
                    
$folderIDs = array();
                    foreach (
$folders as $k => $folder) {
                        
$folderIDs[] = $folder->term_id;
                    }
                    
                    
$folderIDs esc_sql($folderIDs);
                    
                    
$extraQuery "SELECT `ID` FROM ".$wpdbPrefix."posts LEFT JOIN ".$wpdbPrefix."term_relationships ON (".$wpdbPrefix."posts.ID = ".$wpdbPrefix."term_relationships.object_id) WHERE (".$wpdbPrefix."term_relationships.term_taxonomy_id IN (".implode(', '$folderIDs)."))";
                    
$clauses['where'] .= " AND (".$wpdbPrefix."posts.ID NOT IN (".$extraQuery."))";
                }
            }
        }
        
        return 
$clauses;
    }
    
    
    
    public function 
mediamaticAjaxCheckDeletingMedia()
    {
        
$attachmentID    '';
        
$error            'no';
        
$terms            = array();
        
$ajaxNonce        sanitize_text_field($_POST['ajaxNonce']);

        if(!
wp_verify_nonce($ajaxNonce,'ajax-nonce' )){
            
$error        'yes';
        }
        
        if(!isset(
$_POST['attachmentID'])){
           
$error        'yes';
        }
        if(
$error == 'no'){
            
$attachmentID    absint(sanitize_text_field($_POST['attachmentID']));
            
$terms          get_the_terms($attachmentIDMEDIAMATIC_FOLDER);
        }
        
        
$buffyArray     = array(
            
'error'     => $error,
            
'terms'     => $terms,
        );
        die(
json_encode($buffyArray));
    }

}
new 
Mediamatic_Sidebar();


// Custom Category Walker
class Mediamatic_Walker_Category_Mediagridfilter extends \Walker_CategoryDropdown 
{
    function 
start_el( &$output$category$depth 0$args = array(), $id )
    {
        
$space                 str_repeat'&nbsp;'$depth );
        
        if(isset(
$category->name)){
            
$folderName        $category->name;
            
$folderID        $category->term_id;
            
$folderName     apply_filters'list_cats'$folderName$category );
            
            
$output .= ',{"folderID":"' $folderID '",';
            
$output .= '"folderName":"' $space $folderName '"}';
            
        }    
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586