Completed
Push — master ( ab8a0f...a274e4 )
by
unknown
02:16
created

update_object::set_custom_taxonomy()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 9
nop 2
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
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
		// update custom taxonomy
59
		$taxs  = isset( $data['story_custom_taxonomy'] ) ? $data['story_custom_taxonomy'] : false;
60
		self::set_custom_taxonomy( $postid, $taxs );
61
		
62
		//update date
63
		$date  = isset( $data['post_date'] ) ? $data['post_date'] : false;
64
		self::set_date( $postid, $date );
65
66
67
		do_action( 'lasso_post_updated', $postid, $slug, $status, get_current_user_ID() );
68
69
		return true;
70
71
72
	}
73
74
	/**
75
	 * The keys required for the actions of this class.
76
	 *
77
	 * @since     0.9.2
78
	 *
79
	 * @return array Array of keys to pull from $_POST per action and their sanitization callback
80
	 */
81
	public static function params(){
82
		$params[ 'process_update_object_post' ] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
83
			'postid' => 'absint',
84
			'status' => 'strip_tags',
85
			'story_slug' => array(
86
				'trim',
87
				'sanitize_title'
88
			),
89
			'story_cats' => array(
90
				'trim',
91
				'strip_tags',
92
			),
93
			'story_tags' => array(
94
				'trim',
95
				'strip_tags',
96
			),
97
			'story_custom_taxonomy' => array(
98
				'trim',
99
				'strip_tags',
100
			),
101
			'post_date' => array(
102
				'trim',
103
				'strip_tags',
104
			),
105
106
		);
107
108
109
		return $params;
110
	}
111
112
	/**
113
	 * Additional auth callbacks to check.
114
	 *
115
	 * @since     0.9.2
116
	 *
117
	 * @return array Array of additional functions to use to authorize action.
118
	 */
119
	public static function auth_callbacks() {
120
		$params[ 'process_update_object_post' ] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
121
			'lasso_user_can'
122
		);
123
124
125
126
		return $params;
127
128
	}
129
130
131
	/**
132
	 *  Update terms for post.
133
	 *
134
	 *  @since    0.9.3
135
	 *
136
	 *  @param    int    	$postid       	The current postid
137
     *  @param    string|bool     $value    The term slug, or a comma separated list of slugs. Or false to remove all terms set for post.
138
	 *  @param    string     $taxonomy    	The name of the taxonomy to which the term belongs.
139
	 *
140
	 *  @return bool True if update was successful, false if not.
141
	 */
142
	public function set_post_terms( $postid, $value, $taxonomy ) {
143
		if( $value ) {
144
			// first check if multiple, make array if so.
145
			if ( self::has_multiple_objects( $value ) ) {	
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 142 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...
146
				$value = explode( ',', $value );
147
			}
148
			
149
			if ($taxonomy =='category') {
150
                // convert from names to category ids
151
				$cats = array();
152
				if (is_array($value)) {
153
					foreach ($value as $cat) {
154
						$cats [] = get_cat_ID($cat);
155
					}
156
					$value = $cats;
157
				} else {
158
					$value = get_cat_ID($value);
159
				}
160
			}
161
	
162
			$result = wp_set_object_terms( $postid, $value, $taxonomy );
163
		}
164
		else  {
165
			//remove all terms from post
166
			$result = wp_set_object_terms( $postid, null, $taxonomy );
167
168
		}
169
170
		if ( ! is_wp_error( $result ) ) {
171
			return true;
172
		}else{
173
			return false;
174
		}
175
	}
176
	
177
	/**
178
	 *  Update terms for post.
179
	 *
180
	 *
181
	 *  @param    int    	$postid       	The current postid
182
     *  @param    string|bool     $value    The term slug, or a comma separated list of slugs. Or false to remove all terms set for post.
183
	 *                                      The first item is the name of taxonomy
184
	 *
185
	 *  @return bool True if update was successful, false if not.
186
	 */
187
	public function set_custom_taxonomy( $postid, $value) {
188
		
189
		if( $value ) {
190
			// first check if multiple, make array if so.
191
			if ( self::has_multiple_objects( $value ) ) {	
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 187 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...
192
				$value = explode( ',', $value );
193
			}
194
						
195
            // Deleting first array item
196
			$taxonomy = array_shift($value);
197
			$cats = array();
198
			foreach ($value as $cat) {
199
				$cats [] = get_cat_ID($cat);
200
			}
201
			$value = $cats;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
202
			
203
	
204
			$result = wp_set_object_terms( $postid, $cats, $taxonomy );
205
			if ( ! is_wp_error( $result ) ) {
206
				return true;
207
			}else{
208
				return false;
209
			}
210
		}
211
	}
212
	
213
	public function set_date( $postid, $value) {
214
		
215
		if( $value ) {
216
			$time = current_time('mysql');
0 ignored issues
show
Unused Code introduced by
$time is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
217
            wp_update_post(
218
				array (
219
					'ID'            => $postid, // ID of the post to update
220
					'post_date'     => date( 'Y-m-d H:i:s',  strtotime($value) ),
221
					'post_date_gmt'     => gmdate( 'Y-m-d H:i:s',  strtotime($value) ),
222
				)
223
			);
224
		}
225
	}
226
227
	/**
228
	 *  Determines if the given value has multiple terms by checking to see
229
	 *  if a comma exists in the value.
230
	 *
231
	 *  @param    string   $value    The value to evaluate for multiple terms.
232
	 *  @return   bool               True if there are multiple terms; otherwise, false.
233
	 *	@since   0.9.3
234
	 */
235
	public function has_multiple_objects( $value ) {
236
237
		return 0 < strpos( $value, ',' );
238
239
	}
240
241
242
}
243
244