-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththemezee-related-posts.php
More file actions
322 lines (247 loc) · 8.64 KB
/
themezee-related-posts.php
File metadata and controls
322 lines (247 loc) · 8.64 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
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
<?php
/*
Plugin Name: ThemeZee Related Posts
Plugin URI: https://themezee.com/plugins/related-posts/
Description: This plugin is an easy way to display related posts on your website. Your visitors are introduced to other relevant content they might be interested in, which leads to an increase in traffic and reduced bounce rates.
Author: ThemeZee
Author URI: https://themezee.com/
Version: 1.0.7
Text Domain: themezee-related-posts
Domain Path: /languages/
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
ThemeZee Related Posts
Copyright(C) 2018, ThemeZee.com - support@themezee.com
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Use class to avoid namespace collisions.
if ( ! class_exists( 'ThemeZee_Related_Posts' ) ) :
/**
* Main ThemeZee_Related_Posts Class
*
* @package ThemeZee Related Posts
*/
class ThemeZee_Related_Posts {
/**
* Call all Functions to setup the Plugin
*
* @uses ThemeZee_Related_Posts::constants() Setup the constants needed
* @uses ThemeZee_Related_Posts::includes() Include the required files
* @uses ThemeZee_Related_Posts::setup_actions() Setup the hooks and actions
* @return void
*/
static function setup() {
// Setup Constants.
self::constants();
// Setup Translation.
add_action( 'plugins_loaded', array( __CLASS__, 'translation' ) );
// Include Files.
self::includes();
// Setup Action Hooks.
self::setup_actions();
}
/**
* Setup plugin constants
*
* @return void
*/
static function constants() {
// Define Plugin Name.
define( 'TZRP_NAME', 'ThemeZee Related Posts' );
// Define Version Number.
define( 'TZRP_VERSION', '1.0.7' );
// Define Plugin Name.
define( 'TZRP_PRODUCT_ID', 51298 );
// Define Update API URL.
define( 'TZRP_STORE_API_URL', 'https://themezee.com' );
// Define Plugin Name.
define( 'TZRP_LICENSE', '6934e03f8d16074ee58fd950088172e3' );
// Plugin Folder Path.
define( 'TZRP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
// Plugin Folder URL.
define( 'TZRP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
// Plugin Root File.
define( 'TZRP_PLUGIN_FILE', __FILE__ );
}
/**
* Load Translation File
*
* @return void
*/
static function translation() {
load_plugin_textdomain( 'themezee-related-posts', false, dirname( plugin_basename( TZRP_PLUGIN_FILE ) ) . '/languages/' );
}
/**
* Include required files
*
* @return void
*/
static function includes() {
// Include Admin Classes.
require_once TZRP_PLUGIN_DIR . '/includes/admin/class-themezee-plugins-page.php';
require_once TZRP_PLUGIN_DIR . '/includes/admin/class-tzrp-plugin-updater.php';
// Include Settings Classes.
require_once TZRP_PLUGIN_DIR . '/includes/settings/class-tzrp-settings.php';
require_once TZRP_PLUGIN_DIR . '/includes/settings/class-tzrp-settings-page.php';
// Include Related Posts Files.
require_once TZRP_PLUGIN_DIR . '/includes/class-tzrp-related-posts.php';
require_once TZRP_PLUGIN_DIR . '/includes/related-posts-template-functions.php';
}
/**
* Setup Action Hooks
*
* @see https://codex.wordpress.org/Function_Reference/add_action WordPress Codex
* @return void
*/
static function setup_actions() {
// Enqueue Frontend Widget Styles.
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_styles' ) );
// Register Image Sizes.
add_action( 'init', array( __CLASS__, 'add_image_size' ) );
// Add related posts to content.
add_filter( 'the_content', array( __CLASS__, 'related_posts_content_filter' ) );
// Add Settings link to Plugin actions.
add_filter( 'plugin_action_links_' . plugin_basename( TZRP_PLUGIN_FILE ), array( __CLASS__, 'plugin_action_links' ) );
// Add Related Posts Box to Plugin Overview Page.
add_action( 'themezee_plugins_overview_page', array( __CLASS__, 'plugin_overview_page' ) );
// Add License Key admin notice.
add_action( 'admin_notices', array( __CLASS__, 'license_key_admin_notice' ) );
// Add automatic plugin updater from ThemeZee Store API.
add_action( 'admin_init', array( __CLASS__, 'plugin_updater' ), 0 );
}
/**
* Enqueue Styles
*
* @return void
*/
static function enqueue_styles() {
// Return early if theme handles styling.
if ( current_theme_supports( 'themezee-related-posts' ) ) {
return;
}
// Enqueue Plugin Stylesheet.
wp_enqueue_style( 'themezee-related-posts', TZRP_PLUGIN_URL . 'assets/css/themezee-related-posts.css', array(), TZRP_VERSION );
}
/**
* Add custom image size for post thumbnails in related posts
*
* @return void
*/
static function add_image_size() {
// Check if theme defines custom image size.
if ( current_theme_supports( 'themezee-related-posts' ) ) :
$theme_support = get_theme_support( 'themezee-related-posts' );
// Set custom image size.
if ( isset( $theme_support[0]['thumbnail_size'] ) && is_array( $theme_support[0]['thumbnail_size'] ) ) :
$thumbnail_size = $theme_support[0]['thumbnail_size'];
add_image_size( 'themezee-related-posts', $thumbnail_size[0], $thumbnail_size[1], true );
endif;
else :
// Set default image size.
add_image_size( 'themezee-related-posts', 480, 300, true );
endif;
}
/**
* Add related posts to single posts content unless theme handles output
*
* @uses the_content filter hook
* @return void
*/
static function related_posts_content_filter( $content ) {
// Return early if theme handles plugin output.
if ( current_theme_supports( 'themezee-related-posts' ) ) {
return $content;
}
// Return early if it is not a single post.
if ( ! is_singular( 'post' ) ) {
return $content;
}
$related_posts = TZRP_Related_Posts::instance( array( 'echo' => false ) );
return $content . $related_posts->render();
}
/**
* Add Settings link to the plugin actions
*
* @return array $actions Plugin action links
*/
static function plugin_action_links( $actions ) {
$settings_link = array( 'settings' => sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php?page=themezee-plugins&tab=relatedposts' ), __( 'Settings', 'themezee-related-posts' ) ) );
return array_merge( $settings_link, $actions );
}
/**
* Add widget bundle box to plugin overview admin page
*
* @return void
*/
static function plugin_overview_page() {
$plugin_data = get_plugin_data( __FILE__ );
?>
<dl>
<dt>
<h4><?php echo esc_html( $plugin_data['Name'] ); ?></h4>
<span><?php printf( esc_html__( 'Version %s', 'themezee-related-posts' ), esc_html( $plugin_data['Version'] ) ); ?></span>
</dt>
<dd>
<p><?php echo wp_kses_post( $plugin_data['Description'] ); ?><br/></p>
<a href="<?php echo admin_url( 'options-general.php?page=themezee-plugins&tab=relatedposts' ); ?>" class="button button-primary"><?php esc_html_e( 'Plugin Settings', 'themezee-related-posts' ); ?></a>
<a href="<?php echo esc_url( 'https://themezee.com/docs/related-posts-documentation/?utm_source=plugin-overview&utm_medium=button&utm_campaign=related-posts&utm_content=documentation' ); ?>" class="button button-secondary" target="_blank"><?php esc_html_e( 'View Documentation', 'themezee-related-posts' ); ?></a>
</dd>
</dl>
<?php
}
/**
* Add license key admin notice
*
* @return void
*/
static function license_key_admin_notice() {
global $pagenow;
// Display only on Plugins and Updates page.
if ( ! ( 'plugins.php' == $pagenow or 'update-core.php' == $pagenow ) ) {
return;
}
// Get Settings.
$options = TZRP_Settings::instance();
if ( 'valid' <> $options->get( 'license_status' ) ) :
?>
<div class="updated">
<p>
<?php
printf( __( 'Please activate your license for the %1$s plugin in order to receive updates and support. <a href="%2$s">Activate License</a>', 'themezee-related-posts' ),
TZRP_NAME,
admin_url( 'options-general.php?page=themezee-plugins&tab=relatedposts' )
);
?>
</p>
</div>
<?php
endif;
}
/**
* Plugin Updater
*
* @return void
*/
static function plugin_updater() {
if ( ! is_admin() ) :
return;
endif;
$options = TZRP_Settings::instance();
if ( 'valid' === $options->get( 'license_status' ) ) :
// Setup the updater.
$tzrp_updater = new TZRP_Plugin_Updater( TZRP_STORE_API_URL, __FILE__, array(
'version' => TZRP_VERSION,
'license' => TZRP_LICENSE,
'item_name' => TZRP_NAME,
'item_id' => TZRP_PRODUCT_ID,
'author' => 'ThemeZee',
) );
endif;
}
}
// Run Plugin.
ThemeZee_Related_Posts::setup();
endif;