forked from jonparis/AstridReminders
-
Notifications
You must be signed in to change notification settings - Fork 2
/
astridcta.php
266 lines (229 loc) · 8.89 KB
/
astridcta.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
<?php
/*
Plugin Name: Call-To-Action Reminders by Astrid
Description: A plugin that makes it easy to create Remind Me links and buttons within posts and Call-to-Action reminders at the bottom of posts
Version: 0.5
Author: Jon Paris ([email protected]) with Chris Lema ([email protected]) and Justin Kussow ([email protected]) and using Custom Meta Box code from others.
License: GPLv2
*/
/* Directories and URLs */
define( 'ACTA_URL', plugin_dir_url(__FILE__) );
define( 'ACTA_DIR', plugin_dir_path(__FILE__) );
class AstridCTA {
static $instance = false;
function __construct() {
add_filter( 'cmb_meta_boxes', array( &$this, 'acta_meta_boxes' ) );
add_action( 'init', array( &$this, 'init_acta' ), 9999 );
add_action( 'wp_print_scripts', array( &$this, 'print_scripts' ) );
add_action( 'cmb_render_acta_actions', array( &$this, 'render_acta_actions' ), 10, 2 );
add_action( 'cmb_render_acta_button', array( &$this, 'render_acta_button' ), 10, 2 );
add_action( 'cmd_validate_acta_action', array( &$this, 'validate_acta_action' ), 10, 3 );
add_action( 'the_content', array( &$this, 'acta_content_footer' ), 100 );
}
function acta_meta_boxes( $meta_boxes ) {
$prefix = 'acta_';
$acta_title = '';
$acta_title .= 'Astrid Reminders: Add Action items from your post ' .
'so readers can get reminded via email, calendar, or to-do list.';
$meta_boxes[] = array(
'id' => 'acta-options',
'title' => $acta_title,
'pages' => array('post'),
'context' => 'normal',
'priority' => 'low',
'show_names' => true,
'fields' => array(
array (
'id' => $prefix . 'actions',
'type' => 'acta_actions',
'name' => 'Actions'
),
array (
'id' => $prefix . 'add_action',
'type' => 'acta_button',
'text' => '✓ Add New Action',
'js_action' => 'return addActaAction();'
)
)
);
return $meta_boxes;
}
function init_acta() {
if ( !class_exists( 'cmb_Meta_Box' ) ) {
require_once( ACTA_DIR . '/metaboxes/init.php' );
}
}
function print_scripts() {
wp_register_script( 'astridcta', ACTA_URL . 'astridcta.js', array( 'jquery' ), '1.0' );
wp_enqueue_script( 'astridcta' );
if ( is_singular() && is_main_query() && get_astrid_cta_option('collect_statistics')){
wp_register_script('astrid_post_loaded', ACTA_URL . 'astrid_post_loaded.js', array('jquery'), '1.0' );
wp_enqueue_script( 'astrid_post_loaded' );
}
wp_register_style( 'astridcta', ACTA_URL . 'astridcta.css' );
wp_enqueue_style( 'astridcta' );
}
function encodeURIComponent($str) {
$revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
return strtr(rawurlencode($str), $revert);
}
function render_acta_actions( $field, $meta ) {
echo '<ul id="' . $field['id'] . '" name="' . $field['id'] . '">';
if ( $meta && is_array( $meta ) ) {
foreach ( $meta as $val ) {
echo ('<script>addActaAction("'.
self::encodeURIComponent($val['text']).'","'.
self::encodeURIComponent($val['notes']).'","'.
intval($val['reminder_days']).
'");</script>');
}
} else {
echo '<span id="acta_no_actions">Add your first action.</span>';
}
echo '</ul>';
}
function tags_to_todos ($tag) {
$content = get_the_content();
$DOM = new DOMDocument;
$DOM->LoadHTML($content);
$items = $DOM.getElementsByTagName('h1');
/* post all h1 elements, now you can do the same with getElementsByID to get the id's with that you expect. */
for ($i = 0; $i < $items->length; $i++) {
echo ('<script>addActaAction("'.
self::encodeURIComponent($items->item($i)->nodeValue).'","notes",2);</script>');
}
}
function render_acta_button( $field, $meta ) {
echo '<a name="' . $field['id'] . '" id="' .
$field['id'] . '" class="button" onclick="' . $field['js_action'] . '">' . $field['text'] . '</a>';
echo $field['hidden_field'];
}
function validate_acta_action( $new, $post_id, $fields ) {
return $new;
}
function acta_content_footer( $content ) {
global $post;
if ( is_singular() && is_main_query() ) {
$actions = get_post_meta( $post->ID, 'acta_actions', true );
if ( $actions && is_array( $actions ) && count( $actions ) > 0 ) {
$cta_section = '<div id="acta_actions_fe">';
$cta_section .= '<h2>' . get_astrid_cta_option('header') . '</h2>';
$cta_section .= '<p>' . get_astrid_cta_option('description') .'</p>';
$cta_section .= '<ul>';
$siteurl = get_site_url();
$step = 0;
$cta_list = '';
foreach( $actions as $action ) {
$step += 1;
if (!$action['text'])
continue;
$cta_list .= '<li id="acta_action_fe_' . $step . '" class="acta_action_fe">';
$cta_list .= '<a class= "astrid-reminder-link" href="http://astrid.com/tasks/remind_me?title=' . self::encodeURIComponent($action['text']);
$cta_list .= '&due_in_days=' . $action['reminder_days'];
$cta_list .= '¬es='.self::encodeURIComponent($action['notes']);
$cta_list .= '&source_name='.get_the_title();
$cta_list .= '&source_url='.post_permalink();
$cta_list .= '" target="_blank"><span class="a-chk-span">✓</span> ✓ ' . $action['text'] . '</a>';
$cta_list .= '</li>';
}
$cta_section .= $cta_list;
$cta_section .= '</ul>';
$cta_section .= '</div>';
if ($cta_list)
$content .= $cta_section;
}
}
return $content;
}
public static function get_instance() {
if ( !self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
}
AstridCTA::get_instance();
/*** Admin Panel ***/
add_action('admin_init', 'astrid_cta_init' );
add_action('admin_menu', 'astrid_cta_add_page');
function get_astrid_cta_option($option) {
$option_default = array(
"header" => "Don\'t forget!",
"description" => 'Get reminders by email or through <a href="http://astrid.com">Astrid</a>
for iPhone, iPad, or Android.'
);
$options = get_option('astrid_cta');
$option_return = $options[$option] ? $options[$option] : $option_default[$option];
return stripslashes($option_return);
}
function astrid_cta_init(){
register_setting( 'astrid_cta_options', 'astrid_cta', 'astrid_cta_validate' );
}
function astrid_cta_add_page() {
add_options_page('Astrid Calls To Action', 'Astrid Calls-to-Action', 'manage_options', 'astrid_cta', 'astrid_cta_do_page');
}
// Draw menu page
function astrid_cta_do_page() {
?>
<div class="wrap">
<h2>Astrid Calls-To-Action</h2>
<form method="post" action="options.php">
<?php settings_fields('astrid_cta_options'); ?>
</p>
<table class="form-table acta_action_fieid">
<tr valign="top"><th scope="row">Call-To-Action Header</th>
<td><input class="admin_input" name="astrid_cta[header]" type="text" value="<?php echo get_astrid_cta_option('header'); ?>" /></td>
</tr>
<tr valign="top"><th scope="row">Description</th>
<td><textarea class="admin_input" name="astrid_cta[description]" rows="3"><?php echo get_astrid_cta_option('description'); ?></textarea></td>
</tr>
<tr valign="top"><th scope="row">Collect Statistics</th>
<td><input type="checkbox" name="astrid_cta[collect_statistics]"
<?php if (get_astrid_cta_option('collect_statistics')) echo "checked='checked'"; ?>
value="true" /> statistics show the # of people who add reminders complete suggestions.</td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
// Sanitize and validate input. Accepts an array, return a sanitized array.
function astrid_cta_validate($input) {
// Say our second option must be safe text with no HTML tags
$input['header'] = wp_filter_nohtml_kses($input['header']);
$input['description'] = addslashes($input['description']);
$input['collect_statistics'] = ($input['collect_statistics']) ? 1 : 0;
return $input;
}
/*** Add Astrid RM button and style to visual editor to add/preview inline content ***/
function add_astrid_reminder_button() {
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )
return;
if ( get_user_option('rich_editing') == 'true') {
add_filter('mce_external_plugins', 'add_astrid_reminder_tinymce_plugin');
add_filter('mce_buttons', 'register_astrid_reminder_button');
add_filter('mce_css', 'ac_style_for_visual_editor');
}
}
add_action('init', 'add_astrid_reminder_button');
add_shortcode('astridrm', 'addAstridRM');
function ac_style_for_visual_editor($url) {
return plugins_url() . '/AstridReminders/astridcta.css';
}
function register_astrid_reminder_button($buttons) {
array_push($buttons, "|", "astrid_reminder");
return $buttons;
}
function add_astrid_reminder_tinymce_plugin($plugin_array) {
$plugin_array['astrid_reminder'] = plugins_url() . '/AstridReminders/editor_plugin.js';
return $plugin_array;
}
function plugin_get_version() {
$plugin_data = get_plugin_data( __FILE__ );
$plugin_version = $plugin_data['Version'];
return $plugin_version;
}
?>