Completed
Push — master ( 819e30...58fb4c )
by Mike
02:24
created
includes/class-create-post.php 1 patch
Spacing   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
  * @subpackage Evans
9 9
  * @author     Old Town Media
10 10
  */
11
-class CreatePost{
11
+class CreatePost {
12 12
 
13 13
 	/**
14 14
 	 * metaboxes
@@ -25,7 +25,7 @@  discard block
 block discarded – undo
25 25
 	 *
26 26
 	 * @see Metaboxes
27 27
 	 */
28
-	public function __construct(){
28
+	public function __construct() {
29 29
 
30 30
 		$this->metaboxes = new Metaboxes;
31 31
 
@@ -47,32 +47,32 @@  discard block
 block discarded – undo
47 47
 	 * @param boolean $echo Whether or not to echo. Optional.
48 48
 	 * @param int $num Optional. Number of posts to create.
49 49
 	 */
50
-	public function create_post_type_content( $slug, $connection, $echo = false, $num = '' ){
50
+	public function create_post_type_content($slug, $connection, $echo = false, $num = '') {
51 51
 
52 52
 		// If we're missing a custom post type id - don't do anything
53
-		if ( empty( $slug ) ){
53
+		if (empty($slug)) {
54 54
 			return;
55 55
 		}
56 56
 
57 57
 		// Gather the necessary data to create the posts
58
-		$supports 	= $this->get_cpt_supports( $slug );
59
-		$metaboxes	= $this->metaboxes->get_metaboxes( $slug );
58
+		$supports 	= $this->get_cpt_supports($slug);
59
+		$metaboxes	= $this->metaboxes->get_metaboxes($slug);
60 60
 
61 61
 		// Set our connection status for the rest of the methods
62 62
 		$this->connected = $connection;
63 63
 
64 64
 		// If we forgot to put in a quantity, make one for us
65
-		if ( empty( $num ) ){
66
-			$num = rand( 5, 30 );
65
+		if (empty($num)) {
66
+			$num = rand(5, 30);
67 67
 		}
68 68
 
69 69
 		// Create test posts
70
-		for( $i = 0; $i < $num; $i++ ){
70
+		for ($i = 0; $i < $num; $i++) {
71 71
 
72
-			$return = $this->create_test_object( $slug, $supports, $metaboxes );
72
+			$return = $this->create_test_object($slug, $supports, $metaboxes);
73 73
 
74
-			if ( $echo === true ){
75
-				echo \json_encode( $return );
74
+			if ($echo === true) {
75
+				echo \json_encode($return);
76 76
 			}
77 77
 
78 78
 		}
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
 	 * @param array $supports Features that the post type supports.
96 96
 	 * @param array $supports All CMB2 metaboxes attached to the post type.
97 97
 	 */
98
-	private function create_test_object( $slug, $supports, $metaboxes ){
98
+	private function create_test_object($slug, $supports, $metaboxes) {
99 99
 		$return = '';
100 100
 
101 101
 		// Get a random title
@@ -103,7 +103,7 @@  discard block
 block discarded – undo
103 103
 
104 104
 		// First, insert our post
105 105
 		$post = array(
106
-		  'post_name'      => sanitize_title( $title ),
106
+		  'post_name'      => sanitize_title($title),
107 107
 		  'post_status'    => 'publish',
108 108
 		  'post_type'      => $slug,
109 109
 		  'ping_status'    => 'closed',
@@ -111,57 +111,57 @@  discard block
 block discarded – undo
111 111
 		);
112 112
 
113 113
 		// Add title if supported
114
-		if ( $supports['title'] === true ){
114
+		if ($supports['title'] === true) {
115 115
 			$post['post_title'] = $title;
116 116
 		}
117 117
 
118 118
 		// Add main content if supported
119
-		if ( $supports['editor'] === true ){
119
+		if ($supports['editor'] === true) {
120 120
 			$post['post_content'] = TestContent::paragraphs();
121 121
 		}
122 122
 
123 123
 		// Add excerpt content if supported
124
-		if ( $supports['excerpt'] === true ){
124
+		if ($supports['excerpt'] === true) {
125 125
 			$post['post_excerpt'] = TestContent::plain_text();
126 126
 		}
127 127
 
128 128
 		// Insert then post object
129
-		$post_id = wp_insert_post( $post );
129
+		$post_id = wp_insert_post($post);
130 130
 
131 131
 		// Then, set a test content flag on the new post for later deletion
132
-		add_post_meta( $post_id, 'evans_test_content', '__test__', true );
132
+		add_post_meta($post_id, 'evans_test_content', '__test__', true);
133 133
 
134 134
 		// Add thumbnail if supported
135
-		if ( $this->connected == true && ( $supports['thumbnail'] === true || in_array( $slug, array( 'post', 'page' ) ) ) ){
136
-			 update_post_meta( $post_id, '_thumbnail_id', TestContent::image( $post_id ) );
135
+		if ($this->connected == true && ($supports['thumbnail'] === true || in_array($slug, array('post', 'page')))) {
136
+			 update_post_meta($post_id, '_thumbnail_id', TestContent::image($post_id));
137 137
 		}
138 138
 
139
-		$taxonomies = get_object_taxonomies( $slug );
139
+		$taxonomies = get_object_taxonomies($slug);
140 140
 
141 141
 		// Assign the post to terms
142
-		if ( !empty( $taxonomies ) ){
143
-			$return .= $this->assign_terms( $post_id, $taxonomies );
142
+		if (!empty($taxonomies)) {
143
+			$return .= $this->assign_terms($post_id, $taxonomies);
144 144
 		}
145 145
 
146 146
 		// Spin up metaboxes
147
-		if ( !empty( $metaboxes ) ){
148
-			foreach ( $metaboxes as $cmb ) :
149
-				$return .= $this->metaboxes->random_metabox_content( $post_id, $cmb, $this->connected );
147
+		if (!empty($metaboxes)) {
148
+			foreach ($metaboxes as $cmb) :
149
+				$return .= $this->metaboxes->random_metabox_content($post_id, $cmb, $this->connected);
150 150
 			endforeach;
151 151
 		}
152 152
 
153 153
 		// Check if we have errors and return them or created message
154
-		if ( is_wp_error( $return ) ){
155
-			error_log( $return->get_error_message() );
154
+		if (is_wp_error($return)) {
155
+			error_log($return->get_error_message());
156 156
 			return $return;
157 157
 		} else {
158 158
 			return array(
159 159
 				'type'		=> 'created',
160 160
 				'object'	=> 'post',
161 161
 				'pid'		=> $post_id,
162
-				'post_type'	=> get_post_type( $post_id ),
163
-				'link_edit'	=> admin_url( '/post.php?post='.$post_id.'&action=edit' ),
164
-				'link_view'	=> get_permalink( $post_id ),
162
+				'post_type'	=> get_post_type($post_id),
163
+				'link_edit'	=> admin_url('/post.php?post='.$post_id.'&action=edit'),
164
+				'link_view'	=> get_permalink($post_id),
165 165
 			);
166 166
 		}
167 167
 
@@ -178,13 +178,13 @@  discard block
 block discarded – undo
178 178
 	 * @param string $slug a custom post type ID.
179 179
 	 * @return array Array of necessary supports booleans.
180 180
 	 */
181
-	private function get_cpt_supports( $slug ){
181
+	private function get_cpt_supports($slug) {
182 182
 
183 183
 		$supports = array(
184
-			'title'		=> post_type_supports( $slug, 'title' ),
185
-			'editor'	=> post_type_supports( $slug, 'editor' ),
186
-			'excerpt'	=> post_type_supports( $slug, 'excerpt' ),
187
-			'thumbnail'	=> post_type_supports( $slug, 'thumbnail' )
184
+			'title'		=> post_type_supports($slug, 'title'),
185
+			'editor'	=> post_type_supports($slug, 'editor'),
186
+			'excerpt'	=> post_type_supports($slug, 'excerpt'),
187
+			'thumbnail'	=> post_type_supports($slug, 'thumbnail')
188 188
 		);
189 189
 
190 190
 		return $supports;
@@ -205,26 +205,26 @@  discard block
 block discarded – undo
205 205
 	 * @param array $taxonomies taxonomies assigned to this cpt.
206 206
 	 * @return object WP Error if there is one.
207 207
 	 */
208
-	private function assign_terms( $post_id, $taxonomies ){
208
+	private function assign_terms($post_id, $taxonomies) {
209 209
 
210 210
 		// Make sure it's an array & has items
211
-		if ( empty( $taxonomies ) || !is_array( $taxonomies ) ){
211
+		if (empty($taxonomies) || !is_array($taxonomies)) {
212 212
 			return;
213 213
 		}
214 214
 
215
-		foreach ( $taxonomies as $tax ){
215
+		foreach ($taxonomies as $tax) {
216 216
 
217 217
 			// Get the individual terms already existing
218
-			$terms = get_terms( $tax, array( 'hide_empty'	=> false ) );
219
-			$count = count( $terms ) - 1;
218
+			$terms = get_terms($tax, array('hide_empty'	=> false));
219
+			$count = count($terms) - 1;
220 220
 
221 221
 			// If there are no terms, skip to the next taxonomy
222
-			if ( empty( $terms ) ){
222
+			if (empty($terms)) {
223 223
 				continue;
224 224
 			}
225 225
 
226 226
 			// Get a random index to use
227
-			$index = rand( 0, $count );
227
+			$index = rand(0, $count);
228 228
 
229 229
 			// Initialize our array
230 230
 			$post_data = array(
@@ -232,14 +232,14 @@  discard block
 block discarded – undo
232 232
 			);
233 233
 
234 234
 			// Set the term data to update
235
-			$post_data['tax_input'][ $tax ] = array( $terms[$index]->term_id );
235
+			$post_data['tax_input'][$tax] = array($terms[$index]->term_id);
236 236
 
237 237
 			// Update the post with the taxonomy info
238
-			$return = wp_update_post( $post_data );
238
+			$return = wp_update_post($post_data);
239 239
 
240 240
 			// Return the error if it exists
241
-			if ( is_wp_error( $return ) ){
242
-				error_log( $return->get_error_messages() );
241
+			if (is_wp_error($return)) {
242
+				error_log($return->get_error_messages());
243 243
 				return $return->get_error_messages();
244 244
 			}
245 245
 
Please login to merge, or discard this patch.