forked from tormjens/foundation-columns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoundation-columns.php
370 lines (276 loc) · 8.73 KB
/
foundation-columns.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
<?php
/**
* Foundation Columns WordPress Plugin
*
* @package WordPress
**/
/*
Plugin Name: Foundation Columns
Plugin URI: http://tormorten.no
Description: Use the Zurb Foundation Grid System in WordPress posts and pages
Version: 0.7
Author: Tor Morten Jensen
Author URI: http://tormorten.no
*/
/**
* Copyright (c) 2015 Tor Morten Jensen. All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program 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
* (at your option) any later version.
*
* 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.
* **********************************************************************
*/
if ( ! defined( 'ABSPATH' ) ) {
die( 'Access denied.' );
}
/**
* The name of the plugin
**/
define( 'FCOL_NAME', 'Foundation Columns' );
/**
* The minimum required PHP version
**/
define( 'FCOL_REQUIRED_PHP_VERSION', '5' );
/**
* The minimum required WordPress version
**/
define( 'FCOL_REQUIRED_WP_VERSION', '3.7' );
/**
* Checks if the system requirements are met
*
* @return bool True if system requirements are met, false if not
*/
function foundation_columns_requirements_met() {
global $wp_version;
if ( version_compare( PHP_VERSION, FCOL_REQUIRED_PHP_VERSION, '<' ) ) {
return false;
}
if ( version_compare( $wp_version, FCOL_REQUIRED_WP_VERSION, '<' ) ) {
return false;
}
if ( function_exists('foundation_columns') ) {
return false;
}
return true;
}
if( foundation_columns_requirements_met() ) {
/**
* Localize the plugin
*
* @return void
*/
function foundation_columns_textdomain() {
$domain = 'foundation_columns';
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, WP_LANG_DIR.'/'.$domain.'/'.$domain.'-'.$locale.'.mo' );
load_plugin_textdomain( $domain, FALSE, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
}
add_action( 'plugins_loaded', 'foundation_columns_textdomain' );
/**
* Shortcode function for the main grid system
*
* @param array|string $atts Shortcode attributes
* @param string $content The content inside the shortcode
* @return string Column Formatted
*/
function foundation_columns( $atts, $content = null ) {
$atts = extract( shortcode_atts( array( 'cols' => '' ),$atts ) );
if( $cols ) {
$return = '<div class="'. $cols .' columns">';
$return .= '<p>';
$return .= do_shortcode($content);
$return .= '</p>';
$return .= '</div>';
}
return $return;
}
add_shortcode( 'fc','foundation_columns' );
/**
* Shortcode function for the block grid
*
* @param array|string $atts Shortcode attributes
* @param string $content The content inside the shortcode
* @return string A block grid
*/
function foundation_columns_grid( $atts, $content = null ) {
$atts = extract( shortcode_atts( array( 'cols' => '' ),$atts ) );
if( $cols ) {
$return = '<ul class="'. $cols .'">';
$return .= do_shortcode( $content );
$return .= '</ul>';
}
return $return;
}
add_shortcode( 'fc_grid','foundation_columns_grid' );
/**
* Shortcode function for the block grid items
*
* @param array|string $atts Shortcode attributes
* @param string $content The content inside the shortcode
* @return string A block grid item
*/
function foundation_columns_item( $atts, $content = null ) {
$return = '<li>';
$return .= do_shortcode($content);
$return .= '</li>';
return $return;
}
add_shortcode( 'fc_item','foundation_columns_item' );
/**
* Add a class to posts_class if shortcode is present
*
* @param array $classes An array of classes
* @param string $class The current class
* @param integer $post_id The post ID
* @return array Modified classes
*/
function foundation_columns_posts_class( $classes, $class, $post_id ) {
$post = get_post($post_id);
if( has_shortcode( $post->post_content, 'fc' ) || has_shortcode( $post->post_content, 'fc_grid' ) ) {
$classes[] = 'has-foundation-columns';
}
return $classes;
}
add_filter( 'post_class', 'foundation_columns_posts_class', 1, 3 );
/**
* Filters the content and puts a row around if the [fc]-shortcode is present
*
* @param string $content The posts content
* @return string Modified post content
*/
function foundation_columns_content($content) {
if(strpos($content, 'columns') != false) {
$found = true;
}
if(has_shortcode( $content, 'fc' )) {
$paragraphs = explode("\r\n", $content);
$new_content = '';
foreach($paragraphs as $paragraphs) {
if($paragraphs !== '') {
if(substr($paragraphs, 0, 3) == '[fc')
$new_content .= $paragraphs;
else {
$new_content .= '<div class="small-12 columns"><p>';
$new_content .= $paragraphs;
$new_content .= '</p></div>';
}
}
}
$return = '<div class="row">';
$return .= $new_content;
$return .= '</div>';
$return = str_replace('<p></p>', '', $return);
}
else {
$return = $content;
}
return $return;
}
add_filter('the_content', 'foundation_columns_content', 1);
remove_filter( 'the_content', 'wpautop' ); // move the autop filter
add_filter( 'the_content', 'wpautop' , 99); // amend
add_filter( 'the_content', 'shortcode_unautop',100 ); // remove autop inside shortcodes
/**
* Add buttons to the TinyMCE-editor
*
* @return void
*/
function foundation_columns_buttonhooks() {
if ( ( current_user_can('edit_posts') || current_user_can('edit_pages') ) && get_user_option('rich_editing') ) {
add_filter("mce_external_plugins", "foundation_columns_register_tinymce_javascript");
add_filter('mce_buttons', 'foundation_columns_register_buttons');
}
}
/**
* Registers the buttons
*
* @param array $buttons All buttons
* @return array All buttons
*/
function foundation_columns_register_buttons($buttons) {
array_push($buttons, "separator", "foundation_columns");
return $buttons;
}
/**
* Adds the JavaScript used for the grid system in the editor
*
* @param array $plugin_array An array of tinyMCE plugins
* @return array TinyMCE plugins
*/
function foundation_columns_register_tinymce_javascript($plugin_array) {
$debug = defined('WP_DEBUG') ? WP_DEBUG : false;
$plugin_array['foundation_columns'] = plugins_url('/assets/js/foundation_columns'. ($debug ? '.min' : '') .'.js',__file__);
return $plugin_array;
}
add_action('init', 'foundation_columns_buttonhooks');
/**
* Create an icon for the TinyMCE-menu
*
* @return void
*/
function foundation_columns_icon() {
if( is_admin() ) {
?>
<style type="text/css">
.mce-i-zurb-icon {
background: url('<?php echo plugins_url('/assets/img/foundation_columns_20x20.png',__file__); ?>') no-repeat!important;
}
</style>
<?php
}
}
add_action( 'admin_head', 'foundation_columns_icon', 999 );
/**
* Languages for the dialog
*
* @param array $mce_external_languages Existing translations
* @return array Translation
*/
function foundation_columns_localization( $mce_external_languages ) {
$mce_external_languages[ 'foundation_columns' ] = plugin_dir_path( __FILE__ ) . 'localization.php';
return $mce_external_languages;
}
add_filter( 'mce_external_languages', 'foundation_columns_localization' );
}
else {
add_action( 'admin_notices', 'foundation_columns_error' );
}
/**
* Throw and error upon activation if requirements are not met
*
* @return void An error message
*/
function foundation_columns_error() {
global $wp_version;
?>
<div class="error">
<p><?php echo FCOL_NAME; ?> error: Your environment doesn't meet all of the system requirements listed below.</p>
<ul class="ul-disc">
<li>
<strong>PHP <?php echo FCOL_REQUIRED_PHP_VERSION; ?>+</strong>
<em>(You're running version <?php echo PHP_VERSION; ?>)</em>
</li>
<li>
<strong>WordPress <?php echo FCOL_REQUIRED_WP_VERSION; ?>+</strong>
<em>(You're running version <?php echo esc_html( $wp_version ); ?>)</em>
</li>
</ul>
<p>If you need to upgrade your version of PHP you can ask your hosting company for assistance, and if you need help upgrading WordPress you can refer to <a href="http://codex.wordpress.org/Upgrading_WordPress">the Codex</a>.</p>
<p>You might be getting this error if there is already an instance of the plugin installed.</p>
</div>
<?php
}
?>