-
Notifications
You must be signed in to change notification settings - Fork 7
/
sir-trevor-wp.php
127 lines (104 loc) · 3.55 KB
/
sir-trevor-wp.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
<?php
/**
* @package Sir-Trevor-WP
* @version 1.1.1
*/
/*
Plugin Name: Sir Trevor WP
Author: Nick Eyre
Version: 1.1.1
Author URI: http://nickeyre.com
Description: An intuitive, block-based content editor for Wordpress.
*/
/*
ADMIN PANEL
*/
// Start Sir-Trevor Editor if New Post or if Editing an Existing JSON Post
add_action( 'admin_enqueue_scripts', 'stwp_check_admin_page' );
function stwp_check_admin_page( $hook_suffix ) {
global $pagenow;
if($pagenow == 'post-new.php' && !isset($_GET['stwp_off']))
stwp_activate();
else if($pagenow == 'post.php' && $_GET['action'] == 'edit' && !isset($_GET['stwp_off'])){
$stwp_post = get_post($_GET['post']);
if(json_decode($stwp_post->post_content))
stwp_activate();
}
}
// Start Sir-Trevor Editor (Add Scripts & Register Hooks)
function stwp_activate() {
// Load JS Scripts & Styles
wp_enqueue_script('eventable', plugins_url('lib/eventable.js', __FILE__), array('jquery'));
wp_enqueue_script('sir-trevor', plugins_url('lib/sir-trevor-min.js', __FILE__), array('jquery','underscore','eventable'));
// Load Custom Blocks
$files = scandir(plugin_dir_path(__FILE__).'custom-blocks/');
foreach($files as $id=>$file)
if(strlen($file) > 2)
wp_enqueue_script('sir-trevor-'.$id, plugins_url('custom-blocks/'.$file, __FILE__), array('sir-trevor'));
// Load Remaining Scripts
wp_enqueue_script('sir-trevor-wp', plugins_url('sir-trevor-wp.js', __FILE__), array('jquery','sir-trevor'));
wp_enqueue_style('sir-trevor', plugins_url('lib/sir-trevor.css', __FILE__));
wp_enqueue_style('sir-trevor-icons', plugins_url('lib/sir-trevor-icons.css', __FILE__));
wp_enqueue_style('sir-trevor-wp', plugins_url('sir-trevor-wp.css', __FILE__));
}
/*
AJAX
*/
// Get Nonce for Image Upload
add_action('wp_ajax_stwp_nonce', 'wp_ajax_stwp_nonce');
function wp_ajax_stwp_nonce(){
if(strpos($_SERVER['HTTP_REFERER'], get_site_url()) == 0 && current_user_can('edit_posts'))
echo wp_create_nonce('media-form');
die();
}
// Get URLs of Uploaded Images
add_action('wp_ajax_stwp_imgurl', 'wp_ajax_stwp_imgurl');
function wp_ajax_stwp_imgurl(){
if(strpos($_SERVER['HTTP_REFERER'], get_site_url()) == 0 && current_user_can('edit_posts')){
$imagefull = wp_get_attachment_image_src($_GET['id'], 'full');
$imagedisp = wp_get_attachment_image_src($_GET['id'], 'large');
echo json_encode(array('full'=>$imagefull[0], 'disp'=>$imagedisp[0]));
}
die();
}
/*
FRONT-END RENDERING
*/
// Process
add_filter('the_content', 'stwp_modify_content');
function stwp_modify_content($content){
require_once 'lib_michelf_markdown/Markdown.inc.php';
// Check if Sir Trevor Post
$json = get_post();
$json = json_decode($json->post_content, true);
// Process Sir Trevor Posts
if($json){
$output = '';
foreach($json['data'] as $block){
$template = 'block-templates/'.$block['type'].'.php';
ob_start();
$block = $block['data'];
include $template;
$output .= ob_get_clean();
}
return $output;
}
// Pass Normal Posts Through
else
return $content;
}
/**
* DISABLE TINYMCE
*/
function disable_visual_editor($userID) {
global $wpdb;
$wpdb->query("UPDATE `" . $wpdb->prefix . "usermeta` SET `meta_value` = 'false' WHERE `meta_key` = 'rich_editing'");
}
function enable_visual_editor($userID) {
global $wpdb;
$wpdb->query("UPDATE `" . $wpdb->prefix . "usermeta` SET `meta_value` = 'true' WHERE `meta_key` = 'rich_editing'");
}
add_action('profile_update','disable_visual_editor');
add_action('user_register','disable_visual_editor');
register_activation_hook( __FILE__,'disable_visual_editor');
register_deactivation_hook(__FILE__, 'enable_visual_editor');