-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhosted-jft-plugin.php
More file actions
executable file
·221 lines (200 loc) · 7.78 KB
/
hosted-jft-plugin.php
File metadata and controls
executable file
·221 lines (200 loc) · 7.78 KB
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
<?php
/*
Plugin Name: Hosted JFT
Plugin URI: https://wordpress.org/plugins/hosted-jft/
Description: Hosted JFT is a plugin that allows an NA Community to host their own translated version of the JFT.
Version: 1.0.3
Install: Drop this directory into the "wp-content/plugins/" directory and activate it.
*/
/* Disallow direct access to the plugin file */
if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
die('Sorry, but you cannot access this page directly.');
}
require_once('admin/hosted-jft-dashboard.php');
// create admin menu settings page
add_action('admin_menu', 'hosted_jft_options_menu');
function hosted_jft_options_menu()
{
add_options_page('Hosted JFT Plugin Settings', 'Hosted JFT', 'manage_options', 'hosted-jft-plugin', 'hosted_jft_plugin_page');
}
// add settings link to plugins page
function hosted_jft_add_settings_link($links)
{
$settings_link = '<a href="options-general.php?page=hosted-jft-plugin">' . __('Settings') . '</a>';
array_unshift($links, $settings_link);
return $links;
}
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'hosted_jft_add_settings_link');
function hosted_jft_func($atts = [])
{
$args = shortcode_atts(
array(
'timezone' => ''
),
$atts
);
// Set custom_field and timezone - shortcode parameter overrides admin setting
$jft_custom_field = get_option('jft_custom_field');
$jft_timezone = (!empty($args['timezone']) ? sanitize_text_field(strtolower($args['timezone'])) : get_option('jft_timezone'));
date_default_timezone_set($jft_timezone);
$today = date("m-d");
$jft_post = get_posts(array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => $jft_custom_field,
'meta_value' => $today
));
$get_title = get_post_field('post_title', $jft_post[0]->ID ?? '');
$todays_jft_content = get_post_field('post_content', $jft_post[0]->ID ?? '');
if ($todays_jft_content && $todays_jft_content != '[hosted_jft]') {
$todays_jft_title = '<div class="spo-title"><h2 class="spo-title">' . $get_title . '</h2></div>';
$ret = $todays_jft_title . $todays_jft_content;
} else {
$ret = "No JFT Found for today, there could be a problem with settings or missing post for today.";
}
return $ret;
}
function hosted_jft_widget_func($atts = [])
{
$args = shortcode_atts(
array(
'timezone' => ''
),
$atts
);
// Set custom_field and timezone - shortcode parameter overrides admin setting
$jft_custom_field = get_option('jft_custom_field');
$jft_timezone = (!empty($args['timezone']) ? sanitize_text_field(strtolower($args['timezone'])) : get_option('jft_timezone'));
date_default_timezone_set($jft_timezone);
$today = date("m-d");
$jft_post = get_posts(array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => $jft_custom_field,
'meta_value' => $today
));
$todays_jft_array = [];
$todays_jft_array['excerpt'] = get_post_field('post_excerpt', $jft_post[0]->ID);
$todays_jft_array['url'] = get_post_field('guid', $jft_post[0]->ID);
$todays_jft_array['title'] = get_post_field('post_title', $jft_post[0]->ID);
return $todays_jft_array;
}
add_action('pre_get_posts', function ($query) {
global $wp;
$jft_custom_field = get_option('jft_custom_field');
$jft_timezone = get_option('jft_timezone');
if (!is_admin() && $query->is_main_query()) {
if ($wp->request == 'get-jft') {
if ($_GET['tz']) {
date_default_timezone_set($_GET['tz']);
} else {
date_default_timezone_set($jft_timezone);
}
$today = date("m-d");
$jft_post = get_posts(array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => $jft_custom_field,
'meta_value' => $today
));
$todays_jft_array = array();
$todays_jft_array[] = array(
'title' => get_post_field('post_title', $jft_post[0]->ID),
'content' => get_post_field('post_content', $jft_post[0]->ID),
'excerpt' => get_post_field('post_excerpt', $jft_post[0]->ID),
'url' => get_post_field('guid', $jft_post[0]->ID),
);
header('Content-Type: application/json');
echo json_encode($todays_jft_array);
exit;
}
}
});
// create [hosted_jft] shortcode
add_shortcode('hosted_jft', 'hosted_jft_func');
/** START Hosted JFT Widget **/
// register Hosted_JFT_Widget
add_action('widgets_init', function () {
register_widget('Hosted_JFT_Widget');
});
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
// phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
class Hosted_JFT_Widget extends WP_Widget
{
// phpcs:enable PSR1.Classes.ClassDeclaration.MissingNamespace
// phpcs:enable Squiz.Classes.ValidClassName.NotCamelCaps
/**
* Sets up a new Hosted JFT widget instance.
*
*/
public function __construct()
{
$widget_ops = array(
'classname' => 'Hosted_JFT_widget',
'description' => 'Displays the Just For Today',
);
parent::__construct('Hosted_JFT_widget', 'Hosted JFT', $widget_ops);
}
/**
* Outputs the content for the current Hosted JFT widget instance.
*
*
* @hosted_jft_widget_func gets and parses the jft
*
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance Settings for the current Area Meetings Dropdown widget instance.
*/
public function widget($args, $instance)
{
echo $args['before_widget'];
if (! empty($instance['title'])) {
echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
}
$jft_page_url = get_option('jft_page_url');
$jft_more_text = get_option('jft_more_text');
$get_jft = hosted_jft_widget_func($atts);
echo '<div class="jft-widget-title">' . $get_jft['title'] . '</div><br/>';
echo '<div class="jft-widget-excerpt">' . $get_jft['excerpt'] . '</div>';
echo ' <a href="'.$jft_page_url.'" class="jft-widget-link">'.$jft_more_text.'</a><br/><br/>';
echo $args['after_widget'];
}
/**
* Outputs the settings form for the Hosted JFT widget.
* @param $instance
*/
public function form($instance)
{
$title = ! empty($instance['title']) ? $instance['title'] : esc_html__('Title', 'text_domain');
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>">
<?php esc_attr_e('Title:', 'text_domain'); ?>
</label>
<input
class="widefat"
id="<?php echo esc_attr($this->get_field_id('title')); ?>"
name="<?php echo esc_attr($this->get_field_name('title')); ?>"
type="text"
value="<?php echo esc_attr($title); ?>">
</p>
<?php
}
/**
* Handles updating settings for the current Hosted JFT widget instance.
*
* @param array $new_instance New settings for this instance as input by the user via
* WP_Widget::form().
* @param array $old_instance Old settings for this instance.
* @return array Updated settings to save.
*/
public function update($new_instance, $old_instance)
{
$instance = array();
$instance['title'] = ( ! empty($new_instance['title']) ) ? strip_tags($new_instance['title']) : '';
return $instance;
}
}
/** END Hosted JFT Widget **/
?>