Metabox::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
namespace NirjharLo\WP_Plugin_Framework\Src;
3
4
if ( ! defined( 'ABSPATH' ) ) exit;
5
6
/**
7
 * Build a sample metabox in editor screen
8
 *
9
 * @author     Nirjhar Lo
10
 * @package    wp-plugin-framework
11
 */
12
if ( ! class_exists( 'Metabox' ) ) {
13
14
	final class Metabox {
15
16
17
		/**
18
		 * Add Meetabox
19
		 *
20
		 * @return Void
21
		 */
22
		public function __construct() {
23
24
			//Adding the metabox. For custom post type use "add_meta_boxes_posttype" action
25
			add_action( 'add_meta_boxes', array( $this, 'register' ) );
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( 'add_meta_boxes', array( $this, 'register' ) );
Loading history...
26
			add_action( 'save_post', array( $this, 'save' ), 10, 2 );
27
		}
28
29
30
		/**
31
		 * Metabox callback
32
		 *
33
		 * @return Html
0 ignored issues
show
Bug introduced by
The type NirjharLo\WP_Plugin_Framework\Src\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...
34
		 */
35
		public function register() {
36
37
			add_meta_box(
0 ignored issues
show
Bug introduced by
The function add_meta_box 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_meta_box(
Loading history...
38
				'meta-box-id',
39
				esc_html__( 'MetaBox Title', 'textdomain' ),
0 ignored issues
show
Bug introduced by
The function esc_html__ 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
				/** @scrutinizer ignore-call */ 
40
    esc_html__( 'MetaBox Title', 'textdomain' ),
Loading history...
40
				array( $this, 'render' ),
41
				// Declare the post type to show meta box
42
				'post_type',
43
				'normal',
44
				'core'
45
			);
46
		}
47
48
49
		/**
50
		 * Metabox render HTML
51
		 *
52
		 * @return Html
53
		 */
54
		public function render() {
55
56
			wp_nonce_field( basename( __FILE__ ), 'metabox_name_nonce' ); ?>
0 ignored issues
show
Bug introduced by
The function wp_nonce_field 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

56
			/** @scrutinizer ignore-call */ 
57
   wp_nonce_field( basename( __FILE__ ), 'metabox_name_nonce' ); ?>
Loading history...
57
58
			<p>
59
				<label for="metabox_name"><?php _e( "Custom Text", 'textdomain' ); ?></label>
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

59
				<label for="metabox_name"><?php /** @scrutinizer ignore-call */ _e( "Custom Text", 'textdomain' ); ?></label>
Loading history...
60
    			<br />
61
    			<input class="widefat" type="text" name="metabox_field_name" id="metabox_field_name" value="<?php echo esc_attr( get_post_meta( $object->ID, 'metabox_name', true ) ); ?>" />
0 ignored issues
show
Bug introduced by
The function get_post_meta 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

61
    			<input class="widefat" type="text" name="metabox_field_name" id="metabox_field_name" value="<?php echo esc_attr( /** @scrutinizer ignore-call */ get_post_meta( $object->ID, 'metabox_name', true ) ); ?>" />
Loading history...
Comprehensibility Best Practice introduced by
The variable $object seems to be never defined.
Loading history...
Bug introduced by
The function esc_attr 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

61
    			<input class="widefat" type="text" name="metabox_field_name" id="metabox_field_name" value="<?php echo /** @scrutinizer ignore-call */ esc_attr( get_post_meta( $object->ID, 'metabox_name', true ) ); ?>" />
Loading history...
62
  			</p>
63
  			<?php
64
		}
65
66
67
		/**
68
 	    * Save the Metabox post data
69
 		*
70
 		* @param Array $atts
71
 		*
72
 		* @return Html
73
 		*/
74
		function save( $post_id, $post ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
75
76
			//Check if doing autosave
77
			if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
0 ignored issues
show
Bug introduced by
The constant NirjharLo\WP_Plugin_Framework\Src\DOING_AUTOSAVE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
78
79
			//Verify the nonce before proceeding.
80
			if ( !isset( $_POST['metabox_name_nonce'] ) || !wp_verify_nonce( $_POST['metabox_name_nonce'], basename( __FILE__ ) ) ) return;
0 ignored issues
show
Bug introduced by
The function wp_verify_nonce 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

80
			if ( !isset( $_POST['metabox_name_nonce'] ) || !/** @scrutinizer ignore-call */ wp_verify_nonce( $_POST['metabox_name_nonce'], basename( __FILE__ ) ) ) return;
Loading history...
81
82
			//Get the post type object.
83
			$post_type = get_post_type_object( $post->post_type );
0 ignored issues
show
Bug introduced by
The function get_post_type_object 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

83
			$post_type = /** @scrutinizer ignore-call */ get_post_type_object( $post->post_type );
Loading history...
84
85
			//Check if the current user has permission to edit the post.
86
			if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) return $post_id;
0 ignored issues
show
Bug introduced by
The function current_user_can 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

86
			if ( !/** @scrutinizer ignore-call */ current_user_can( $post_type->cap->edit_post, $post_id ) ) return $post_id;
Loading history...
87
88
			if ( isset( $_POST['metabox_field_name'] ) ) {
89
				update_post_meta( $post_id, 'metabox_field_name', sanitize_text_field($_POST['metabox_field_name']) );
0 ignored issues
show
Bug introduced by
The function sanitize_text_field 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
				update_post_meta( $post_id, 'metabox_field_name', /** @scrutinizer ignore-call */ sanitize_text_field($_POST['metabox_field_name']) );
Loading history...
Bug introduced by
The function update_post_meta 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
    update_post_meta( $post_id, 'metabox_field_name', sanitize_text_field($_POST['metabox_field_name']) );
Loading history...
90
			}
91
		}
92
	}
93
} ?>
94