Completed
Push — master ( d1e536...37e872 )
by
unknown
02:11
created

update_object::set_post_terms()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 14
nop 3
dl 0
loc 36
rs 6.7272
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
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(
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...
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(
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...
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
Bug introduced by
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
			if ($taxonomy =='category') {
134
                // convert from names to category ids
135
				$cats = array();
136
				if (is_array($value)) {
137
					foreach ($value as $cat) {
138
						$cats [] = get_cat_ID($cat);
139
					}
140
					$value = $cats;
141
				} else {
142
					$value = get_cat_ID($value);
143
				}
144
			}
145
	
146
			$result = wp_set_object_terms( $postid, $value, $taxonomy );
147
		}
148
		else  {
149
			//remove all terms from post
150
			$result = wp_set_object_terms( $postid, null, $taxonomy );
151
152
		}
153
154
		if ( ! is_wp_error( $result ) ) {
155
			return true;
156
		}else{
157
			return false;
158
		}
159
160
161
	}
162
163
	/**
164
	 *  Determines if the given value has multiple terms by checking to see
165
	 *  if a comma exists in the value.
166
	 *
167
	 *  @param    string   $value    The value to evaluate for multiple terms.
168
	 *  @return   bool               True if there are multiple terms; otherwise, false.
169
	 *	@since   0.9.3
170
	 */
171
	public function has_multiple_objects( $value ) {
172
173
		return 0 < strpos( $value, ',' );
174
175
	}
176
177
178
}
179
180