-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpicker.php
More file actions
296 lines (251 loc) · 9.22 KB
/
picker.php
File metadata and controls
296 lines (251 loc) · 9.22 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
<?php
/*
Plugin Name: Picker
Description: Picker is a simple and flexible plugin which allow users to choose a specific post inside admin widgets page and display it in their site frontend.
Author: Andrea Landonio
Author URI: http://www.andrealandonio.it
Text Domain: picker
Domain Path: /languages/
Version: 1.1.6
License: GPL v3
Picker
Copyright (C) 2013-2023, Andrea Landonio - landonio.andrea@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Security check
if ( ! defined( 'ABSPATH' ) ) die( 'Direct access to the file is not permitted' );
if ( ! class_exists( 'Picker_Plugin' ) ) :
/**
* Main Picker Class
*
* @author Andrea Landonio
* @class Picker_Plugin
* @category Class
* @package Picker
*/
final class Picker_Plugin {
/**
* @var string The plugin version
*/
public $version = '1.1.2';
/**
* @var Picker_Plugin The single instance of the class
*/
protected static $_instance = null;
/**
* @var Picker_Item_Factory $factory The factory class
*/
public $factory = null;
/**
* Main Picker Instance
* Ensures only one instance of Picker is loaded or can be loaded
*
* @return Picker_Plugin - Main instance
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* __clone function because cloning is forbidden
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', PKR_DOMAIN ), '1.0.0' );
}
/**
* __wakeup function because unserialize instances of this class is forbidden
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', PKR_DOMAIN ), '1.0.0' );
}
/**
* __construct function
*
* @return Picker_Plugin
*/
public function __construct() {
// Auto-load classes on demand
if ( function_exists( '__autoload' ) ) {
spl_autoload_register( '__autoload' );
}
spl_autoload_register( array( $this, 'autoload' ) );
// Define constants
$this->define_constants();
// Include required files
$this->includes();
// Hooks
add_action( 'init', array( $this, 'init' ), 0 );
add_action( 'widgets_init', array( $this, 'include_widgets' ) );
}
/**
* __get function to auto-load in-accessible properties on demand
*
* @param mixed $key
* @return mixed
*/
public function __get( $key ) {
if ( method_exists( $this, $key ) ) {
return $this->$key();
}
return false;
}
/**
* Auto-load classes on demand to reduce memory consumption
*
* @param mixed $class
*/
public function autoload( $class ) {
$path = null;
$class = strtolower( $class );
$file = 'class-' . str_replace( '_', '-', $class ) . '.php';
if ( strpos( $class, 'picker_admin' ) === 0 ) {
$path = $this->plugin_path() . '/includes/admin/';
} elseif ( strpos( $class, 'picker_utils' ) === 0 ) {
$path = $this->plugin_path() . '/includes/utils/';
} elseif ( strpos( $class, 'picker_widget_' ) === 0 ) {
$path = $this->plugin_path() . '/includes/widgets/';
}
if ( $path && is_readable( $path . $file ) ) {
include_once( $path . $file );
return;
}
// Fallback
if ( strpos( $class, 'picker_' ) === 0 ) {
$path = $this->plugin_path() . '/includes/';
}
if ( $path && is_readable( $path . $file ) ) {
include_once( $path . $file );
return;
}
}
/**
* Define constants
*/
private function define_constants() {
define( 'PKR_PLUGIN_FILE', __FILE__ );
define( 'PKR_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'PKR_PLUGIN_SITE', '#' );
define( 'PKR_VERSION', $this->version );
define( 'PKR_LOCALE_IT', 'it_IT' );
define( 'PKR_DOMAIN', 'picker' );
define( 'PKR_BASE', 'picker' );
if ( ! defined( 'PKR_DELIMITER' ) ) {
define( 'PKR_DELIMITER', '|' );
}
// Default values for admin settings variables
define( 'PKR_POST_LIST_ITEMS', 50 );
define( 'PKR_POST_TYPES', 'post' );
define( 'PKR_CACHE_ENABLED', true );
define( 'PKR_CACHE_CLEANUP', true );
define( 'PKR_CACHE_EXPIRE_DEFAULT', 60 * 60 * 24 );
define( 'PKR_CACHE_EXPIRE_SCHEDULED', 60 * 5 );
}
/**
* Include required core files used in admin and on the frontend
*/
private function includes() {
// Include generics files
include_once( 'includes/core.php' );
include_once( 'includes/install.php' );
include_once( 'includes/settings.php' );
// Include admin classes
if ( is_admin() ) {
include_once( 'includes/admin/class-picker-admin.php' );
}
// Include abstract classes
include_once( 'includes/abstracts/abstract-picker-item.php' );
// Include classes and factories
include_once( 'includes/class-picker-item-factory.php' );
}
/**
* Include core widgets
*/
public function include_widgets() {
include_once( 'includes/abstracts/abstract-picker-widget.php' );
// Register widgets
register_widget( 'Picker_Widget_Default' );
}
/**
* Init Picker when WordPress initialises
*/
public function init() {
// Set up localization
$this->load_plugin_textdomain();
// Load class Picker Item Factory to create new item instances
$this->factory = new Picker_Item_Factory();
}
/**
* Load Localisation files
*
* Note: the first-loaded translation file overrides any following ones if the same translation is present
*/
public function load_plugin_textdomain() {
$locale = apply_filters( 'plugin_locale', get_locale(), PKR_DOMAIN );
$dir = trailingslashit( WP_LANG_DIR );
/**
* Global Locale. Looks in:
* - WP_LANG_DIR/Picker/Picker-LOCALE.mo
* - picker/languages/Picker-LOCALE.mo (which if not found falls back to:)
* - WP_LANG_DIR/plugins/Picker-LOCALE.mo
*/
load_textdomain( PKR_DOMAIN, $dir . 'picker/picker-' . $locale . '.mo' );
load_plugin_textdomain( PKR_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . "/languages" );
}
/********************
* Helper functions *
********************/
/**
* Get plugin url
*
* @return string
*/
public function plugin_url() {
return untrailingslashit( plugins_url( '/', __FILE__ ) );
}
/**
* Get plugin path
*
* @return string
*/
public function plugin_path() {
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
/**
* Get template path
*
* @return string
*/
public function template_path() {
return apply_filters( 'picker_template_path', 'picker/' );
}
/**
* Get Ajax URL
*
* @return string
*/
public function ajax_url() {
return admin_url( 'admin-ajax.php', 'relative' );
}
}
endif;
/**
* Returns the main instance of Picker to prevent the need to use globals.
*
* @return Picker_Plugin
*/
function Picker_Plugin() {
return Picker_Plugin::instance();
}
// Global for backwards compatibility.
$GLOBALS[ 'Picker_Plugin' ] = Picker_Plugin();