Passed
Push — master ( 2434f2...f94701 )
by Nirjhar
08:29
created

PLUGIN_AJAX::custom_name_js()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.7998
1
<?php
2
/**
3
 * Doing AJAX the WordPress way.
4
 * Use this class in admin or user side
5
 *
6
 * @author     Nirjhar Lo
7
 * @version    1.2.1
8
 * @package    wp-plugin-framework
9
 */
10
if ( ! defined( 'ABSPATH' ) ) exit;
11
12
//AJAX helper class
13
if ( ! class_exists( 'PLUGIN_AJAX' ) ) {
14
15
	final class PLUGIN_AJAX {
16
17
18
		/**
19
		 * Add basic actions
20
		 *
21
		 * @return Void
22
		 */
23
		public function __construct() {
24
25
			add_action( 'wp_footer', array( $this, 'custom_name_js' ) );
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

25
			/** @scrutinizer ignore-call */ 
26
   add_action( 'wp_footer', array( $this, 'custom_name_js' ) );
Loading history...
26
			add_action( 'wp_ajax_custom_name', array( $this, 'custom_name' ) );
27
			add_action( 'wp_ajax_nopriv_custom_name', array( $this, 'custom_name' ) );
28
		}
29
30
31
		/**
32
		 * Output the form
33
		 *
34
		 * @return Html
0 ignored issues
show
Bug introduced by
The type Html was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
		 */
36
		public function form() { ?>
37
38
			<form id="add_by_ajax" method="POST" action="">
39
				<input type="text" name="text_name" placeholder="<?php _e( 'Text', 'textdomain' ); ?>">
0 ignored issues
show
Bug introduced by
The function _e 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

39
				<input type="text" name="text_name" placeholder="<?php /** @scrutinizer ignore-call */ _e( 'Text', 'textdomain' ); ?>">
Loading history...
40
				<input id="ajax_submit" type="submit" name="submit" value="Submit">
41
			</form>
42
			<?php
43
		}
44
45
46
		/**
47
		 * The javascript
48
		 *
49
		 * @return Html
50
		 */
51
		public function custom_name_js() { ?>
52
53
			<script type="text/javascript">
54
				jQuery(document).ready(function() {
55
56
					jQuery("#add_by_ajax form").submit(function() {
57
58
						event.preventDefault();
59
60
						var val = jQuery("input[name='text_name']").val();
61
62
							jQuery.post(
63
								'<?php echo admin_url("admin-ajax.php"); ?>',
0 ignored issues
show
Bug introduced by
The function admin_url 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

63
								'<?php echo /** @scrutinizer ignore-call */ admin_url("admin-ajax.php"); ?>',
Loading history...
64
								{ 'action': 'custom_name', 'val': val },
65
								function(response) {
66
									if ( response != '' && response != false && response != undefined ) {
67
68
										var data = JSON.parse(response);
69
										// Do some stuff
70
									}
71
								}
72
							);
73
						}
74
					});
75
				});
76
			</script>
77
		<?php
78
		}
79
80
81
		/**
82
		 * The data processor
83
		 *
84
		 * @return Json
85
		 */
86
		public function custom_name() {
87
88
			$val = $_POST['val'];
0 ignored issues
show
Unused Code introduced by
The assignment to $val is dead and can be removed.
Loading history...
89
90
			// DO some stuff
91
92
			$response = array( 'val' => $value );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $value does not exist. Did you maybe mean $val?
Loading history...
93
			echo json_encode( $response );
94
			wp_die();
0 ignored issues
show
Bug introduced by
The function wp_die 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

94
			/** @scrutinizer ignore-call */ 
95
   wp_die();
Loading history...
95
		}
96
	}
97
} ?>
98