-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalnp-js-boilerplate.php
186 lines (168 loc) · 5.04 KB
/
alnp-js-boilerplate.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
<?php
/*
* Plugin Name: Auto Load Next Post: JS Boilerplate
* Plugin URI: https://github.com/autoloadnextpost/alnp-js-boilerplate
* Description: Boilerplate for writing plugins for Auto Load Next Post JavaScript.
* Author: Auto Load Next Post
* Author URI: https://autoloadnextpost.com
* Version: 1.0.0
* Developer: Sébastien Dumont
* Developer URI: https://sebastiendumont.com
* Text Domain: alnp-js-boilerplate
* Domain Path: /languages/
*
* Copyright: © 2019 Sébastien Dumont
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* @package Auto Load Next Post: JS Boilerplate
* @author Auto Load Next Post
* @copyright Copyright © 2019, Auto Load Next Post
* @license GNU General Public License v3.0 http://www.gnu.org/licenses/gpl-3.0.html
*/
if ( ! class_exists( 'ALNP_JS_Boilerplate' ) ) {
class ALNP_JS_Boilerplate {
/**
* @var ALNP_JS_Boilerplate - the single instance of the class.
*
* @access protected
* @static
* @since 1.0.0
*/
protected static $_instance = null;
/**
* Plugin Version
*
* @access public
* @static
* @since 1.0.0
*/
public static $version = '1.0.0';
/**
* Required Auto Load Next Post Version
*
* @access public
* @static
* @since 1.0.0
*/
public static $required_alnp = '1.4.8';
/**
* Main ALNP_JS_Boilerplate Instance.
*
* Ensures only one instance of ALNP_JS_Boilerplate is loaded or can be loaded.
*
* @access public
* @static
* @since 1.0.0
* @see ALNP_JS_Boilerplate()
* @return ALNP_JS_Boilerplate - Main instance
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
} // END instance()
/**
* Cloning is forbidden.
*
* @access public
* @since 1.0.0
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'Cloning this object is forbidden.', 'alnp-js-boilerplate' ), self::$version );
} // END __clone()
/**
* Unserializing instances of this class is forbidden.
*
* @access public
* @since 1.0.0
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'alnp-js-boilerplate' ), self::$version );
} // END __wakeup()
/**
* ALNP_JS_Boilerplate Constructor.
*
* @access public
* @since 1.0.0
* @return ALNP_JS_Boilerplate
*/
public function __construct() {
$this->init_hooks();
} // END __construct()
/**
* Initialize hooks.
*
* @access private
* @since 1.0.0
*/
private function init_hooks() {
add_action( 'auto_load_next_post_loaded', array( $this, 'check_required_version' ) );
add_action( 'init', array( $this, 'load_plugin_textdomain' ), 0 );
add_action( 'wp_enqueue_scripts', array( $this, 'alnp_enqueue_scripts' ) );
} // END init_hooks()
/**
* Checks if the required Auto Load Next Post is installed.
*
* @access public
* @since 1.0.0
* @return bool
*/
public function check_required_version() {
if ( ! defined( 'AUTO_LOAD_NEXT_POST_VERSION' ) || version_compare( AUTO_LOAD_NEXT_POST_VERSION, self::$equired_alnp, '<' ) ) {
add_action( 'admin_notices', array( $this, 'alnp_not_installed' ) );
return false;
}
} // END check_alnp_installed()
/**
* Required version of Auto Load Next Post is Not Installed Notice.
*
* @access public
* @since 1.0.0
* @return void
*/
public function alnp_not_installed() {
echo '<div class="error"><p>' . sprintf( __( 'Auto Load Next Post: JS Boilerplate requires $1%s v$2%s or higher to be installed.', 'alnp-js-boilerplate' ), '<a href="https://autoloadnextpost.com/" target="_blank">Auto Load Next Post</a>', self::$required_alnp ) . '</p></div>';
} // END alnp_not_installed()
/**
* Get the Plugin URL.
*
* @access public
* @static
* @since 1.0.0
* @return string
*/
public static function plugin_url() {
return plugins_url( basename( plugin_dir_path(__FILE__) ), basename( __FILE__ ) );
} // END plugin_url()
/**
* Make the plugin translation ready.
*
* Translations should be added in the WordPress language directory:
* - WP_LANG_DIR/plugins/alnp-js-boilerplate-LOCALE.mo
*
* @access public
* @since 1.0.0
* @return void
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'alnp-js-boilerplate', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
} // END load_plugin_textdomain()
/**
* Load JS only on the front end for a single post.
*
* @access public
* @since 1.0.0
* @return void
*/
public function alnp_enqueue_scripts() {
if ( is_singular() && get_post_type() == 'post' ) {
wp_register_script( 'alnp-js-boilerplate', $this->plugin_url() . '/assets/js/alnp-js-boilerplate.js', array( 'jquery' ), self::$version, true );
wp_enqueue_script( 'alnp-js-boilerplate' );
}
} // END alnp_enqueue_scripts()
} // END class
} // END if class exists
return ALNP_JS_Boilerplate::instance();