forked from carstingaxion/cbach-wp-gridster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbach-wp-gridster.php
2498 lines (1951 loc) · 102 KB
/
cbach-wp-gridster.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: Gridster
* Plugin URI: https://github.com/carstingaxion/cbach-wp-gridster
* Description: Gridster is a WordPress plugin that makes building intuitive draggable layouts from elements spanning multiple columns. You can even dynamically resize, add and remove elements from the grid, as edit the elements content inline.
* Author: Carsten Bach
* Version: 1.4
* Author URI: http://carsten-bach.de
* Text Domain: cbach-wp-gridster
* License: GPLv2
*
* Copyright 2013
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if( ! class_exists( 'cbach_wpGridster' ) ) {
class cbach_wpGridster {
CONST
PHPNEED = '5.0.4',
WPNEED = '3.3',
NAME = 'Gridster';
/**
* Basename of this file.
*
* @see __construct()
* @type string
*/
protected $base_name = '';
/**
* Current Screen in 'wp-admin'
*
* @see __construct()
* @type object
*/
protected $current_screen = '';
/**
* Plugin-Version
*
* @used when enqueuing scripts & styles
* @type string
*/
protected $version = '1.4';
/**
* gridster.js-Version
*
* @used when enqueuing scripts & styles
* @type string
*/
protected $gridster_version = '0.1.0';
/**
* Use minified js & css or not
*
* @see __construct()
* @type string
*/
protected $minified_js_files = '';
protected $minified_css_files = '';
/**
* Internal prefix for creating CSS classes and ids
*
* @type string
*/
protected $prefix = 'gridster_';
/**
* Nonce
*
* @used for verification on save actions
* @type string
*/
protected $nonce = 'gridster_nonce';
/**
* Post_type name
*
* @type string
*/
private static $cpt_gridster = 'gridster';
/**
* Shortcode name
*
* @type string
*/
private $gridster_shortcode = 'gridster';
/**
* Slug of the options page
*
* @used for whitelisting options, generating menu slug and tabs
*
* @type string
*/
private $default_settings_slug = 'gridster_default_options';
/**
* Settings Identifier
*
* @used as array_key of serialized options
*
* @type string
*/
private static $default_settings_name = 'gridster_default_options';
/**
* Our Tabs on the settings page
*
* @see load_settings()
* @type array
*/
protected $plugin_settings_tabs = array();
/**
* Settings of current gridster post
*
* @see load_post_settings()
* @type array
*/
protected $post_settings = array();
/**
* Load Scripts & styles only when shortcode in use
*
* @type bool
*/
protected $shortcode_used = false;
/**
* Dynamic filter values to get best fitting images inside widget
*
* @type array
*/
protected $thumbnail_filter_dimensions = array();
/**
* Capability which allows users to overwrite the default layout settings for each gridster
*
* @type string
*/
protected $overwrite_caps = 'edit_theme_options';
/**
* Error Handler
*
* @type array
*/
protected $error_msg = array();
/**
* Feature pointer holder
*
* @type mixed
*/
protected $show_feature_pointers = false;
/**
* Construct the CLASS
*
*/
public function __construct() {
// 4 debugging only
# define( 'SCRIPT_DEBUG', true );
// Used by some fn, i.e. add_settings_link() later.
$this->base_name = plugin_basename( __FILE__ );
// wether to use concetenated scripts & styles or the developer versions
$this->minified_js_files = ( defined('SCRIPT_DEBUG') && constant('SCRIPT_DEBUG') ) ? '' : 'min.';
$this->minified_css_files = ( defined('SCRIPT_DEBUG') && constant('SCRIPT_DEBUG') ) ? '' : 'min.';
// get the capability, which allows user to overwrite the default options per each gridster
$this->overwrite_caps = apply_filters( 'gridster_overwrite_post_options_with_cap', $this->overwrite_caps );
// show errors the WP way
add_action( 'admin_notices', array( &$this, 'admin_notices' ) );
// check requirements
if ( $this->check_dependencies() ) {
//Hook up to the init action
add_action( 'init', array( &$this, 'init' ) );
// Register Post_type staging
add_action( 'init', array( &$this, 'gridster_register_as_posttype' ) );
// get settings
add_action( 'init', array( &$this, 'load_settings' ) );
// check if there are any existing gridster posts
add_action( 'init', array( &$this, 'have_gridster_posts' ) );
// set constants and settings after the theme, so users can overwrite them easily
add_action( 'after_setup_theme', array( &$this, 'after_setup_theme' ) );
}
}
/**
* Run during the activation of the plugin
*
* @since 1.0
*
*/
public static function activate() {
// init first plugin options
# update_option( self::$default_settings_name, $this->get_default_settings() );
}
/**
* Run during the deactivation of the plugin
*
* @since
*
*/
public static function deactivate() {
}
/**
* Run during the uninstallation of the plugin
*
* Delete all 'gridster' posts, its post_meta and plugin options
*
* @since 1.0
*
*/
public static function uninstall() {
// important: check if the file is the one that was registered with the uninstall hook (function)
if ( __FILE__ != WP_UNINSTALL_PLUGIN )
return;
// get all gridster posts
$all_gridsters = get_posts( array(
'posts_per_page'=> -1,
'post_type' => self::$cpt_gridster,
'post_status' => 'any'
) );
// delete all gridster-posts and related post metas
foreach( $all_gridsters as $gridster ) {
delete_post_meta( $gridster->ID, '_gridster_layout' );
delete_post_meta( $gridster->ID, '_gridster_query_posts_not_in' );
delete_post_meta( $gridster->ID, '_gridster_dimensions' );
wp_delete_post( $gridster->ID, $force_delete = true );
}
// delete plugin options
delete_option( self::$default_settings_name );
}
/*
* Run during the initialization of Wordpress
*
* @since 1.0
*/
public function init() {
if ( is_admin() ) {
// Setup localization, we need this in 'wp-admin' only
load_plugin_textdomain( 'cbach-wp-gridster', false, dirname( $this->base_name ) . '/languages' );
// Append Stylesheet(s) to WP BackEnd
add_action( 'admin_print_styles', array( &$this, 'admin_css' ) );
// Append JavaScript(s) to WP BackEnd
add_action( 'admin_enqueue_scripts', array( &$this, 'admin_js' ) );
// Add columns to gridster list
add_filter( 'manage_edit-gridster_columns', array( &$this, 'gridster_column_header_function' ) );
// Add content to gridster columns
add_filter( 'manage_gridster_posts_custom_column', array( &$this, 'gridster_populate_rows_function' ), 10, 2 );
// Make new gridster columns sortable
add_filter( 'manage_edit-gridster_sortable_columns', array( &$this, 'gridster_sortable_columns' ) );
// Modify Quick-edit Links
add_filter( 'post_row_actions', array( &$this, 'gridster_post_row_actions' ), 10, 2 );
// Add copy-able shortcode to "publish"-meta_box of post.php and edit.php
add_action( 'post_submitbox_misc_actions', array( &$this, 'add_shortcode_to_publish_metabox' ), 999 );
// Add Settings Page
add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
// whitelist plugin options
add_action( 'admin_init', array( &$this, 'settings_register_general' ) );
// redirect to edit.php to post-new.php, when there are no existing gridster posts
add_action( 'admin_init', array( &$this, 'redirect_edit_to_post_new_when_no_posts_exist' ) );
// get current screen object as early as possible
add_action( 'current_screen', array( &$this, 'current_screen' ) );
// save gridster post_metas
add_action( 'save_post', array( &$this, 'save_post' ) );
// delete gridster post_metas on post deletion
add_action( 'delete_post', array( &$this, 'delete_post' ) );
// update "having-gridster-posts" option after post deletion
add_action( 'after_delete_post', array( &$this, 'after_delete_post' ) );
// show customized "updated" messages
add_filter( 'post_updated_messages', array( &$this, 'post_updated_messages' ) );
// add HTML attribute of "autocomplete='off'" to form element on post.php and edit.php
add_action( 'post_edit_form_tag', array( &$this, 'post_edit_form_tag' ) );
// load TinyMCE Plugin to replace gridster shortcode with graphical zone
add_filter('mce_external_plugins', array( &$this, 'mce_external_plugins' ));
// load TinyMCE CSS to style the graphical shortcode
add_filter('tiny_mce_before_init', array( &$this, 'tiny_mce_before_init' ) );
// add Shortcode-Button to TinyMCE
add_filter('mce_buttons', array( &$this, 'mce_buttons' ) );
// load transaltions for TinyMCE Plugin
add_filter( 'mce_external_languages', array( &$this, 'mce_external_languages' ) );
// modify post_types usable as gridster-widgets
add_filter( 'gridster_post_types_as_widget_blocks', array( &$this, 'filter_gridster_post_types_as_widget_blocks' ) );
// Display a Settings link on the main Plugins page
add_filter( 'plugin_action_links_' . $this->base_name, array( &$this, 'plugin_action_links'), 10 );
// Add additional links to plugin-description-section
add_filter( 'plugin_row_meta', array( &$this, 'plugin_row_meta' ), 10, 2 );
// AJAX callback to get templated HTML by $post->ID as gridster-widegt
add_action( 'wp_ajax_ajax_gridster_get_post', array( &$this, 'ajax_gridster_get_post' ) );
// AJAX callback for TinyMCE modal, initiated by Button
add_action('wp_ajax_ajax_gridster_shortcode_update_modal', array( &$this, 'ajax_gridster_shortcode_update_modal' ) );
// AJAX callback for TinyMCE modal, initiated by Button
add_action('wp_ajax_ajax_get_posts_by_type_widget_block', array( &$this, 'ajax_get_posts_by_type_widget_block' ) );
// AJAX callback for textile rendering of Jeditable "textile" field
# add_action('wp_ajax_ajax_get_textile_markup_for_jeditable', array( &$this, 'ajax_get_textile_markup_for_jeditable' ) );
#add_action( 'admin_enqueue_scripts', array( &$this, 'feature_pointer' ), 1000 );
#add_filter( 'gridster_admin_pointers-post', array( &$this, 'feature_pointers_post' ) );
#add_filter( 'gridster_admin_pointers-post', array( &$this, 'sec_feature_pointers_post' ) );
} else {
// Render gridster Shortcode
add_shortcode( $this->gridster_shortcode, array( &$this, 'shortcode_render_gridster' ) );
// load stylesheets
add_action( 'wp_footer', array( &$this, 'print_css' ) );
// add scripts to frontend
add_action( 'wp_footer', array( &$this, 'print_js' ) );
// add body_class
add_filter( 'body_class', array( &$this, 'body_class' ), 100, 1);
}
// apply image size filter to all AJAX requests
add_filter( 'post_thumbnail_size', array( &$this, 'filter_image_size_on_ajax_request' ) );
}
/**
* Set constants and settings after the theme,
* so users can overwrite them easily
*
* @since 1.2
*
* @todo add check for edit-pages of post_type gridster
*
*/
public function after_setup_theme ( ) {
global $wp_version;
// only in 'wp-admin/post-new.php?post_type=gridster'
// or '/wp-admin/post.php'
# if ( is_admin() && isset( $_GET['post_type'] ) && $_GET['post_type'] == self::$cpt_gridster ) {
// in WP < 3.4 the has_post_thumbnail function is only
// available if theme supports 'post-thumbnails' feature
// to make this Version work with our 'views/gridster-default.php'
// we need to add this feature on the 'wp-admin'
if ( version_compare( $wp_version, '3.4', '<' ) || ! function_exists( 'has_post_thumbnail' ) ) {
add_theme_support( 'post-thumbnails' );
}
# }
// Wether to use frontend CSS or not
if ( !defined( 'GRIDSTER_FRONTEND_CSS' ) )
define( 'GRIDSTER_FRONTEND_CSS', true );
}
/**
* Adds a Stylesheet to the WP BE
*
* @since 1.0
*
*/
public function admin_css () {
$deps = array();
// get plugin base styles, for metaboxes, admin options and so on
wp_register_style( $this->prefix.'admin_css', plugins_url( '/css/gridster_admin.'.$this->minified_css_files.'css', __FILE__ ), $deps, $this->version );
wp_enqueue_style( $this->prefix.'admin_css' );
$deps[] = $this->prefix.'admin_css';
// get styles for our workbench
if ( $this->current_screen->base == 'post' && $this->current_screen->post_type == self::$cpt_gridster ) {
// Jquery UI
wp_register_style( 'jquery-ui-base-css', plugins_url( '/css/jquery-ui/jquery-ui-base.'.$this->minified_css_files.'css', __FILE__ ), $deps, '1.0' );
wp_enqueue_style( 'jquery-ui-base-css' );
$deps[] = 'jquery-ui-base-css';
// Chosen.js styles
wp_register_style( 'chosen-css', plugins_url( '/css/chosen/chosen.'.$this->minified_css_files.'css', __FILE__ ), $deps, '0.9.12' );
wp_enqueue_style( 'chosen-css' );
$deps[] = 'chosen-css';
// gridster.js styles
wp_register_style( $this->prefix.'lib_css', plugins_url( '/css/gridster/jquery.gridster.'.$this->minified_css_files.'css', __FILE__ ), $deps, $this->gridster_version );
wp_enqueue_style( $this->prefix.'lib_css' );
}
/*
// get styles for feature pointers
if ( $this->show_feature_pointers ) {
// Add pointers style to queue.
wp_enqueue_style( 'wp-pointer' );
}
*/
}
/**
* Adds JavaScript to the WP BE
*
* @since 1.0
*
*/
public function admin_js () {
// prepare settings and global vars, so it can be used in JS
$localize = array(
'ajaxNonce' => wp_create_nonce( $this->nonce ),
'ajaxUrl' => admin_url( '/admin-ajax.php' ),
'textMoveHandle' => esc_attr__( 'Move', 'cbach-wp-gridster' ),
'textDelete' => esc_attr__( 'Delete', 'cbach-wp-gridster' ),
'textAjaxLoadProblem' => __( 'There was a problem loading your content, please try again.', 'cbach-wp-gridster' ),
'textAjaxNothingFound' => __( 'Nothing found.', 'cbach-wp-gridster'),
'textMaximumContentWidth' => esc_attr__( 'Maximum content width defined in your current theme by the variable $content_width.', 'cbach-wp-gridster' ),
'JeditableToolTip' => esc_attr__( 'Click to edit', 'cbach-wp-gridster' ),
'JeditableCancel' => esc_attr__( 'Cancel', 'cbach-wp-gridster' ),
'JeditableOk' => esc_attr__( 'OK', 'cbach-wp-gridster' ),
'chosenSelectOptions' => json_encode( $this->default_settings['chosen_select_options'] ),
'chosenSelectPlaceholder' => esc_attr__( 'Choose styles', 'cbach-wp-gridster' ),
'chosenNoResultsText' => esc_attr__( 'No results matched', 'cbach-wp-gridster' ),
'widget_margin_x' => $this->post_settings['dimensions']['widget_margin_x'],
'widget_margin_y' => $this->post_settings['dimensions']['widget_margin_y'],
'widget_base_width' => $this->post_settings['dimensions']['widget_base_width'],
'widget_base_height' => $this->post_settings['dimensions']['widget_base_height'],
# 'extra_rows' => $this->post_settings['dimensions']['extra_rows'],
# 'extra_cols' => $this->post_settings['dimensions']['extra_cols'],
'min_cols' => $this->post_settings['dimensions']['min_cols'],
# 'min_rows' => $this->post_settings['dimensions']['min_rows'],
# 'max_size_x' => $this->post_settings['dimensions']['max_size_x'],
# 'max_size_y' => $this->post_settings['dimensions']['max_size_y'],
);
// Register our workbench Scripts
if ( $this->current_screen->base == 'post' && $this->current_screen->post_type == self::$cpt_gridster ) {
// load depending scripts first
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-ui-draggable' );
wp_enqueue_script( 'jquery-ui-droppable' );
wp_enqueue_script( 'jquery-ui-resizable' );
// default dependencies for loading our script files
$deps = array('jquery', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-resizable' );
// gridster lib
wp_register_script( $this->prefix.'lib_js', plugins_url( '/js/gridster/jquery.gridster.'.$this->minified_js_files.'js', __FILE__ ), $deps, $this->gridster_version );
# wp_register_script( $this->prefix.'lib_js', plugins_url( '/js/gridster/jquery.gridster.with-extras.'.$this->minified_js_files.'js', __FILE__ ), $deps, $this->gridster_version );
# wp_register_script( $this->prefix.'lib_js', plugins_url( '/js/gridster/maxgalbu.jquery.gridster.js', __FILE__ ), $deps, $this->gridster_version );
# wp_register_script( $this->prefix.'lib_js', plugins_url( '/js/gridster/dustmo.jquery.gridster.js', __FILE__ ), $deps, $this->gridster_version );
wp_enqueue_script( $this->prefix.'lib_js' );
$deps[] = $this->prefix.'lib_js';
// jeditable for inline editing of widget content
wp_register_script( 'jquery_jeditable_js', plugins_url( '/js/jeditable/jquery.jeditable.'.$this->minified_js_files.'js', __FILE__ ), $deps, '1.7.1' );
wp_enqueue_script( 'jquery_jeditable_js' );
$deps[] = 'jquery_jeditable_js';
// jeditable autogrow extension
wp_register_script( 'jquery_jeditable_autogrow_extension_js', plugins_url( '/js/jeditable/jquery.jeditable.autogrow.'.$this->minified_js_files.'js', __FILE__ ), $deps, '1.7.1' );
wp_enqueue_script( 'jquery_jeditable_autogrow_extension_js' );
$deps[] = 'jquery_jeditable_autogrow_extension_js';
// autogrow Plugin used for <textarea> of type "autogrow" from jeditable
wp_register_script( 'jquery_autogrow_js', plugins_url( '/js/jeditable/jquery.autogrow.'.$this->minified_js_files.'js', __FILE__ ), $deps, '1.2.2' );
wp_enqueue_script( 'jquery_autogrow_js' );
$deps[] = 'jquery_autogrow_js';
// chosen.js
wp_register_script( 'chosen_js', plugins_url( '/js/chosen/chosen.jquery.'.$this->minified_js_files.'js', __FILE__ ), $deps, '0.9.12' );
wp_enqueue_script( 'chosen_js' );
$deps[] = 'chosen_js';
// plugin base script
wp_register_script( $this->prefix.'admin_js', plugins_url( '/js/gridster_admin.'.$this->minified_js_files.'js', __FILE__ ), $deps, $this->version );
wp_enqueue_script( $this->prefix.'admin_js' );
wp_localize_script( $this->prefix.'admin_js', $this->prefix.'admin', $localize );
}
/*
// load scripts for feature-pointers
if ( $this->show_feature_pointers ) {
// Add pointers script to queue.
wp_register_script( $this->prefix.'feature-pointer', plugins_url( 'js/gridster_feature-pointer.'.$this->minified_js_files.'js', __FILE__ ), array( 'wp-pointer' ), $this->version );
wp_enqueue_script( $this->prefix.'feature-pointer' );
// Add pointer options to script.
wp_localize_script( $this->prefix.'feature-pointer', $this->prefix.'Pointers', $this->show_feature_pointers );
}
*/
}
/**
* Adds JavaScript to the frontend
*
* @since 1.0
*
*/
public function print_js () {
// only when Shortcode is used
if ( $this->shortcode_used !== true )
return;
// prepare settings and global vars, so it can be used in JS
$localize = array(
'layout' => $this->post_settings['layout'],
'widget_margin_x' => $this->post_settings['dimensions']['widget_margin_x'],
'widget_margin_y' => $this->post_settings['dimensions']['widget_margin_y'],
'widget_base_width' => $this->post_settings['dimensions']['widget_base_width'],
'widget_base_height' => $this->post_settings['dimensions']['widget_base_height'],
# 'extra_rows' => $this->post_settings['dimensions']['extra_rows'],
# 'extra_cols' => $this->post_settings['dimensions']['extra_cols'],
'min_cols' => $this->post_settings['dimensions']['min_cols'],
# 'min_rows' => $this->post_settings['dimensions']['min_rows'],
# 'max_size_x' => $this->post_settings['dimensions']['max_size_x'],
# 'max_size_y' => $this->post_settings['dimensions']['max_size_y'],
);
// default dependencies for loading our script files
wp_enqueue_script( 'jquery' );
$deps = array('jquery' );
// gridster lib
wp_register_script( $this->prefix.'lib_js', plugins_url( '/js/gridster/jquery.gridster.'.$this->minified_js_files.'js', __FILE__ ), $deps, $this->gridster_version );
wp_enqueue_script( $this->prefix.'lib_js' );
$deps[] = $this->prefix.'lib_js';
// plugin base script
wp_register_script( $this->prefix.'frontend_js', plugins_url( '/js/gridster_frontend.'.$this->minified_js_files.'js', __FILE__ ), $deps, $this->version );
wp_enqueue_script( $this->prefix.'frontend_js' );
wp_localize_script( $this->prefix.'frontend_js', $this->prefix.'frontend', $localize );
}
/**
* Load CSS for frontend
*
* @since 1.0
*
*/
public function print_css () {
// only when Shortcode is used
// or constant GRIDSTER_FRONTEND_CSS is not falsed
if ( $this->shortcode_used !== true || constant( 'GRIDSTER_FRONTEND_CSS' ) === false )
return;
// default dependencies for loading our style files
$deps = array();
wp_register_style( $this->prefix.'frontend_css', plugins_url( '/css/gridster_frontend.'.$this->minified_css_files.'css', __FILE__ ), $deps, $this->version );
wp_enqueue_style( $this->prefix.'frontend_css' );
}
/***************************************************************************************************************************************************************************************************
*
* CUSTOM POST_TYPES and CUSTOM TAXONOMIES
*
***************************************************************************************************************************************************************************************************/
/**
* Register Custom-Post-Type "gridster"
*
* @since 1.0
*
*/
public function gridster_register_as_posttype() {
// Register post_type
$gridster_labels = array(
'name' => __( 'Gridster', 'cbach-wp-gridster' ),
'singular_name' => __( 'Gridster', 'cbach-wp-gridster' ),
'menu_name' => _x( 'Gridster', 'menu name', 'cbach-wp-gridster' ),
'all_items' => __( 'All Gridster', 'cbach-wp-gridster' ),
'add_new' => _x( 'Add New', 'add new gridster', 'cbach-wp-gridster' ),
'add_new_item' => __( 'Add New Gridster', 'cbach-wp-gridster' ),
'edit_item' => __( 'Edit Gridster', 'cbach-wp-gridster' ),
'new_item' => __( 'New Gridster', 'cbach-wp-gridster' ),
'view_item' => __( 'View Gridster', 'cbach-wp-gridster' ),
'search_items' => __( 'Search Gridster', 'cbach-wp-gridster' ),
'not_found' => __( 'No Gridster found', 'cbach-wp-gridster' ),
'not_found_in_trash' => __( 'No Gridster found in Trash', 'cbach-wp-gridster' ),
'parent_item_colon' => __( 'Parent Gridster:', 'cbach-wp-gridster' ),
);
$gridster_args = array(
'labels' => $gridster_labels,
'description' => __( 'Content arranged within a grid, powered by gridster.js', 'cbach-wp-gridster' ),
'public' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 20,
#'menu_icon' => plugins_url( '', __FILE__ ),
#'capability_type' => 'post',
#'capabilities' => array(),
#'map_meta_cap' => false,
'hierarchical' => false,
# 'supports' => array( 'title', 'revisions', 'author' ),
'supports' => array( 'title', 'author' ),
'register_meta_box_cb' => array( &$this, 'add_meta_boxes' ),
#'taxonomies' => array(),
'has_archive' => false,
#'permalink_epmask' => '',
'rewrite' => false,
'query_var' => true,
'can_export' => true,
);
register_post_type( self::$cpt_gridster, $gridster_args );
}
/**
* Use custom "updated messages"
* labeled for Gridsters and without theese anoying Preview-Links
*
* @since 1.0
*
* @used from function save_post() and added dynamically
*
* @see http://wordpress.stackexchange.com/a/29254
*
* @param array updated messages for all post_types
*
* @return array new updated messages for all post_types, including gridster
*
*/
public function post_updated_messages ( $messages ) {
global $post, $post_ID;
$messages[self::$cpt_gridster] = array(
0 => '', // Unused. Messages start at index 1.
1 => _x( 'Gridster updated.', 'post_updated message', 'cbach-wp-gridster' ),
2 => __( 'Custom field updated.', 'cbach-wp-gridster' ),
3 => __( 'Custom field deleted.', 'cbach-wp-gridster' ),
4 => _x( 'Gridster updated.', 'post_updated message', 'cbach-wp-gridster' ),
5 => isset($_GET['revision']) ? sprintf( __( 'Gridster restored to revision from %s', 'cbach-wp-gridster' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => _x( 'Gridster published.', 'post_updated message', 'cbach-wp-gridster' ),
7 => __( 'Gridster saved.', 'cbach-wp-gridster'),
8 => _x( 'Gridster submitted.', 'post_updated message', 'cbach-wp-gridster' ),
9 => sprintf( _x( 'Gridster scheduled for: <strong>%1$s</strong>.', 'post_updated message', 'cbach-wp-gridster'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ) ),
10 => _x( 'Gridster draft updated.', 'post_updated message', 'cbach-wp-gridster' ),
);
return $messages;
}
/**
* Redirects edit.php to post-new.php if no gridster posts exist
*
* @since 1.1
*
*/
public function redirect_edit_to_post_new_when_no_posts_exist ( ) {
global $pagenow;
// ok, we do have some posts saved yet
if( $this->default_settings['have_gridster_posts'] )
return;
// Check current admin page
if( $pagenow == 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] == self::$cpt_gridster ){
wp_redirect( admin_url('/post-new.php?post_type=' . self::$cpt_gridster, 'http'), 302 );
exit;
}
}
/***************************************************************************************************************************************************************************************************
*
* ADMIN UI
*
***************************************************************************************************************************************************************************************************/
/**
* Add Rows to columns of CPT list
*
* @since 1.0
*
* @params array pre-defined Columns of this post_type
*
* @return array new populated Columns of this post_type
*/
public function gridster_column_header_function( $columns ) {
$columns = $this->array_insert( $columns, array( 'shortcode' => __( 'Shortcode', 'cbach-wp-gridster' ) ), 2 );
return $columns;
}
/**
* Populate new columns with content
*
* @since 1.0
*
* @params string name of current Column
* @params int ID of looped $post
*/
public function gridster_populate_rows_function( $column, $post_id ) {
switch( $column ) {
case 'shortcode' :
echo '<input type="text" class="'.$this->prefix.'shortcode-in-list-table" value="['.$this->gridster_shortcode.' id="'.$post_id.'" title="'.get_the_title($post_id).'"]" readonly="readonly" onfocus="this.select();">';
break;
}
}
/**
* Make these columns sortable
*
* @since 1.0
*
* @return array all sortable columns
*/
public function gridster_sortable_columns() {
return array(
'title' => 'title',
'author' => 'author',
'date' => 'date',
);
}
/**
* Remove "Quick-Edit" and "Preview" Links from post-rows on edit.php
*
* @since 1.3
*
* @param array post row actions
* @param obj $post-object of current post
*
* @return array updated post row actions
*
*/
public function gridster_post_row_actions( $actions, $post ) {
if ( $post->post_type == self::$cpt_gridster ) {
unset( $actions['inline hide-if-no-js'] );
unset( $actions['view'] );
}
return $actions;
}
/**
* Show Shortcode inside the "publish"-meta_box
*
* @since 1.0
*
*/
public function add_shortcode_to_publish_metabox() {
global $post;
if ( self::$cpt_gridster != get_post_type( $post->ID ) )
return;
echo '<div class="misc-pub-section ' . $this->prefix . 'shortcode-copy-section" >'. __('Shortcode').': ';
echo '<input type="text" class="'.$this->prefix.'shortcode-in-list-table" value="['.$this->gridster_shortcode.' id="'.$post->ID.'" title="'.get_the_title($post->ID).'"]" readonly="readonly" onfocus="this.select();">';
echo '</div>';
}
/**
* Init all metaboxes used for gridster post_type
*
* @since 1.0
*
*/
public function add_meta_boxes () {
// add "Workbench" meta box to gridster post_types
add_meta_box(
'gridster_workbench_metabox',
__( 'Gridster', 'cbach-wp-gridster' ),
array( &$this, 'gridster_workbench_meta_box' ),
self::$cpt_gridster,
'normal',
'high'
);
// add "Layout Options" meta box to gridster post_types
// if current user has capabilities to overwrite default settings
if ( current_user_can( $this->overwrite_caps ) ) {
add_meta_box(
'gridster_options_metabox',
__( 'Gridster - Layout options', 'cbach-wp-gridster' ),
array( &$this, 'gridster_options_meta_box' ),
self::$cpt_gridster,
'side',
'default'
);
}
// remove "Slug" metabox 'cause we really don't need it
remove_meta_box( 'slugdiv', self::$cpt_gridster, 'normal' );
// add CSS classes filter for workbench-metabox
add_filter( 'postbox_classes_gridster_gridster_workbench_metabox', array( &$this, 'postbox_classes_post_gridster_workbench_metabox' ) );
}
/**
* Add CSS class to metabox
*
* by default, set this to a 2-columns layout,
* what will be changed by JS on resizing of screen or workbench area
*
* @since 1.0
*
* @param array all CSS classes applied to this metabox
*
* @return array all updated CSS classes
*
*/
function postbox_classes_post_gridster_workbench_metabox( $classes ) {
// In order to ensure we don't duplicate classes, we should
// check to make sure it's not already in the array
if( !in_array( 'two-columns', $classes ) )
$classes[] = 'two-columns';
return $classes;
}
/**
* Render "Gridster Workbench" meta_box content
*
* @since 1.0
*
* @params object current $post
* @params array metaboxes ID, title, callback, and callback-arguments
*
*/
public function gridster_workbench_meta_box ( $post, $meta_box ) {
global $content_width;
// Use nonce for verification
wp_nonce_field( $this->base_name, $this->nonce );
// this stores all our used post->IDs, used as gridster widgets
$query_posts_not_in = get_post_meta( $post->ID, '_gridster_query_posts_not_in', true );
echo '<input type="hidden" id="'.$this->prefix.'query_posts_not_in" name="'.$this->prefix.'query_posts_not_in" value="'.esc_attr($query_posts_not_in).'" />';
// store gridster layout
$gridster_layout = get_post_meta( $post->ID, '_gridster_layout', true );
echo '<input type="hidden" id="'.$this->prefix.'layout" name="'.$this->prefix.'layout" value="'.esc_attr($gridster_layout).'" size="100"/>';
// render gridster work-bench
echo '<div id="'.$this->prefix.'workbench_wrap">';
// this is our gridster workbench
echo '<div id="'.$this->prefix.'workbench" class="gridster"><ul data-content_width="' . $content_width . '">';
echo '</ul></div> <!-- // end div#'.$this->prefix.'workbench -->';
// add loader
echo '<div id="'.$this->prefix.'load-wrap"><div id="'.$this->prefix.'loader">'.
'<p class="howto">' .
'<img class="waiting" src="'. esc_url( admin_url( 'images/wpspin_light.gif' ) ) . '" alt="" />'.
__( 'Your content is being prepared as a gridster widget.', 'cbach-wp-gridster' ) .
'</p>'.
'</div></div>';
// some fallback information for disabled JavaScript
echo '<noscript><div class="error">' .