-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgambit-gallery.php
233 lines (195 loc) · 6.25 KB
/
gambit-gallery.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
<?php
/*
Plugin Name: Gambit Gallery
Plugin URI: http://www.gambitgallery.com/
Description: Gambit Gallery extends WordPress' image gallery feature with new options that allow you to create beautiful image galleries.
Author: Benjamin Intal, Gambit
Version: 0.1-alpha
Author URI: http://gambit.ph
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
// Used for tracking the version used
defined( 'GG_VERSION' ) or define( 'GG_VERSION', '0.1-alpha' );
// Used for text domains
defined( 'GG_I18NDOMAIN' ) or define( 'GG_I18NDOMAIN', 'gambit-gallery' );
// Used for general naming, e.g. nonces
defined( 'GG_SLUG' ) or define( 'GG_SLUG', 'gambit-gallery' );
// Used for general naming
defined( 'GG_NAME' ) or define( 'GG_NAME', 'Gambit Gallery' );
// Used for file includes
defined( 'GG_PATH' ) or define( 'GG_PATH', trailingslashit( dirname( __FILE__ ) ) );
require_once( GG_PATH . 'class-gg-core.php' );
/**
* Gambit Gallery Plugin Class
*
* @since 0.1-alpha
*/
class GambitGalleryPlugin {
/**
* Constructor, add hooks
*
* @since 0.1-alpha
*/
function __construct() {
add_action( 'plugins_loaded', array( $this, 'loadTextDomain' ) );
add_filter( 'plugin_row_meta', array( $this, 'pluginLinks' ), 10, 2 );
// Override default gallery shortcode output
add_filter( 'post_gallery', array( $this, 'renderGalleryOutput'), 10, 2 );
// Adds our own gallery settings
add_action( 'print_media_templates', array( $this, 'renderGallerySettings' ) );
// Style our gallery settings
add_action( 'admin_enqueue_scripts', array( $this, "loadAdminScripts" ) );
// Enable / disable Gambit Gallery setting
add_action( 'gg_settings_create', array( $this, 'createSettings' ), -100 );
add_filter( 'gg_settings_attrib_defaults', array( $this, 'attributeDefaults' ), -100 );
}
/**
* Load plugin translations
*
* @access public
* @return void
* @since 0.1-alpha
*/
public function loadTextDomain() {
load_plugin_textdomain( GG_I18NDOMAIN, false, basename( dirname( __FILE__ ) ) . '/languages/' );
}
/**
* Adds links to the docs and GitHub
*
* @access public
* @param $plugin_meta array The current array of links
* @param $plugin_file array The plugin file
* @return array The current array of links together with our additions
* @since 0.1-alpha
**/
public function pluginLinks( $plugin_meta, $plugin_file ) {
if ( $plugin_file == plugin_basename( __FILE__ ) ) {
$plugin_meta[] = sprintf( "<a href='%s' target='_blank'>%s</a>",
"http://www.gambitgallery.com/docs",
__( "Documentation", GG_I18NDOMAIN )
);
$plugin_meta[] = sprintf( "<a href='%s' target='_blank'>%s</a>",
"https://github.com/gambitph/Gambit-Gallery",
__( "GitHub Repo", GG_I18NDOMAIN )
);
$plugin_meta[] = sprintf( "<a href='%s' target='_blank'>%s</a>",
"https://github.com/gambitph/Gambit-Gallery/issues",
__( "Issue Tracker", GG_I18NDOMAIN )
);
}
return $plugin_meta;
}
/**
* Loads the admin styles for our settings
*
* @access public
* @return void
* @since 0.1-alpha
*/
public function loadAdminScripts() {
wp_enqueue_style(
GG_SLUG . '-admin',
plugins_url( 'css/admin.css', __FILE__ ),
array(),
GG_VERSION
);
wp_enqueue_script(
GG_SLUG . '-admin',
plugins_url( 'js/admin.js', __FILE__ ),
array( 'jquery' ),
GG_VERSION
);
}
/**
* Renders the Gambit Gallery settings after the normal gallery settings
*
* @access public
* @return void
* @since 0.1-alpha
*/
public function renderGallerySettings() {
// Associative array of attributes and their default values
$attributeDefaults = apply_filters( 'gg_settings_attrib_defaults', array() );
// define your backbone template;
// the "tmpl-" prefix is required,
// and your input field should have a data-setting attribute
// matching the shortcode name
?>
<script type="text/html" id="tmpl-<?php echo GG_SLUG ?>-settings">
<h3 class="gg_heading">
<?php _e( 'Gambit Gallery Settings', GG_I18NDOMAIN ) ?>
</h3>
<?php do_action( 'gg_settings_create' ) ?>
</script>
<script>
jQuery(document).ready(function($){
// add your shortcode attribute and its default value to the
// gallery settings list; $.extend should work as well...
_.extend(wp.media.gallery.defaults,
<?php echo json_encode( $attributeDefaults ) ?>
);
// merge default gallery settings template with yours
wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
template: function(view){
return wp.media.template('gallery-settings')(view)
+ wp.media.template('<?php echo GG_SLUG ?>-settings')(view);
}
});
});
</script>
<?php
}
/**
* Overrides the default gallery shortcode output
*
* @access public
* @param $output string The current rendered shortcode (empty string)
* @param $atts array The list of gallery attributes
* @return string The new gallery shortcode output
* @since 0.1-alpha
*/
public function renderGalleryOutput( $output, $atts ) {
// Check whether to apply gambit gallery stuff
if ( empty( $atts['gambit_gallery'] ) ) {
return $output;
}
if ( strtolower( $atts['gambit_gallery'] ) !== 'enabled' ) {
return $output;
}
$atts = apply_filters( 'gg_gallery_attributes', $atts );
$output = apply_filters( 'gg_gallery_render', $output, $atts );
return apply_filters( 'gg_gallery_output', $output );
}
/**
* Overrides the default gallery shortcode output
*
* @access public
* @param $atts array The list of gallery attributes
* @return string The default values of the attributes
* @since 0.1-alpha
*/
public function attributeDefaults( $atts ) {
return array_merge( $atts, array(
'gambit_gallery' => 'disabled',
) );
}
/**
* Create the disable / enable Gambit Gallery setting
*
* @access public
* @return void
* @since 0.1-alpha
*/
public function createSettings() {
?>
<label class="setting <?php echo GG_SLUG ?>">
<span><?php _e( 'Gambit Gallery', GG_I18NDOMAIN ); ?></span>
<select data-setting="gambit_gallery">
<option value='disabled'><?php _e( 'Disabled', GG_I18NDOMAIN ) ?></option>
<option value='enabled'><?php _e( 'Enabled', GG_I18NDOMAIN ) ?></option>
</select>
</label>
<?php
}
}
new GambitGalleryPlugin();