Completed
Push — master ( ea40a4...a4569a )
by
unknown
02:51
created

includes/process/update_object.php (1 issue)

Labels
Severity

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
 * This class is responsible for updating the post settings such as the post slug or post status
4
 * and is toggle from the post settings modal
5
 *
6
 * @since 1.0
7
 */
8
namespace lasso\process;
9
10
use lasso\internal_api\api_action;
11
12
class update_object implements api_action{
13
14
	/**
15
	 * The nonce action for this request.
16
	 *
17
	 * @since 0.9.2
18
	 *
19
	 * @var string
20
	 */
21
	public $nonce_action = 'lasso-update-post-settings';
22
23
	/**
24
	 * Process the post update
25
	 *
26
	 * @since 0.9.2
27
	 *
28
	 * @param array $data Sanitized data to use for saving.
29
	 *
30
	 * @return bool Always returns true.
31
	 */
32
	public function post( $data ) {
33
34
		$status = isset( $data['status'] ) ? $data['status'] : false;
35
		$postid = isset( $data['postid'] ) ? $data['postid'] : false;
36
		$slug   = isset( $data['story_slug'] ) ? $data['story_slug'] : false;
37
38
39
40
		$args = array(
41
			'ID'   			=> (int) $postid,
42
			'post_name'  	=> $slug,
43
			'post_status' 	=> $status
44
		);
45
46
		wp_update_post( apply_filters( 'lasso_object_status_update_args', $args ) );
47
48
49
		// update categories
50
		$cats  = isset( $data['story_cats'] ) ? $data['story_cats'] : false;
51
		self::set_post_terms( $postid, $cats, 'category' );
52
53
54
		// update tags
55
		$tags = isset( $data['story_tags'] ) ? $data['story_tags'] : false;
56
		self::set_post_terms( $postid, $tags, 'post_tag' );
57
58
59
		do_action( 'lasso_post_updated', $postid, $slug, $status, get_current_user_ID() );
60
61
		return true;
62
63
64
	}
65
66
	/**
67
	 * The keys required for the actions of this class.
68
	 *
69
	 * @since     0.9.2
70
	 *
71
	 * @return array Array of keys to pull from $_POST per action and their sanitization callback
72
	 */
73
	public static function params(){
74
		$params[ 'process_update_object_post' ] = array(
75
			'postid' => 'absint',
76
			'status' => 'strip_tags',
77
			'story_slug' => array(
78
				'trim',
79
				'sanitize_title'
80
			),
81
			'story_cats' => array(
82
				'trim',
83
				'strip_tags',
84
			),
85
			'story_tags' => array(
86
				'trim',
87
				'strip_tags',
88
			),
89
90
		);
91
92
93
		return $params;
94
	}
95
96
	/**
97
	 * Additional auth callbacks to check.
98
	 *
99
	 * @since     0.9.2
100
	 *
101
	 * @return array Array of additional functions to use to authorize action.
102
	 */
103
	public static function auth_callbacks() {
104
		$params[ 'process_update_object_post' ] = array(
105
			'lasso_user_can'
106
		);
107
108
109
110
		return $params;
111
112
	}
113
114
115
	/**
116
	 *  Update terms for post.
117
	 *
118
	 *  @since    0.9.3
119
	 *
120
	 *  @param    int    	$postid       	The current postid
121
     *  @param    string|bool     $value    The term slug, or a comma separated list of slugs. Or false to remove all terms set for post.
122
	 *  @param    string     $taxonomy    	The name of the taxonomy to which the term belongs.
123
	 *
124
	 *  @return bool True if update was successful, false if not.
125
	 */
126
	public function set_post_terms( $postid, $value, $taxonomy ) {
127
		if( $value ) {
128
			// first check if multiple, make array if so.
129
			if ( self::has_multiple_objects( $value ) ) {
0 ignored issues
show
It seems like $value defined by parameter $value on line 126 can also be of type boolean; however, lasso\process\update_obj...:has_multiple_objects() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
130
				$value = explode( ',', $value );
131
			}
132
133
134
			$result = wp_set_object_terms( $postid, $value, $taxonomy );
135
		}
136
		else  {
137
			//remove all terms from post
138
			$result = wp_set_object_terms( $postid, null, $taxonomy );
139
140
		}
141
142
		if ( ! is_wp_error( $result ) ) {
143
			return true;
144
		}else{
145
			return false;
146
		}
147
148
149
	}
150
151
	/**
152
	 *  Determines if the given value has multiple terms by checking to see
153
	 *  if a comma exists in the value.
154
	 *
155
	 *  @param    string   $value    The value to evaluate for multiple terms.
156
	 *  @return   bool               True if there are multiple terms; otherwise, false.
157
	 *	@since   0.9.3
158
	 */
159
	public function has_multiple_objects( $value ) {
160
161
		return 0 < strpos( $value, ',' );
162
163
	}
164
165
166
}
167
168