ALNP_AJAX::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Auto Load Next Post - AJAX Events.
4
 *
5
 * @since    1.6.0
6
 * @author   Sébastien Dumont
7
 * @category Classes
8
 * @package  Auto Load Next Post/Classes/AJAX
9
 * @license  GPL-2.0+
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
if ( ! class_exists( 'ALNP_AJAX' ) ) {
18
19
	class ALNP_AJAX {
20
21
		/**
22
		 * Constructor.
23
		 *
24
		 * @access public
25
		 */
26
		public function __construct() {
27
			self::add_ajax_events();
28
		} // END __construct()
29
30
		/**
31
		 * Hook in methods - uses WordPress Ajax handlers (admin-ajax).
32
		 *
33
		 * @access public
34
		 * @static
35
		 */
36
		public static function add_ajax_events() {
37
			$ajax_events = array(
38
				'find_template_location'        => true,
39
				'get_container_selectors'       => true,
40
				'get_title_selectors'           => true,
41
				'get_post_navigation_selectors' => true,
42
				'get_comment_selectors'         => true,
43
				'set_setting'                   => true,
44
			);
45
46
			foreach ( $ajax_events as $ajax_event => $nopriv ) {
47
				add_action( 'wp_ajax_alnp_' . $ajax_event, array( __CLASS__, $ajax_event ) );
48
49
				if ( $nopriv ) {
50
					add_action( 'wp_ajax_nopriv_alnp_' . $ajax_event, array( __CLASS__, $ajax_event ) );
51
				}
52
			}
53
		} // END add_ajax_events()
54
55
		/**
56
		 * Find template location.
57
		 *
58
		 * @access public
59
		 * @static
60
		 * @return string|int
61
		 */
62
		public static function find_template_location() {
63
			$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
64
65
			if ( empty( $post_type ) ) {
66
				wp_send_json( -1 );
67
				wp_die();
68
			}
69
70
			// Scan directories and saves the location.
71
			alnp_scan_directories( $post_type );
72
73
			// Get directory location.
74
			$directory = alnp_get_template_directory( $post_type );
75
76
			// Return if directory was saved.
77
			if ( ! empty( $directory ) || $directory == null ) {
78
				wp_send_json( $directory );
79
			} else {
80
				wp_send_json( -1 );
81
			}
82
83
			wp_die();
84
		} // END find_template_location()
85
86
		/**
87
		 * Return container theme selectors.
88
		 *
89
		 * @access public
90
		 * @static
91
		 * @return json $selectors
92
		 */
93
		public static function get_container_selectors() {
94
			$selectors = array(
95
				'main.site-main',
96
				'main#main',
97
				'main.post-wrap',
98
				'div#main',
99
				'div.site-content',
100
				'div#content',
101
				'div.content-container'
102
			);
103
104
			wp_send_json( $selectors );
105
			wp_die();
106
		} // END get_container_selectors()
107
108
		/**
109
		 * Return title theme selectors.
110
		 *
111
		 * @access public
112
		 * @static
113
		 * @return json $selectors
114
		 */
115
		public static function get_title_selectors() {
116
			$selectors = array(
117
				'h1.entry-title',
118
				'h1.post-title',
119
				'h1.page-title',
120
				'h1.title-single',
121
				'h1'
122
			);
123
124
			wp_send_json( $selectors );
125
			wp_die();
126
		} // END get_title_selectors()
127
128
		/**
129
		 * Return post navigation theme selectors.
130
		 *
131
		 * @access public
132
		 * @static
133
		 * @return json $selectors
134
		 */
135
		public static function get_post_navigation_selectors() {
136
			$selectors = array(
137
				'nav.post-navigation',
138
				'nav.navigation-post',
139
				'nav.navigation',
140
				'div.navigation',
141
				'nav#nav-below',
142
				'#nav-single',
143
				'div.next-prev',
144
				'nav.prev-next-nav'
145
			);
146
147
			wp_send_json( $selectors );
148
			wp_die();
149
		} // END get_post_navigation_selectors()
150
151
		/**
152
		 * Return comments container theme selectors.
153
		 *
154
		 * @access public
155
		 * @static
156
		 * @return json $selectors
157
		 */
158
		public static function get_comment_selectors() {
159
			$selectors = array(
160
				'div#comments',
161
				'section#comments'
162
			);
163
164
			wp_send_json( $selectors );
165
			wp_die();
166
		} // END get_comment_selectors()
167
168
		/**
169
		 * Sets an Auto Load Next Post Setting.
170
		 *
171
		 * @access public
172
		 * @static
173
		 * @return bool
174
		 */
175
		public static function set_setting() {
176
			$setting = isset( $_POST['setting'] ) ? $_POST['setting'] : '';
177
			$value   = isset( $_POST['value'] ) ? $_POST['value'] : '';
178
179
			if ( empty( $setting ) && empty( $value ) ) {
180
				wp_send_json( -1 );
181
				wp_die();
182
			}
183
184
			update_option( 'auto_load_next_post_' . $setting, $value );
185
186
			wp_send_json( 1 );
187
			wp_die();
188
		} // END set_setting()
189
190
	} // END class
191
192
} // END if class exists
193
194
return new ALNP_AJAX();
195