Passed
Push — ci ( 9054a2...49f094 )
by litefeel
03:00
created

lib/export.php (1 issue)

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
 * GitHub Export Manager.
4
 *
5
 * @package Writing_On_GitHub
6
 */
7
8
/**
9
 * Class Writing_On_GitHub_Export
10
 */
11
class Writing_On_GitHub_Export {
12
13
	/**
14
	 * Application container.
15
	 *
16
	 * @var Writing_On_GitHub
17
	 */
18
	protected $app;
19
20
	/**
21
	 * Initializes a new export manager.
22
	 *
23
	 * @param Writing_On_GitHub $app Application container.
24
	 */
25
	public function __construct( Writing_On_GitHub $app ) {
26
		$this->app = $app;
27
	}
28
29
	/**
30
	 * Updates all of the current posts in the database on master.
31
	 *
32
     * @param  bool    $force
33
     *
34
     * @return string|WP_Error
35
     */
36
	public function full( $force = false ) {
37
		$posts = $this->app->database()->fetch_all_supported( $force );
38
39
		if ( is_wp_error( $posts ) ) {
40
            /* @var WP_Error $posts */
41
			return $posts;
42
		}
43
44
        $error = '';
45
46 View Code Duplication
        foreach ( $posts as $post ) {
47
            $result = $this->update( $post->id() );
48
            if ( is_wp_error( $result ) ) {
49
                /* @var WP_Error $result */
50
                $error = wogh_append_error( $error, $result );
51
            }
52
        }
53
54
        if ( is_wp_error( $error ) ) {
55
            /* @var WP_Error $error */
56
            return $error;
57
        }
58
59
        return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
60
	}
61
62
63
	/**
64
	 * Check if it exists in github
65
	 * @param  int  $post_id
66
	 * @return boolean
67
	 */
68
	protected function github_path( $post_id ) {
69
		$github_path = get_post_meta( $post_id, '_wogh_github_path', true );
70
71
		if ( $github_path && $this->app->api()->fetch()->exists( $github_path ) ) {
72
			return $github_path;
73
		}
74
75
		return false;
76
	}
77
78
	/**
79
	 * Updates the provided post ID in master.
80
	 *
81
	 * @param int $post_id Post ID to update.
82
	 *
83
	 * @return string|WP_Error
84
	 */
85
	public function update( $post_id ) {
86
		$post = $this->app->database()->fetch_by_id( $post_id );
87
88
		if ( is_wp_error( $post ) ) {
89
            /* @var WP_Error $post */
90
			return $post;
91
		}
92
93
		if ( 'trash' === $post->status() ) {
94
			return $this->delete( $post_id );
95
		}
96
97
		if ( $old_github_path = $this->github_path( $post->id() ) ) {
98
			error_log("old_github_path: $old_github_path");
99
			$post->set_old_github_path($old_github_path);
100
		}
101
102
		$result = $this->new_posts( array( $post ) );
0 ignored issues
show
array($post) is of type array<integer,object<WP_...ting_On_GitHub_Post>"}>, but the function expects a array<integer,object<Writing_On_GitHub_Post>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
104
		if ( is_wp_error( $result ) ) {
105
            /* @var WP_Error $result */
106
			return $result;
107
		}
108
109
		return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
110
	}
111
112
	/**
113
	 * Updates GitHub-created posts with latest WordPress data.
114
	 *
115
	 * @param Writing_On_GitHub_Post[] $posts Array of Posts to create.
116
	 *
117
	 * @return string|WP_Error
118
	 */
119
	public function new_posts( array $posts ) {
120
        $persist = $this->app->api()->persist();
121
122
		$error = '';
123 View Code Duplication
		foreach ( $posts as $post ) {
124
			$result = $this->new_post( $post, $persist );
125
			if ( is_wp_error( $result ) ) {
126
                /* @var WP_Error $result */
127
                $error = wogh_append_error( $error, $result );
128
			}
129
		}
130
131
		if ( is_wp_error( $error ) ) {
132
			return $error;
133
		}
134
135
        return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
136
	}
137
138
	protected function new_post( $post, $persist ) {
139
		$github_path = $post->github_path();
140
		$old_github_path = $post->old_github_path();
141
		$blob = $post->to_blob();
142
		$result = false;
143
144
		if ( $old_github_path && $old_github_path != $github_path ) {
145
			// rename
146
			$message = apply_filters(
147
				'wogh_commit_msg_move_post',
148
				sprintf(
149
					'Move %s to %s via WordPress at %s (%s)',
150
					$old_github_path, $github_path,
151
					site_url(),
152
					get_bloginfo( 'name' )
153
				)
154
			) . $this->get_commit_msg_tag();
155
156
			$result = $persist->delete_file( $post->old_github_path(), $blob->sha(), $message );
157
			if ( is_wp_error( $result ) ) {
158
				return $result;
159
			}
160
161
			$result = $persist->create_file( $blob, $message );
162
			if ( is_wp_error( $result ) ) {
163
				return $result;
164
			}
165
		} elseif ( ! $old_github_path ) {
166
			// create new
167
			$message = apply_filters(
168
				'wogh_commit_msg_new_post',
169
				sprintf(
170
					'Create new post %s from WordPress at %s (%s)',
171
					$github_path,
172
					site_url(),
173
					get_bloginfo( 'name' )
174
				)
175
			) . $this->get_commit_msg_tag();
176
			$result = $persist->create_file( $blob, $message );
177
			if ( is_wp_error( $result ) ) {
178
				return $result;
179
			}
180
		} elseif ( $old_github_path && $old_github_path == $github_path ) {
181
			// update
182
			$message = apply_filters(
183
				'wogh_commit_msg_update_post',
184
				sprintf(
185
					'Update post %s from WordPress at %s (%s)',
186
					$github_path,
187
					site_url(),
188
					get_bloginfo( 'name' )
189
				)
190
			) . $this->get_commit_msg_tag();
191
			$result = $persist->update_file( $blob, $message );
192
			if ( is_wp_error( $result ) ) {
193
				return $result;
194
			}
195
		}
196
197
		$sha = $result->content->sha;
198
		$post->set_sha($sha);
199
		$post->set_old_github_path($github_path);
200
201
		return true;
202
	}
203
204
	/**
205
	 * Deletes a provided post ID from master.
206
	 *
207
	 * @param int $post_id Post ID to delete.
208
	 *
209
	 * @return string|WP_Error
210
	 */
211
	public function delete( $post_id ) {
212
		$post = $this->app->database()->fetch_by_id( $post_id );
213
214
		if ( is_wp_error( $post ) ) {
215
            /* @var WP_Error $post */
216
			return $post;
217
		}
218
219
		$github_path = get_post_meta( $post_id, '_wogh_github_path', true );
220
221
		$message = apply_filters(
222
			'wogh_commit_msg_delete',
223
			sprintf(
224
				'Deleting %s via WordPress at %s (%s)',
225
				$github_path,
226
				site_url(),
227
				get_bloginfo( 'name' )
228
			),
229
			$post
230
		) . $this->get_commit_msg_tag();
231
232
		$result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message );
233
234
		if ( is_wp_error( $result ) ) {
235
            /* @var WP_Error $result */
236
			return $result;
237
		}
238
239
		return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
240
	}
241
242
243
	/**
244
	 * Saves the export user to the database.
245
	 *
246
	 * @param int $user_id User ID to export with.
247
	 *
248
	 * @return bool
249
	 */
250
	public function set_user( $user_id ) {
251
		return update_option( self::EXPORT_USER_OPTION, (int) $user_id );
252
	}
253
254
	/**
255
	 * Gets the commit message tag.
256
	 *
257
	 * @return string
258
	 */
259
	protected function get_commit_msg_tag() {
260
		$tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' );
261
262
		if ( ! $tag ) {
263
			throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) );
264
		}
265
266
		return ' - ' . $tag;
267
	}
268
}
269