-
Notifications
You must be signed in to change notification settings - Fork 1
/
trending-posts.php
267 lines (229 loc) · 6.3 KB
/
trending-posts.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
267
<?php
/**
* Plugin Name: Trending Posts
* Plugin URI:
* Description: Adds rating and views count for posts and custom post types.
* Version: 1.0.1
* Author: Zemez
* Author URI: https://zemez.io/wordpress/
* Text Domain: trending-posts
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// If class 'TM_Trending_Posts' not exists.
if ( ! class_exists( 'TM_Trending_Posts' ) ) {
/**
* Sets up and initializes the `Trending Posts` plugin.
*
* @since 1.0.0
*/
class TM_Trending_Posts {
/**
* A reference to an instance of this class.
*
* @since 1.0.0
* @var object
*/
private static $instance = null;
/**
* A reference to an instance of cherry framework core class.
*
* @since 1.0.0
* @var object
*/
private $core = null;
/**
* Sets up needed actions/filters for the plugin to initialize.
*
* @since 1.0.0
*/
public function __construct() {
// Define plugin constants.
add_action( 'after_setup_theme', array( $this, 'constants' ), 0 );
// Load the installer core.
add_action( 'after_setup_theme', require( trailingslashit( __DIR__ ) . 'cherry-framework/setup.php' ), 0 );
// Load the core class.
add_action( 'after_setup_theme', array( $this, 'get_core' ), 1 );
// Load all modules.
add_action( 'after_setup_theme', array( 'Cherry_Core', 'load_all_modules' ), 2 );
// Internationalize the text strings used.
add_action( 'after_setup_theme', array( $this, 'lang' ), 2 );
// Load the plugin files.
add_action( 'after_setup_theme', array( $this, 'includes' ), 3 );
// Load the admin files.
add_action( 'after_setup_theme', array( $this, 'admin' ), 4 );
// Load public-facing stylesheet and JavaScript.
add_action( 'wp_enqueue_scripts', array( $this, 'public_assets' ), 9 );
// Enqueue public-facing JavaScript only on specific action.
add_action( 'tm_trending_posts_assets', array( $this, 'enqueue_assets' ) );
}
/**
* Defines constants for the plugin.
*
* @since 1.0.0
*/
public function constants() {
/**
* Set the version number of the plugin.
*
* @since 1.0.0
*/
define( 'TM_TREND_POSTS_VERSION', '1.0.1' );
/**
* Set constant path to the plugin directory.
*
* @since 1.0.0
*/
define( 'TM_TREND_POSTS_DIR', trailingslashit( plugin_dir_path( __FILE__ ) ) );
/**
* Set constant path to the plugin URI.
*
* @since 1.0.0
*/
define( 'TM_TREND_POSTS_URI', trailingslashit( plugin_dir_url( __FILE__ ) ) );
/**
* Set the cache time.
*
* @since 1.0.0
*/
if ( ! defined( 'TM_TREND_POSTS_CACHE_TIME' ) ) {
define( 'TM_TREND_POSTS_CACHE_TIME', 30 * DAY_IN_SECONDS );
}
}
/**
* Loads the core functions. These files are needed before loading anything else in the
* themes or plugins because they have required functions for use.
*
* @since 1.0.0
*/
public function get_core() {
global $chery_core_version;
if ( null !== $this->core ) {
return $this->core;
}
if ( 0 < sizeof( $chery_core_version ) ) {
$core_paths = array_values( $chery_core_version );
require_once( $core_paths[0] );
} else {
die( 'Class Cherry_Core not found' );
}
$this->core = new Cherry_Core( array(
'base_dir' => TM_TREND_POSTS_DIR . 'cherry-framework',
'base_url' => TM_TREND_POSTS_URI . 'cherry-framework',
'modules' => array(
'cherry-utility' => array(
'autoload' => true,
),
'cherry-js-core' => array(
'autoload' => false,
),
'cherry-ui-elements' => array(
'autoload' => false,
),
'cherry-interface-builder' => array(
'autoload' => false,
),
'cherry-widget-factory' => array(
'autoload' => true,
),
),
) );
return $this->core;
}
/**
* Loads files from the '/inc' folder.
*
* @since 1.0.0
*/
public function includes() {
require_once( TM_TREND_POSTS_DIR . 'inc/class-trending-posts-data.php' );
require_once( TM_TREND_POSTS_DIR . 'inc/class-trending-posts-widget.php' );
require_once( TM_TREND_POSTS_DIR . 'inc/class-trending-posts-callback-views.php' );
require_once( TM_TREND_POSTS_DIR . 'inc/class-trending-posts-callback-rating.php' );
}
/**
* Loads the translation files.
*
* @since 1.0.0
*/
public function lang() {
load_plugin_textdomain( 'trending-posts', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Loads admin files.
*
* @since 1.0.0
*/
public function admin() {
if ( ! is_admin() ) {
return;
}
global $pagenow;
// Init `cherry-js-core` module only on `Widgets` page.
if ( null !== $pagenow && 'widgets.php' === $pagenow ) {
$this->get_core()->init_module( 'cherry-js-core' );
}
}
/**
* Register and enqueue public-facing stylesheet.
*
* @since 1.0.0
*/
public function public_assets() {
wp_register_style(
'font-awesome',
TM_TREND_POSTS_URI . 'assets/css/font-awesome.min.css', array(), '4.5.0'
);
wp_enqueue_style(
'tm-trending-posts',
TM_TREND_POSTS_URI . 'assets/css/style.css', array( 'font-awesome' ), TM_TREND_POSTS_VERSION
);
wp_register_script(
'tm-trending-posts',
TM_TREND_POSTS_URI . 'assets/js/min/script.min.js', array( 'jquery' ), TM_TREND_POSTS_VERSION, true
);
}
/**
* Enqueue required assets only on call.
*
* @since 1.0.0
*/
public function enqueue_assets() {
/**
* Check before printing JavaScript variable via `wp_localize_script()`.
*/
if ( did_action( 'tm_trending_posts_assets' ) > 1 ) {
return;
}
wp_enqueue_script( 'tm-trending-posts' );
wp_localize_script(
'tm-trending-posts',
'tmTrendPosts',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'tm_trend_posts' ),
'cache' => apply_filters( 'tm_trending_posts_cache_fix', 0 ),
)
);
}
/**
* Returns the instance.
*
* @since 1.0.0
* @return object
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
}
TM_Trending_Posts::get_instance();
}