-
Notifications
You must be signed in to change notification settings - Fork 0
/
sectioned-content.php
220 lines (175 loc) · 7.17 KB
/
sectioned-content.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
<?php
/*
Plugin Name: Sectioned Content (Wired Media)
Plugin URI:
Description: Enables the adding of new sections for a peice of content
Version: 0.1
Author: Wired Media (carl)
Author URI: http://wiredmedia.co.uk
License: GPLv2
*/
namespace SECTIONED;
define('SECTIONED_OPTION', 'sectioned_content');
require_once dirname(__FILE__) . '/upgrade.php';
require_once dirname(__FILE__) . '/post-types.php';
require_once dirname(__FILE__) . '/api.php';
class Plugin {
public function __construct() {
add_action('admin_init', array(&$this, 'check_php_version'));
add_action('init', array(&$this, 'upgrade_plugin'));
add_action('admin_enqueue_scripts', array(&$this, 'css_and_js'));
// attached to both logged in and non logged in ajax functions
add_action('wp_ajax_sectioned_get_tabs', array(&$this, 'get_tabs'));
add_action('wp_ajax_nopriv_sectioned_get_tabs', array(&$this, 'get_tabs'));
add_filter('tiny_mce_before_init', array(&$this, 'config_mce'), 1111);
add_action('admin_head', array(&$this, 'js_templates'));
add_action('save_post', array(&$this, 'save_post'));
}
public function upgrade_plugin(){
new Upgrade();
}
public function check_php_version(){
if( version_compare(PHP_VERSION, '5.3', '<') ) {
$plugin = plugin_basename( __FILE__ );
if( is_plugin_active($plugin) ) {
deactivate_plugins($plugin);
add_action('admin_notices', function(){
$plugin_data = get_plugin_data( __FILE__, false );
echo '<div class="error">
<p>Sorry <strong>'. $plugin_data['Name'] .'</strong> requires PHP 5.2 or higher! your PHP version is '. PHP_VERSION .'. The plugin was not activated.</p>
</div>';
});
}
}
}
function css_and_js($hook){
global $post;
if( $hook != 'post.php' && $hook != 'post-new.php' ){
return;
}
if( $post->post_type != 'page' ) {
return;
}
wp_register_style( 'sectionedcontent', plugins_url('css/sectioned-content.css', __FILE__) );
wp_enqueue_style( 'sectionedcontent' );
wp_register_script('sectionedcontent', plugins_url('js/sectioned-content.js', __FILE__), array('jquery', 'jquery-ui-tabs'), '', true);
wp_register_script('handlesbars', plugins_url('js/handlebars-1.0.rc.1.js', __FILE__), array('sectionedcontent'), '', true);
wp_enqueue_script('sectionedcontent');
wp_enqueue_script('handlesbars');
wp_localize_script('sectionedcontent', 'sectionedcontent', array('post_id' => $post->ID));
}
function get_tabs(){
$post_id = $_POST['post_id'];
$sections = json_decode(get_post_meta($post_id, '_post_sections', true));
if(is_array($sections)){
foreach($sections as $key => $section){
$post = get_post($section->id);
if(is_object($post)){
$section->content = wpautop($post->post_content);
$fields = get_post_meta($post->ID);
foreach($fields as $key => $value){
if (strstr($key, '_section_')){
$section->fields->{$key} = $value;
}
}
}else{
unset($sections[$key]);
}
}
}else{
$sections = array();
}
echo json_encode($sections);
exit;
}
function js_templates(){
echo '<script id="entry-template" type="text/x-handlebars-template">';
require 'js/templates/wp-editor.html';
echo '</script>';
echo '<script id="field-template" type="text/x-handlebars-template">';
require 'js/templates/field-group.html';
echo '</script>';
echo '<script id="new-field-template" type="text/x-handlebars-template">';
require 'js/templates/new-field.html';
echo '</script>';
}
function save_post($post_id){
// Verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return;
}
// have to do this check to avoid infinite loop!
if(get_post_type($post_id) === 'content_section'){
return;
}
if(get_post_type($post_id) === 'revision'){
return;
}
$sections = json_decode(get_post_meta($post_id, '_post_sections', true));
$updated_sections = array();
foreach($_POST as $key => $section_content){
if(substr($key, 0, 22) == "editor-sectioned-post-"){
$section_id = (int) substr($key, 22);
$section_exists = false;
if(is_array($sections)){
foreach($sections as $key => $value){
if($value->id === $section_id){
$section_exists = true;
unset($sections[$key]);
break;
}
}
}
/*
* make sure the menu order is correct,
* when the sections are outputed we arte using order=ASC
*/
$menu_order = count($updated_sections) + 1;
//check if in sections array
if($section_exists){
// update the post
wp_update_post(array(
'ID' => $section_id,
'post_content' => $section_content,
'menu_order' => $menu_order
));
}else{
// create a new post
$section_id = wp_insert_post(array(
'post_content' => $section_content,
'post_type' => 'content_section',
'post_title' => 'Section for - '. $post_id,
'post_status' => 'publish',
'menu_order' => $menu_order
));
}
// save custom fields
foreach($_POST as $key => $value){
if(strstr($key, '_section_') && strstr($key, '_'. $section_id .'_')){
update_post_meta($section_id, $key, $value);
}
}
// add it to the updated_sections
$updated_sections[] = array('id' => $section_id);
}
}
// now check for deleted sections
// if any values left in $sections array, they all need deleting
if(!empty($sections)){
foreach($sections as $key => $value){
wp_delete_post($value->id, true);
unset($sections[$key]);
}
}
// save post meta
update_post_meta($post_id, '_post_sections', json_encode($updated_sections));
}
function config_mce($a) {
$options = array(
'height' => 463
);
return array_merge($a, $options);
}
}
new Plugin;