Completed
Pull Request — master (#133)
by Sébastien
01:53
created

includes/admin/class-alnp-admin.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Auto Load Next Post - Admin.
4
 *
5
 * @since    1.0.0
6
 * @version  1.5.0
7
 * @author   Sébastien Dumont
8
 * @category Admin
9
 * @package  Auto Load Next Post/Admin
10
 * @license  GPL-2.0+
11
 */
12
13
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
if ( ! class_exists( 'Auto_Load_Next_Post_Admin' ) ) {
19
20
	class Auto_Load_Next_Post_Admin {
21
22
		/**
23
		 * Constructor
24
		 *
25
		 * @access  public
26
		 * @since   1.0.0
27
		 * @version 1.4.10
28
		 */
29
		public function __construct() {
30
			// Include classes.
31
			add_action( 'admin_init', array( $this, 'includes' ), 10 );
32
33
			// Register scripts and styles for settings page.
34
			add_action( 'admin_enqueue_scripts', array( $this, 'admin_styles' ), 10 );
35
			add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ), 10 );
36
37
			// Add a message in the WP Privacy Policy Guide page.
38
			add_action( 'admin_init', array( $this, 'add_privacy_policy_guide_content' ) );
39
40
			// Add settings page.
41
			add_action( 'admin_menu', array( $this, 'admin_menu' ), 9 );
42
43
			// Filters
44
			add_filter( 'plugin_action_links_' . plugin_basename( AUTO_LOAD_NEXT_POST_FILE ), array( $this, 'plugin_action_links' ) );
45
			add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta'), 10, 3 );
46
			add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ) );
47
			add_filter( 'update_footer', array( $this, 'update_footer'), 15 );
48
		} // END __construct()
49
50
		/**
51
		 * Include any classes we need within admin.
52
		 *
53
		 * @access public
54
		 * @since  1.0.0
55
		 */
56
		public function includes() {
57
			// Classes we only need if the ajax is not-ajax
58
			if ( ! auto_load_next_post_is_ajax() ) {
59
				include( dirname( __FILE__ ) . '/class-alnp-admin-notices.php' ); // Plugin Notices
60
				include( dirname( __FILE__ ) . '/class-alnp-admin-help.php' ); // Plugin Help Tab
61
			}
62
		} // END includes()
63
64
		/**
65
		 * Register and enqueue stylesheets.
66
		 *
67
		 * @access public
68
		 * @since  1.5.0
69
		 * @global $wp_scripts
70
		 */
71
		public function admin_styles() {
72
			global $wp_scripts;
73
74
			$screen    = get_current_screen();
75
			$screen_id = $screen ? $screen->id : '';
0 ignored issues
show
$screen_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
77
			Auto_Load_Next_Post::load_file( AUTO_LOAD_NEXT_POST_SLUG . '_admin', '/assets/css/admin/auto-load-next-post' . AUTO_LOAD_NEXT_POST_SCRIPT_MODE . '.css' );
78
79
			if ( $screen->id == 'settings_page_auto-load-next-post-settings' ) {
80
				// Select2 - Make sure that we remove other registered Select2 to prevent styling issues.
81
				if ( wp_script_is( 'select2', 'registered' ) ) {
82
					wp_dequeue_style( 'select2' );
83
					wp_deregister_style( 'select2' );
84
				}
85
86
				Auto_Load_Next_Post::load_file( 'select2', '/assets/css/libs/select2' . AUTO_LOAD_NEXT_POST_SCRIPT_MODE . '.css' );
87
			}
88
		} // END admin_styles()
89
90
		/**
91
		 * Registers and enqueue javascripts.
92
		 *
93
		 * @access  public
94
		 * @since   1.0.0
95
		 * @version 1.5.0
96
		 */
97
		public function admin_scripts() {
98
			$screen    = get_current_screen();
99
			$screen_id = $screen ? $screen->id : '';
0 ignored issues
show
$screen_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
100
101
			if ( $screen->id == 'settings_page_auto-load-next-post-settings' ) {
102
				// Select2 - Make sure that we remove other registered Select2 to prevent plugin conflict issues.
103
				if ( wp_script_is( 'select2', 'registered' ) ) {
104
					wp_dequeue_script( 'select2' );
105
					wp_deregister_script( 'select2' );
106
				}
107
108
				// Load Select2
109
				Auto_Load_Next_Post::load_file( 'select2', '/assets/js/libs/select2' . AUTO_LOAD_NEXT_POST_SCRIPT_MODE . '.js', true, array( 'jquery' ), '4.0.5' );
110
111
				// Load plugin settings.
112
				Auto_Load_Next_Post::load_file( AUTO_LOAD_NEXT_POST_SLUG . '_admin', '/assets/js/admin/settings' . AUTO_LOAD_NEXT_POST_SCRIPT_MODE . '.js', true, array( 'jquery' ), AUTO_LOAD_NEXT_POST_VERSION );
113
114
				// Variables for Admin JavaScripts
115
				wp_localize_script( AUTO_LOAD_NEXT_POST_SLUG . '_admin', 'alnp_settings_params', array(
116
					'is_rtl'           => is_rtl() ? 'rtl' : 'ltr',
117
					'i18n_nav_warning' => esc_html__( 'The changes you made will be lost if you navigate away from this page.', 'auto-load-next-post' ),
118
				) );
119
			}
120
		} // END admin_scripts()
121
122
		/**
123
		 * Add a message in the WP Privacy Policy Guide page.
124
		 *
125
		 * @access public
126
		 * @since  1.5.0
127
		 * @static
128
		 */
129
		public static function add_privacy_policy_guide_content() {
130
			if ( function_exists( 'wp_add_privacy_policy_content' ) ) {
131
				wp_add_privacy_policy_content( esc_html__( 'Auto Load Next Post', 'auto-load-next-post' ), self::get_privacy_policy_guide_message() );
132
			}
133
		} // END add_privacy_policy_guide_content()
134
135
		/**
136
		 * Message to add in the WP Privacy Policy Guide page.
137
		 *
138
		 * @access protected
139
		 * @since  1.5.0
140
		 * @static
141
		 * @return string
142
		 */
143
		protected static function get_privacy_policy_guide_message() {
144
			$content = '
145
				<div contenteditable="false">' .
146
					'<p class="wp-policy-help">' .
147
						sprintf( __( '%s does not collect, store or share any personal data.', 'auto-load-next-post' ), esc_html__( 'Auto Load Next Post', 'auto-load-next-post' ) ) .
148
					'</p>' .
149
				'</div>';
150
151
			return $content;
152
		} // END get_privacy_policy_guide_message()
153
154
		/**
155
		 * Add Auto Load Next Post to the settings menu.
156
		 *
157
		 * @access  public
158
		 * @since   1.0.0
159
		 * @version 1.4.10
160
		 */
161
		public function admin_menu() {
162
			$settings_page = add_options_page(
163
				sprintf( __( '%s Settings', 'auto-load-next-post' ), esc_html__( 'Auto Load Next Post', 'auto-load-next-post' ) ),
164
				esc_html__( 'Auto Load Next Post', 'auto-load-next-post' ),
165
				'manage_options',
166
				'auto-load-next-post-settings',
167
				array( $this, 'settings_page' )
168
			);
169
170
			add_action( 'load-' . $settings_page, array( $this, 'settings_page_init' ) );
171
		} // END admin_menu()
172
173
		/**
174
		 * Loads settings.
175
		 *
176
		 * @access public
177
		 * @since  1.0.0
178
		 * @global string $current_tab
179
		 * @global string $current_section
180
		 */
181
		public function settings_page_init() {
182
			global $current_tab, $current_section;
183
184
			// Include settings pages.
185
			include_once( dirname( __FILE__ ) . '/class-alnp-admin-settings.php' );
186
187
			Auto_Load_Next_Post_Admin_Settings::get_settings_pages();
188
189
			// Get current tab/section.
190
			$current_tab     = empty( $_GET['tab'] ) ? 'theme-selectors' : sanitize_title( wp_unslash( $_GET['tab'] ) );
191
			$current_section = empty( $_REQUEST['section'] ) ? '' : sanitize_title( wp_unslash( $_REQUEST['section'] ) );
192
193
			// Save settings if data has been posted.
194
			if ( apply_filters( '' !== $current_section ? "auto_load_next_post_save_settings_{$current_tab}_{$current_section}" : "auto_load_next_post_save_settings_{$current_tab}", ! empty( $_POST ) ) ) {
195
				Auto_Load_Next_Post_Admin_Settings::save();
196
			}
197
198
			// Add any posted messages.
199
			if ( ! empty( $_GET['auto_load_next_post_error'] ) ) {
200
				Auto_Load_Next_Post_Admin_Settings::add_error( wp_kses_post( wp_unslash( $_GET['auto_load_next_post_error'] ) ) );
201
			}
202
203
			if ( ! empty( $_GET['auto_load_next_post_message'] ) ) {
204
				Auto_Load_Next_Post_Admin_Settings::add_message( wp_kses_post( wp_unslash( $_GET['auto_load_next_post_message'] ) ) );
205
			}
206
207
			do_action( 'auto_load_next_post_settings_page_init' );
208
		} // END settings_page_init()
209
210
		/**
211
		 * Initialize the Auto Load Next Post settings page.
212
		 *
213
		 * @access public
214
		 * @since  1.0.0
215
		 */
216
		public function settings_page() {
217
			include_once( dirname( __FILE__ ) . '/class-alnp-admin-settings.php' );
218
219
			Auto_Load_Next_Post_Admin_Settings::output();
220
		} // END settings_page()
221
222
		/**
223
		 * Plugin action links.
224
		 *
225
		 * @access  public
226
		 * @since   1.0.0
227
		 * @version 1.4.10
228
		 * @param   array $links
229
		 * @return  array $links
230
		 */
231
		public function plugin_action_links( $links ) {
232
			$plugin_action_links = array();
233
234
			if ( current_user_can( 'manage_options' ) ) {
235
				// Checks if Auto Load Next Post Pro has been installed.
236
				if ( ! is_alnp_pro_version_installed() ) {
237
					$plugin_action_links['go-pro'] = '<a href="' . esc_url( 'https://autoloadnextpost.com/pro/?utm_source=plugin&utm_medium=link&utm_campaign=plugins-page' ) . '" target="_blank" style="color:green; font-weight:bold;">' . __( 'Upgrade to Pro', 'auto-load-next-post' ) . '</a>';
238
				}
239
240
				$plugin_action_links['settings'] = '<a href="' . admin_url( 'options-general.php?page=auto-load-next-post-settings' ) . '">' . __( 'Settings', 'auto-load-next-post' ) . '</a>';
241
242
				return array_merge( $plugin_action_links, $links );
243
			}
244
245
			return $links;
246
		} // END plugin_action_links()
247
248
		/**
249
		 * Plugin row meta links
250
		 *
251
		 * @access  public
252
		 * @since   1.0.0
253
		 * @version 1.5.0
254
		 * @param   array  $links Plugin Row Meta
255
		 * @param   string $file  Plugin Base file
256
		 * @param   array  $data  Plugin Information
257
		 * @return  array  $links
258
		 */
259
		public function plugin_row_meta( $links, $file, $data ) {
260
			if ( $file == plugin_basename( AUTO_LOAD_NEXT_POST_FILE ) ) {
261
				$links[ 1 ] = sprintf( __( 'Developed By %s', 'auto-load-next-post' ), '<a href="' . $data[ 'AuthorURI' ] . '">' . $data[ 'Author' ] . '</a>' );
262
263
				$row_meta = array(
264
					'docs' => '<a href="' . esc_url( 'https://autoloadnextpost.com/documentation/?utm_source=plugin&utm_medium=link&utm_campaign=plugins-page' ) . '" target="_blank">' . __( 'Documentation', 'auto-load-next-post' ) . '</a>',
265
					'community' => '<a href="' . esc_url( 'https://wordpress.org/support/plugin/auto-load-next-post' ) . '" target="_blank">' . __( 'Community Support', 'auto-load-next-post' ) . '</a>',
266
					'theme-support' => '<a href="' . esc_url( 'https://autoloadnextpost.com/product/theme-support/?utm_source=plugin&utm_medium=link&utm_campaign=plugins-page' ) . '" target="_blank">' . __( 'Theme Support', 'auto-load-next-post' ) . '</a>',
267
				);
268
269
				$links = array_merge( $links, $row_meta );
270
			}
271
272
			return $links;
273
		} // END plugin_row_meta()
274
275
		/**
276
		 * Filters the admin footer text by placing simply thank you to those who
277
		 * like and review the plugin on WordPress.org.
278
		 *
279
		 * @access  public
280
		 * @since   1.0.0
281
		 * @version 1.4.10
282
		 * @param   string $text
283
		 * @return  string $text
284
		 */
285
		public function admin_footer_text( $text ) {
286
			$current_screen = get_current_screen();
287
288
			if ( isset( $current_screen->id ) && $current_screen->id == 'settings_page_auto-load-next-post-settings' ) {
289
				// Rating and Review
290
				return sprintf(
291
					/* translators: 1: Auto Load Next Post 2:: five stars */
292
					__( 'If you like %1$s, please leave a %2$s rating. A huge thank you in advance!', 'auto-load-next-post' ),
293
					sprintf( '<strong>%1$s</strong>', esc_html__( 'Auto Load Next Post', 'auto-load-next-post' ) ),
294
					'<a href="https://wordpress.org/support/plugin/auto-load-next-post/reviews?rate=5#new-post" target="_blank" data-rated="' . esc_attr__( 'Thanks :)', 'auto-load-next-post' ) . '">&#9733;&#9733;&#9733;&#9733;&#9733;</a>'
295
				);
296
			}
297
298
			return $text;
299
		} // END admin_footer_text()
300
301
		/**
302
		 * Filters the update footer by placing the version of the plugin
303
		 * when viewing any of the plugins pages.
304
		 *
305
		 * @access  public
306
		 * @since   1.0.0
307
		 * @version 1.4.10
308
		 * @param   string $text
309
		 * @return  string $text
310
		 */
311
		public function update_footer( $text ) {
312
			$screen = get_current_screen();
313
314
			if ( $screen->id == 'settings_page_auto-load-next-post-settings' ) {
315
				return '<p class="alignright">' . sprintf( __( '%s Version', 'auto-load-next-post' ), esc_html__( 'Auto Load Next Post', 'auto-load-next-post' ) ) . ' ' . esc_attr( AUTO_LOAD_NEXT_POST_VERSION ) . '</p>';
316
			}
317
318
			return $text;
319
		} // END update_footer()
320
321
	} // END class
322
323
} // END if class exists
324
325
return new Auto_Load_Next_Post_Admin();
326