Completed
Pull Request — master (#52)
by Bui Quang
02:42
created

Ajax::hooks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace WPDFI;
4
5
/**
6
 * This class handle all ajax actions of this plugin
7
 * 
8
 * @author Duc Bui Quang <[email protected]>
9
 * @since 1.0.0
10
 */
11
12
use WPDFI\Traits\Singleton;
13
14
final class Ajax
15
{
16
	use Singleton;
17
18
	/**
19
	 * @traitDoc
20
	 */
21
	public function initializes()
22
	{
23
		$this->hooks();
24
	}
25
26
	/**
27
	 * All ajax action of this plugin come here
28
	 *
29
	 * @since 1.0.0
30
	 * @return void
31
	 */
32
	public function hooks() {
33
		$actions = ['get_post_types', 'get_terms', 'get_image_size_names_and_dimensions', 
34
					'get_default_layout', 'get_related_layout', 'generate_feature_image'];
35
36
		foreach($actions as $action) {
37
			\add_action('wp_ajax_wpdfi_'. $action, [$this, $action]);
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
			/** @scrutinizer ignore-call */ 
38
   \add_action('wp_ajax_wpdfi_'. $action, [$this, $action]);
Loading history...
38
			\add_action('wp_ajax_nopriv_wpdfi'. $action, [$this, $action]);
39
		}
40
	}
41
42
	/**
43
	 * Get related layout with post type value, related layout include taxonomies, image upload and image size
44
	 *
45
	 * @since 1.0.0
46
	 * @return void
47
	 */
48
	public function get_related_layout() {
49
		echo json_encode(\wpdfi()->layout->get_related_layout($_POST['section_index'], $_POST['post_type']));
0 ignored issues
show
Bug Best Practice introduced by
The property layout does not exist on WPDFI. Since you implemented __get, consider adding a @property annotation.
Loading history...
50
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
51
	}
52
53
	/**
54
	 * Get default layout of single section in admin
55
	 *
56
	 * @since 1.0.0
57
	 * @return void
58
	 */
59
	public function get_default_layout() {
60
		echo json_encode(\wpdfi()->layout->get_default_layout($_POST['index'], $_POST['include_delete']));
0 ignored issues
show
Bug Best Practice introduced by
The property layout does not exist on WPDFI. Since you implemented __get, consider adding a @property annotation.
Loading history...
61
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
62
	}
63
64
	/**
65
	 * Ajax action to get all post types which support thumbnail feature
66
	 *
67
	 * @since 1.0.0
68
	 * @return array
69
	 */
70
	public function get_post_types() {
71
		echo json_encode(\wpdfi()->post_type->get_id_and_text());
0 ignored issues
show
Bug Best Practice introduced by
The property post_type does not exist on WPDFI. Since you implemented __get, consider adding a @property annotation.
Loading history...
72
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
73
	}
74
75
	/**
76
	 * Ajax action to get all terms by given taxonomy
77
	 * 
78
	 * @since 1.0.0
79
	 * @return array
80
	 */
81
	public function get_terms() {
82
		echo json_encode(\wpdfi()->term->get($_POST['taxonomy']));
0 ignored issues
show
Bug Best Practice introduced by
The property term does not exist on WPDFI. Since you implemented __get, consider adding a @property annotation.
Loading history...
83
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
84
	}
85
86
87
	public function generate_feature_image() {
88
		/* security check. */
89
		check_ajax_referer( 'wpdfi-ajax-nonce', 'security' );
0 ignored issues
show
Bug introduced by
The function check_ajax_referer was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
		/** @scrutinizer ignore-call */ 
90
  check_ajax_referer( 'wpdfi-ajax-nonce', 'security' );
Loading history...
90
		$update_fimage = \wpdfi()->post_type->update_fimage($_POST['post_id']);
0 ignored issues
show
Bug Best Practice introduced by
The property post_type does not exist on WPDFI. Since you implemented __get, consider adding a @property annotation.
Loading history...
91
		$post_type = \get_post_type($_POST['post_id']);
0 ignored issues
show
Bug introduced by
The function get_post_type was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
		$post_type = /** @scrutinizer ignore-call */ \get_post_type($_POST['post_id']);
Loading history...
92
		$post_type_name = \wpdfi()->post_type->get_singular_name($post_type);
93
		echo json_encode(['status' => $update_fimage, 'namePT' => $post_type_name, 'postId' => $_POST['post_id']]);
94
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
95
	}
96
}