@@ -9,264 +9,264 @@ |
||
9 | 9 | */ |
10 | 10 | class Writing_On_GitHub_Admin { |
11 | 11 | |
12 | - /** |
|
13 | - * plugin file name rel plugin dir. |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $plugin_file; |
|
17 | - |
|
18 | - /** |
|
19 | - * Hook into GitHub API |
|
20 | - */ |
|
21 | - public function __construct( $plugin_file ) { |
|
22 | - $this->plugin_file = $plugin_file; |
|
23 | - |
|
24 | - add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); |
|
25 | - add_action( 'admin_init', array( $this, 'register_settings' ) ); |
|
26 | - add_action( 'current_screen', array( $this, 'trigger_cron' ) ); |
|
27 | - add_filter( 'plugin_action_links', array($this, 'settings_links'), 10, 2 ); |
|
28 | - } |
|
29 | - |
|
30 | - /** |
|
31 | - * settings link |
|
32 | - * @param string[] $links |
|
33 | - * @param string $file |
|
34 | - * @return string[] |
|
35 | - */ |
|
36 | - public function settings_links( $links, $file ) { |
|
37 | - if ( $file != $this->plugin_file ) { |
|
38 | - return $links; |
|
39 | - } |
|
40 | - |
|
41 | - $settings_link = '<a href="options-general.php?page=' . |
|
42 | - Writing_On_GitHub::$text_domain . '">' . __( 'Settings', 'writing-on-github' ) . '</a>'; |
|
43 | - |
|
44 | - array_push( $links, $settings_link ); |
|
45 | - |
|
46 | - return $links; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Callback to render the settings page view |
|
51 | - */ |
|
52 | - public function settings_page() { |
|
53 | - include dirname( dirname( __FILE__ ) ) . '/views/options.php'; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Callback to register the plugin's options |
|
58 | - */ |
|
59 | - public function register_settings() { |
|
60 | - add_settings_section( |
|
61 | - 'general', |
|
62 | - 'General Settings', |
|
63 | - array( $this, 'section_callback' ), |
|
64 | - Writing_On_GitHub::$text_domain |
|
65 | - ); |
|
66 | - |
|
67 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_host' ); |
|
68 | - add_settings_field( 'wogh_host', __( 'GitHub hostname', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
69 | - 'default' => 'https://api.github.com', |
|
70 | - 'name' => 'wogh_host', |
|
71 | - 'help_text' => __( 'The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'writing-on-github' ), |
|
72 | - ) |
|
73 | - ); |
|
74 | - |
|
75 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_repository' ); |
|
76 | - add_settings_field( 'wogh_repository', __( 'Repository', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
77 | - 'default' => '', |
|
78 | - 'name' => 'wogh_repository', |
|
79 | - 'help_text' => __( 'The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>github/hubot.github.com</code>. The repository should contain an initial commit, which is satisfied by including a README when you create the repository on GitHub.', 'writing-on-github' ), |
|
80 | - ) |
|
81 | - ); |
|
82 | - |
|
83 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_branch' ); |
|
84 | - add_settings_field( 'wogh_branch', __( 'Branch', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
85 | - 'default' => 'master', |
|
86 | - 'name' => 'wogh_branch', |
|
87 | - 'help_text' => __( 'The GitHub branch to commit to, default is master.', 'writing-on-github' ), |
|
88 | - ) |
|
89 | - ); |
|
90 | - |
|
91 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_oauth_token' ); |
|
92 | - add_settings_field( 'wogh_oauth_token', __( 'Oauth Token', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
93 | - 'default' => '', |
|
94 | - 'name' => 'wogh_oauth_token', |
|
95 | - 'help_text' => __( "A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'writing-on-github' ), |
|
96 | - ) |
|
97 | - ); |
|
98 | - |
|
99 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_secret' ); |
|
100 | - add_settings_field( 'wogh_secret', __( 'Webhook Secret', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
101 | - 'default' => '', |
|
102 | - 'name' => 'wogh_secret', |
|
103 | - 'help_text' => __( "The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'writing-on-github' ), |
|
104 | - ) |
|
105 | - ); |
|
106 | - |
|
107 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_default_user' ); |
|
108 | - add_settings_field( 'wogh_default_user', __( 'Default Import User', 'writing-on-github' ), array( &$this, 'user_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
109 | - 'default' => '', |
|
110 | - 'name' => 'wogh_default_user', |
|
111 | - 'help_text' => __( 'The fallback user for import, in case Writing On GitHub cannot find the committer in the database.', 'writing-on-github' ), |
|
112 | - ) |
|
113 | - ); |
|
114 | - |
|
115 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_author' ); |
|
116 | - add_settings_field( 'wogh_ignore_author', __( 'Ignore author', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
117 | - 'default' => '', |
|
118 | - 'name' => 'wogh_ignore_author', |
|
119 | - 'help_text' => __( 'Do not export author and do not use author info from GitHub.', 'writing-on-github' ), |
|
120 | - ) |
|
121 | - ); |
|
122 | - |
|
123 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_dont_export_content' ); |
|
124 | - add_settings_field( 'wogh_dont_export_content', __( 'Don\'t export content', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
125 | - 'default' => '', |
|
126 | - 'name' => 'wogh_dont_export_content', |
|
127 | - 'help_text' => __( 'Do not export post content to github, only export meta.', 'writing-on-github' ), |
|
128 | - ) |
|
129 | - ); |
|
130 | - |
|
131 | - // register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_metas' ); |
|
132 | - // add_settings_field( 'wogh_ignore_metas', __( 'Ignore post metas', 'writing-on-github' ), array( &$this, 'textarea_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
133 | - // 'default' => '', |
|
134 | - // 'name' => 'wogh_ignore_metas', |
|
135 | - // 'help_text' => __( 'These meta keys will be ignored and cannot be imported and exported. One meta key per line.', 'writing-on-github' ), |
|
136 | - // ) |
|
137 | - // ); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Callback to render an individual options field |
|
142 | - * |
|
143 | - * @param array $args Field arguments. |
|
144 | - */ |
|
145 | - public function field_callback( $args ) { |
|
146 | - include dirname( dirname( __FILE__ ) ) . '/views/setting-field.php'; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Callback to render the default import user field. |
|
151 | - * |
|
152 | - * @param array $args Field arguments. |
|
153 | - */ |
|
154 | - public function user_field_callback( $args ) { |
|
155 | - include dirname( dirname( __FILE__ ) ) . '/views/user-setting-field.php'; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Callback to render the textarea field. |
|
160 | - * |
|
161 | - * @param array $args Field arguments. |
|
162 | - */ |
|
163 | - public function textarea_field_callback( $args ) { |
|
164 | - include dirname( dirname( __FILE__ ) ) . '/views/textarea-setting-field.php'; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Callback to render the checkbox field. |
|
169 | - * |
|
170 | - * @param array $args Field arguments. |
|
171 | - */ |
|
172 | - public function checkbox_field_callback( $args ) { |
|
173 | - include dirname( dirname( __FILE__ ) ) . '/views/checkbox-setting-field.php'; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Displays settings messages from background processes |
|
178 | - */ |
|
179 | - public function section_callback() { |
|
180 | - if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { |
|
181 | - return; |
|
182 | - } |
|
183 | - |
|
184 | - if ( 'yes' === get_option( '_wogh_export_started' ) ) { ?> |
|
12 | + /** |
|
13 | + * plugin file name rel plugin dir. |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $plugin_file; |
|
17 | + |
|
18 | + /** |
|
19 | + * Hook into GitHub API |
|
20 | + */ |
|
21 | + public function __construct( $plugin_file ) { |
|
22 | + $this->plugin_file = $plugin_file; |
|
23 | + |
|
24 | + add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); |
|
25 | + add_action( 'admin_init', array( $this, 'register_settings' ) ); |
|
26 | + add_action( 'current_screen', array( $this, 'trigger_cron' ) ); |
|
27 | + add_filter( 'plugin_action_links', array($this, 'settings_links'), 10, 2 ); |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * settings link |
|
32 | + * @param string[] $links |
|
33 | + * @param string $file |
|
34 | + * @return string[] |
|
35 | + */ |
|
36 | + public function settings_links( $links, $file ) { |
|
37 | + if ( $file != $this->plugin_file ) { |
|
38 | + return $links; |
|
39 | + } |
|
40 | + |
|
41 | + $settings_link = '<a href="options-general.php?page=' . |
|
42 | + Writing_On_GitHub::$text_domain . '">' . __( 'Settings', 'writing-on-github' ) . '</a>'; |
|
43 | + |
|
44 | + array_push( $links, $settings_link ); |
|
45 | + |
|
46 | + return $links; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Callback to render the settings page view |
|
51 | + */ |
|
52 | + public function settings_page() { |
|
53 | + include dirname( dirname( __FILE__ ) ) . '/views/options.php'; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Callback to register the plugin's options |
|
58 | + */ |
|
59 | + public function register_settings() { |
|
60 | + add_settings_section( |
|
61 | + 'general', |
|
62 | + 'General Settings', |
|
63 | + array( $this, 'section_callback' ), |
|
64 | + Writing_On_GitHub::$text_domain |
|
65 | + ); |
|
66 | + |
|
67 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_host' ); |
|
68 | + add_settings_field( 'wogh_host', __( 'GitHub hostname', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
69 | + 'default' => 'https://api.github.com', |
|
70 | + 'name' => 'wogh_host', |
|
71 | + 'help_text' => __( 'The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'writing-on-github' ), |
|
72 | + ) |
|
73 | + ); |
|
74 | + |
|
75 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_repository' ); |
|
76 | + add_settings_field( 'wogh_repository', __( 'Repository', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
77 | + 'default' => '', |
|
78 | + 'name' => 'wogh_repository', |
|
79 | + 'help_text' => __( 'The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>github/hubot.github.com</code>. The repository should contain an initial commit, which is satisfied by including a README when you create the repository on GitHub.', 'writing-on-github' ), |
|
80 | + ) |
|
81 | + ); |
|
82 | + |
|
83 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_branch' ); |
|
84 | + add_settings_field( 'wogh_branch', __( 'Branch', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
85 | + 'default' => 'master', |
|
86 | + 'name' => 'wogh_branch', |
|
87 | + 'help_text' => __( 'The GitHub branch to commit to, default is master.', 'writing-on-github' ), |
|
88 | + ) |
|
89 | + ); |
|
90 | + |
|
91 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_oauth_token' ); |
|
92 | + add_settings_field( 'wogh_oauth_token', __( 'Oauth Token', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
93 | + 'default' => '', |
|
94 | + 'name' => 'wogh_oauth_token', |
|
95 | + 'help_text' => __( "A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'writing-on-github' ), |
|
96 | + ) |
|
97 | + ); |
|
98 | + |
|
99 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_secret' ); |
|
100 | + add_settings_field( 'wogh_secret', __( 'Webhook Secret', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
101 | + 'default' => '', |
|
102 | + 'name' => 'wogh_secret', |
|
103 | + 'help_text' => __( "The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'writing-on-github' ), |
|
104 | + ) |
|
105 | + ); |
|
106 | + |
|
107 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_default_user' ); |
|
108 | + add_settings_field( 'wogh_default_user', __( 'Default Import User', 'writing-on-github' ), array( &$this, 'user_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
109 | + 'default' => '', |
|
110 | + 'name' => 'wogh_default_user', |
|
111 | + 'help_text' => __( 'The fallback user for import, in case Writing On GitHub cannot find the committer in the database.', 'writing-on-github' ), |
|
112 | + ) |
|
113 | + ); |
|
114 | + |
|
115 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_author' ); |
|
116 | + add_settings_field( 'wogh_ignore_author', __( 'Ignore author', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
117 | + 'default' => '', |
|
118 | + 'name' => 'wogh_ignore_author', |
|
119 | + 'help_text' => __( 'Do not export author and do not use author info from GitHub.', 'writing-on-github' ), |
|
120 | + ) |
|
121 | + ); |
|
122 | + |
|
123 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_dont_export_content' ); |
|
124 | + add_settings_field( 'wogh_dont_export_content', __( 'Don\'t export content', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
125 | + 'default' => '', |
|
126 | + 'name' => 'wogh_dont_export_content', |
|
127 | + 'help_text' => __( 'Do not export post content to github, only export meta.', 'writing-on-github' ), |
|
128 | + ) |
|
129 | + ); |
|
130 | + |
|
131 | + // register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_metas' ); |
|
132 | + // add_settings_field( 'wogh_ignore_metas', __( 'Ignore post metas', 'writing-on-github' ), array( &$this, 'textarea_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
133 | + // 'default' => '', |
|
134 | + // 'name' => 'wogh_ignore_metas', |
|
135 | + // 'help_text' => __( 'These meta keys will be ignored and cannot be imported and exported. One meta key per line.', 'writing-on-github' ), |
|
136 | + // ) |
|
137 | + // ); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Callback to render an individual options field |
|
142 | + * |
|
143 | + * @param array $args Field arguments. |
|
144 | + */ |
|
145 | + public function field_callback( $args ) { |
|
146 | + include dirname( dirname( __FILE__ ) ) . '/views/setting-field.php'; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Callback to render the default import user field. |
|
151 | + * |
|
152 | + * @param array $args Field arguments. |
|
153 | + */ |
|
154 | + public function user_field_callback( $args ) { |
|
155 | + include dirname( dirname( __FILE__ ) ) . '/views/user-setting-field.php'; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Callback to render the textarea field. |
|
160 | + * |
|
161 | + * @param array $args Field arguments. |
|
162 | + */ |
|
163 | + public function textarea_field_callback( $args ) { |
|
164 | + include dirname( dirname( __FILE__ ) ) . '/views/textarea-setting-field.php'; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Callback to render the checkbox field. |
|
169 | + * |
|
170 | + * @param array $args Field arguments. |
|
171 | + */ |
|
172 | + public function checkbox_field_callback( $args ) { |
|
173 | + include dirname( dirname( __FILE__ ) ) . '/views/checkbox-setting-field.php'; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Displays settings messages from background processes |
|
178 | + */ |
|
179 | + public function section_callback() { |
|
180 | + if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { |
|
181 | + return; |
|
182 | + } |
|
183 | + |
|
184 | + if ( 'yes' === get_option( '_wogh_export_started' ) ) { ?> |
|
185 | 185 | <div class="updated"> |
186 | 186 | <p><?php esc_html_e( 'Export to GitHub started.', 'writing-on-github' ); ?></p> |
187 | 187 | </div><?php |
188 | - delete_option( '_wogh_export_started' ); |
|
189 | - } |
|
188 | + delete_option( '_wogh_export_started' ); |
|
189 | + } |
|
190 | 190 | |
191 | - if ( $message = get_option( '_wogh_export_error' ) ) { ?> |
|
191 | + if ( $message = get_option( '_wogh_export_error' ) ) { ?> |
|
192 | 192 | <div class="error"> |
193 | 193 | <p><?php esc_html_e( 'Export to GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p> |
194 | 194 | </div><?php |
195 | - delete_option( '_wogh_export_error' ); |
|
196 | - } |
|
195 | + delete_option( '_wogh_export_error' ); |
|
196 | + } |
|
197 | 197 | |
198 | - if ( 'yes' === get_option( '_wogh_export_complete' ) ) { ?> |
|
198 | + if ( 'yes' === get_option( '_wogh_export_complete' ) ) { ?> |
|
199 | 199 | <div class="updated"> |
200 | 200 | <p><?php esc_html_e( 'Export to GitHub completed successfully.', 'writing-on-github' );?></p> |
201 | 201 | </div><?php |
202 | - delete_option( '_wogh_export_complete' ); |
|
203 | - } |
|
202 | + delete_option( '_wogh_export_complete' ); |
|
203 | + } |
|
204 | 204 | |
205 | - if ( 'yes' === get_option( '_wogh_import_started' ) ) { ?> |
|
205 | + if ( 'yes' === get_option( '_wogh_import_started' ) ) { ?> |
|
206 | 206 | <div class="updated"> |
207 | 207 | <p><?php esc_html_e( 'Import from GitHub started.', 'writing-on-github' ); ?></p> |
208 | 208 | </div><?php |
209 | - delete_option( '_wogh_import_started' ); |
|
210 | - } |
|
209 | + delete_option( '_wogh_import_started' ); |
|
210 | + } |
|
211 | 211 | |
212 | - if ( $message = get_option( '_wogh_import_error' ) ) { ?> |
|
212 | + if ( $message = get_option( '_wogh_import_error' ) ) { ?> |
|
213 | 213 | <div class="error"> |
214 | 214 | <p><?php esc_html_e( 'Import from GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p> |
215 | 215 | </div><?php |
216 | - delete_option( '_wogh_import_error' ); |
|
217 | - } |
|
216 | + delete_option( '_wogh_import_error' ); |
|
217 | + } |
|
218 | 218 | |
219 | - if ( 'yes' === get_option( '_wogh_import_complete' ) ) { ?> |
|
219 | + if ( 'yes' === get_option( '_wogh_import_complete' ) ) { ?> |
|
220 | 220 | <div class="updated"> |
221 | 221 | <p><?php esc_html_e( 'Import from GitHub completed successfully.', 'writing-on-github' );?></p> |
222 | 222 | </div><?php |
223 | - delete_option( '_wogh_import_complete' ); |
|
224 | - } |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Add options menu to admin navbar |
|
229 | - */ |
|
230 | - public function add_admin_menu() { |
|
231 | - add_options_page( |
|
232 | - __( 'Writing On GitHub', 'writing-on-github' ), |
|
233 | - __( 'Writing On GitHub', 'writing-on-github' ), |
|
234 | - 'manage_options', |
|
235 | - Writing_On_GitHub::$text_domain, |
|
236 | - array( $this, 'settings_page' ) |
|
237 | - ); |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * Admin callback to trigger import/export because WordPress admin routing lol |
|
242 | - */ |
|
243 | - public function trigger_cron() { |
|
244 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
245 | - return; |
|
246 | - } |
|
247 | - |
|
248 | - if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { |
|
249 | - return; |
|
250 | - } |
|
251 | - |
|
252 | - if ( ! isset( $_GET['action'] ) ) { |
|
253 | - return; |
|
254 | - } |
|
255 | - |
|
256 | - if ( 'export' === $_GET['action'] ) { |
|
257 | - Writing_On_GitHub::$instance->start_export(); |
|
258 | - } |
|
259 | - if ( 'force_export' === $_GET['action'] ) { |
|
260 | - Writing_On_GitHub::$instance->start_export(true); |
|
261 | - } |
|
262 | - if ( 'import' === $_GET['action'] ) { |
|
263 | - Writing_On_GitHub::$instance->start_import(); |
|
264 | - } |
|
265 | - if ( 'force_import' === $_GET['action'] ) { |
|
266 | - Writing_On_GitHub::$instance->start_import(true); |
|
267 | - } |
|
268 | - |
|
269 | - wp_redirect( admin_url( 'options-general.php?page=writing-on-github' ) ); |
|
270 | - die; |
|
271 | - } |
|
223 | + delete_option( '_wogh_import_complete' ); |
|
224 | + } |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Add options menu to admin navbar |
|
229 | + */ |
|
230 | + public function add_admin_menu() { |
|
231 | + add_options_page( |
|
232 | + __( 'Writing On GitHub', 'writing-on-github' ), |
|
233 | + __( 'Writing On GitHub', 'writing-on-github' ), |
|
234 | + 'manage_options', |
|
235 | + Writing_On_GitHub::$text_domain, |
|
236 | + array( $this, 'settings_page' ) |
|
237 | + ); |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * Admin callback to trigger import/export because WordPress admin routing lol |
|
242 | + */ |
|
243 | + public function trigger_cron() { |
|
244 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
245 | + return; |
|
246 | + } |
|
247 | + |
|
248 | + if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { |
|
249 | + return; |
|
250 | + } |
|
251 | + |
|
252 | + if ( ! isset( $_GET['action'] ) ) { |
|
253 | + return; |
|
254 | + } |
|
255 | + |
|
256 | + if ( 'export' === $_GET['action'] ) { |
|
257 | + Writing_On_GitHub::$instance->start_export(); |
|
258 | + } |
|
259 | + if ( 'force_export' === $_GET['action'] ) { |
|
260 | + Writing_On_GitHub::$instance->start_export(true); |
|
261 | + } |
|
262 | + if ( 'import' === $_GET['action'] ) { |
|
263 | + Writing_On_GitHub::$instance->start_import(); |
|
264 | + } |
|
265 | + if ( 'force_import' === $_GET['action'] ) { |
|
266 | + Writing_On_GitHub::$instance->start_import(true); |
|
267 | + } |
|
268 | + |
|
269 | + wp_redirect( admin_url( 'options-general.php?page=writing-on-github' ) ); |
|
270 | + die; |
|
271 | + } |
|
272 | 272 | } |
@@ -18,13 +18,13 @@ discard block |
||
18 | 18 | /** |
19 | 19 | * Hook into GitHub API |
20 | 20 | */ |
21 | - public function __construct( $plugin_file ) { |
|
21 | + public function __construct($plugin_file) { |
|
22 | 22 | $this->plugin_file = $plugin_file; |
23 | 23 | |
24 | - add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); |
|
25 | - add_action( 'admin_init', array( $this, 'register_settings' ) ); |
|
26 | - add_action( 'current_screen', array( $this, 'trigger_cron' ) ); |
|
27 | - add_filter( 'plugin_action_links', array($this, 'settings_links'), 10, 2 ); |
|
24 | + add_action('admin_menu', array($this, 'add_admin_menu')); |
|
25 | + add_action('admin_init', array($this, 'register_settings')); |
|
26 | + add_action('current_screen', array($this, 'trigger_cron')); |
|
27 | + add_filter('plugin_action_links', array($this, 'settings_links'), 10, 2); |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | /** |
@@ -33,15 +33,15 @@ discard block |
||
33 | 33 | * @param string $file |
34 | 34 | * @return string[] |
35 | 35 | */ |
36 | - public function settings_links( $links, $file ) { |
|
37 | - if ( $file != $this->plugin_file ) { |
|
36 | + public function settings_links($links, $file) { |
|
37 | + if ($file != $this->plugin_file) { |
|
38 | 38 | return $links; |
39 | 39 | } |
40 | 40 | |
41 | 41 | $settings_link = '<a href="options-general.php?page=' . |
42 | - Writing_On_GitHub::$text_domain . '">' . __( 'Settings', 'writing-on-github' ) . '</a>'; |
|
42 | + Writing_On_GitHub::$text_domain . '">' . __('Settings', 'writing-on-github') . '</a>'; |
|
43 | 43 | |
44 | - array_push( $links, $settings_link ); |
|
44 | + array_push($links, $settings_link); |
|
45 | 45 | |
46 | 46 | return $links; |
47 | 47 | } |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | * Callback to render the settings page view |
51 | 51 | */ |
52 | 52 | public function settings_page() { |
53 | - include dirname( dirname( __FILE__ ) ) . '/views/options.php'; |
|
53 | + include dirname(dirname(__FILE__)) . '/views/options.php'; |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
@@ -60,71 +60,71 @@ discard block |
||
60 | 60 | add_settings_section( |
61 | 61 | 'general', |
62 | 62 | 'General Settings', |
63 | - array( $this, 'section_callback' ), |
|
63 | + array($this, 'section_callback'), |
|
64 | 64 | Writing_On_GitHub::$text_domain |
65 | 65 | ); |
66 | 66 | |
67 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_host' ); |
|
68 | - add_settings_field( 'wogh_host', __( 'GitHub hostname', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
67 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_host'); |
|
68 | + add_settings_field('wogh_host', __('GitHub hostname', 'writing-on-github'), array($this, 'field_callback'), Writing_On_GitHub::$text_domain, 'general', array( |
|
69 | 69 | 'default' => 'https://api.github.com', |
70 | 70 | 'name' => 'wogh_host', |
71 | - 'help_text' => __( 'The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'writing-on-github' ), |
|
71 | + 'help_text' => __('The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'writing-on-github'), |
|
72 | 72 | ) |
73 | 73 | ); |
74 | 74 | |
75 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_repository' ); |
|
76 | - add_settings_field( 'wogh_repository', __( 'Repository', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
75 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_repository'); |
|
76 | + add_settings_field('wogh_repository', __('Repository', 'writing-on-github'), array($this, 'field_callback'), Writing_On_GitHub::$text_domain, 'general', array( |
|
77 | 77 | 'default' => '', |
78 | 78 | 'name' => 'wogh_repository', |
79 | - 'help_text' => __( 'The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>github/hubot.github.com</code>. The repository should contain an initial commit, which is satisfied by including a README when you create the repository on GitHub.', 'writing-on-github' ), |
|
79 | + 'help_text' => __('The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>github/hubot.github.com</code>. The repository should contain an initial commit, which is satisfied by including a README when you create the repository on GitHub.', 'writing-on-github'), |
|
80 | 80 | ) |
81 | 81 | ); |
82 | 82 | |
83 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_branch' ); |
|
84 | - add_settings_field( 'wogh_branch', __( 'Branch', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
83 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_branch'); |
|
84 | + add_settings_field('wogh_branch', __('Branch', 'writing-on-github'), array($this, 'field_callback'), Writing_On_GitHub::$text_domain, 'general', array( |
|
85 | 85 | 'default' => 'master', |
86 | 86 | 'name' => 'wogh_branch', |
87 | - 'help_text' => __( 'The GitHub branch to commit to, default is master.', 'writing-on-github' ), |
|
87 | + 'help_text' => __('The GitHub branch to commit to, default is master.', 'writing-on-github'), |
|
88 | 88 | ) |
89 | 89 | ); |
90 | 90 | |
91 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_oauth_token' ); |
|
92 | - add_settings_field( 'wogh_oauth_token', __( 'Oauth Token', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
91 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_oauth_token'); |
|
92 | + add_settings_field('wogh_oauth_token', __('Oauth Token', 'writing-on-github'), array($this, 'field_callback'), Writing_On_GitHub::$text_domain, 'general', array( |
|
93 | 93 | 'default' => '', |
94 | 94 | 'name' => 'wogh_oauth_token', |
95 | - 'help_text' => __( "A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'writing-on-github' ), |
|
95 | + 'help_text' => __("A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'writing-on-github'), |
|
96 | 96 | ) |
97 | 97 | ); |
98 | 98 | |
99 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_secret' ); |
|
100 | - add_settings_field( 'wogh_secret', __( 'Webhook Secret', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
99 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_secret'); |
|
100 | + add_settings_field('wogh_secret', __('Webhook Secret', 'writing-on-github'), array($this, 'field_callback'), Writing_On_GitHub::$text_domain, 'general', array( |
|
101 | 101 | 'default' => '', |
102 | 102 | 'name' => 'wogh_secret', |
103 | - 'help_text' => __( "The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'writing-on-github' ), |
|
103 | + 'help_text' => __("The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'writing-on-github'), |
|
104 | 104 | ) |
105 | 105 | ); |
106 | 106 | |
107 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_default_user' ); |
|
108 | - add_settings_field( 'wogh_default_user', __( 'Default Import User', 'writing-on-github' ), array( &$this, 'user_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
107 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_default_user'); |
|
108 | + add_settings_field('wogh_default_user', __('Default Import User', 'writing-on-github'), array(&$this, 'user_field_callback'), Writing_On_GitHub::$text_domain, 'general', array( |
|
109 | 109 | 'default' => '', |
110 | 110 | 'name' => 'wogh_default_user', |
111 | - 'help_text' => __( 'The fallback user for import, in case Writing On GitHub cannot find the committer in the database.', 'writing-on-github' ), |
|
111 | + 'help_text' => __('The fallback user for import, in case Writing On GitHub cannot find the committer in the database.', 'writing-on-github'), |
|
112 | 112 | ) |
113 | 113 | ); |
114 | 114 | |
115 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_author' ); |
|
116 | - add_settings_field( 'wogh_ignore_author', __( 'Ignore author', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
115 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_ignore_author'); |
|
116 | + add_settings_field('wogh_ignore_author', __('Ignore author', 'writing-on-github'), array(&$this, 'checkbox_field_callback'), Writing_On_GitHub::$text_domain, 'general', array( |
|
117 | 117 | 'default' => '', |
118 | 118 | 'name' => 'wogh_ignore_author', |
119 | - 'help_text' => __( 'Do not export author and do not use author info from GitHub.', 'writing-on-github' ), |
|
119 | + 'help_text' => __('Do not export author and do not use author info from GitHub.', 'writing-on-github'), |
|
120 | 120 | ) |
121 | 121 | ); |
122 | 122 | |
123 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_dont_export_content' ); |
|
124 | - add_settings_field( 'wogh_dont_export_content', __( 'Don\'t export content', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
123 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_dont_export_content'); |
|
124 | + add_settings_field('wogh_dont_export_content', __('Don\'t export content', 'writing-on-github'), array(&$this, 'checkbox_field_callback'), Writing_On_GitHub::$text_domain, 'general', array( |
|
125 | 125 | 'default' => '', |
126 | 126 | 'name' => 'wogh_dont_export_content', |
127 | - 'help_text' => __( 'Do not export post content to github, only export meta.', 'writing-on-github' ), |
|
127 | + 'help_text' => __('Do not export post content to github, only export meta.', 'writing-on-github'), |
|
128 | 128 | ) |
129 | 129 | ); |
130 | 130 | |
@@ -142,8 +142,8 @@ discard block |
||
142 | 142 | * |
143 | 143 | * @param array $args Field arguments. |
144 | 144 | */ |
145 | - public function field_callback( $args ) { |
|
146 | - include dirname( dirname( __FILE__ ) ) . '/views/setting-field.php'; |
|
145 | + public function field_callback($args) { |
|
146 | + include dirname(dirname(__FILE__)) . '/views/setting-field.php'; |
|
147 | 147 | } |
148 | 148 | |
149 | 149 | /** |
@@ -151,8 +151,8 @@ discard block |
||
151 | 151 | * |
152 | 152 | * @param array $args Field arguments. |
153 | 153 | */ |
154 | - public function user_field_callback( $args ) { |
|
155 | - include dirname( dirname( __FILE__ ) ) . '/views/user-setting-field.php'; |
|
154 | + public function user_field_callback($args) { |
|
155 | + include dirname(dirname(__FILE__)) . '/views/user-setting-field.php'; |
|
156 | 156 | } |
157 | 157 | |
158 | 158 | /** |
@@ -160,8 +160,8 @@ discard block |
||
160 | 160 | * |
161 | 161 | * @param array $args Field arguments. |
162 | 162 | */ |
163 | - public function textarea_field_callback( $args ) { |
|
164 | - include dirname( dirname( __FILE__ ) ) . '/views/textarea-setting-field.php'; |
|
163 | + public function textarea_field_callback($args) { |
|
164 | + include dirname(dirname(__FILE__)) . '/views/textarea-setting-field.php'; |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | /** |
@@ -169,58 +169,58 @@ discard block |
||
169 | 169 | * |
170 | 170 | * @param array $args Field arguments. |
171 | 171 | */ |
172 | - public function checkbox_field_callback( $args ) { |
|
173 | - include dirname( dirname( __FILE__ ) ) . '/views/checkbox-setting-field.php'; |
|
172 | + public function checkbox_field_callback($args) { |
|
173 | + include dirname(dirname(__FILE__)) . '/views/checkbox-setting-field.php'; |
|
174 | 174 | } |
175 | 175 | |
176 | 176 | /** |
177 | 177 | * Displays settings messages from background processes |
178 | 178 | */ |
179 | 179 | public function section_callback() { |
180 | - if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { |
|
180 | + if (get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain) { |
|
181 | 181 | return; |
182 | 182 | } |
183 | 183 | |
184 | - if ( 'yes' === get_option( '_wogh_export_started' ) ) { ?> |
|
184 | + if ('yes' === get_option('_wogh_export_started')) { ?> |
|
185 | 185 | <div class="updated"> |
186 | - <p><?php esc_html_e( 'Export to GitHub started.', 'writing-on-github' ); ?></p> |
|
186 | + <p><?php esc_html_e('Export to GitHub started.', 'writing-on-github'); ?></p> |
|
187 | 187 | </div><?php |
188 | - delete_option( '_wogh_export_started' ); |
|
188 | + delete_option('_wogh_export_started'); |
|
189 | 189 | } |
190 | 190 | |
191 | - if ( $message = get_option( '_wogh_export_error' ) ) { ?> |
|
191 | + if ($message = get_option('_wogh_export_error')) { ?> |
|
192 | 192 | <div class="error"> |
193 | - <p><?php esc_html_e( 'Export to GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p> |
|
193 | + <p><?php esc_html_e('Export to GitHub failed with error:', 'writing-on-github'); ?> <?php echo esc_html($message); ?></p> |
|
194 | 194 | </div><?php |
195 | - delete_option( '_wogh_export_error' ); |
|
195 | + delete_option('_wogh_export_error'); |
|
196 | 196 | } |
197 | 197 | |
198 | - if ( 'yes' === get_option( '_wogh_export_complete' ) ) { ?> |
|
198 | + if ('yes' === get_option('_wogh_export_complete')) { ?> |
|
199 | 199 | <div class="updated"> |
200 | - <p><?php esc_html_e( 'Export to GitHub completed successfully.', 'writing-on-github' );?></p> |
|
200 | + <p><?php esc_html_e('Export to GitHub completed successfully.', 'writing-on-github'); ?></p> |
|
201 | 201 | </div><?php |
202 | - delete_option( '_wogh_export_complete' ); |
|
202 | + delete_option('_wogh_export_complete'); |
|
203 | 203 | } |
204 | 204 | |
205 | - if ( 'yes' === get_option( '_wogh_import_started' ) ) { ?> |
|
205 | + if ('yes' === get_option('_wogh_import_started')) { ?> |
|
206 | 206 | <div class="updated"> |
207 | - <p><?php esc_html_e( 'Import from GitHub started.', 'writing-on-github' ); ?></p> |
|
207 | + <p><?php esc_html_e('Import from GitHub started.', 'writing-on-github'); ?></p> |
|
208 | 208 | </div><?php |
209 | - delete_option( '_wogh_import_started' ); |
|
209 | + delete_option('_wogh_import_started'); |
|
210 | 210 | } |
211 | 211 | |
212 | - if ( $message = get_option( '_wogh_import_error' ) ) { ?> |
|
212 | + if ($message = get_option('_wogh_import_error')) { ?> |
|
213 | 213 | <div class="error"> |
214 | - <p><?php esc_html_e( 'Import from GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p> |
|
214 | + <p><?php esc_html_e('Import from GitHub failed with error:', 'writing-on-github'); ?> <?php echo esc_html($message); ?></p> |
|
215 | 215 | </div><?php |
216 | - delete_option( '_wogh_import_error' ); |
|
216 | + delete_option('_wogh_import_error'); |
|
217 | 217 | } |
218 | 218 | |
219 | - if ( 'yes' === get_option( '_wogh_import_complete' ) ) { ?> |
|
219 | + if ('yes' === get_option('_wogh_import_complete')) { ?> |
|
220 | 220 | <div class="updated"> |
221 | - <p><?php esc_html_e( 'Import from GitHub completed successfully.', 'writing-on-github' );?></p> |
|
221 | + <p><?php esc_html_e('Import from GitHub completed successfully.', 'writing-on-github'); ?></p> |
|
222 | 222 | </div><?php |
223 | - delete_option( '_wogh_import_complete' ); |
|
223 | + delete_option('_wogh_import_complete'); |
|
224 | 224 | } |
225 | 225 | } |
226 | 226 | |
@@ -229,11 +229,11 @@ discard block |
||
229 | 229 | */ |
230 | 230 | public function add_admin_menu() { |
231 | 231 | add_options_page( |
232 | - __( 'Writing On GitHub', 'writing-on-github' ), |
|
233 | - __( 'Writing On GitHub', 'writing-on-github' ), |
|
232 | + __('Writing On GitHub', 'writing-on-github'), |
|
233 | + __('Writing On GitHub', 'writing-on-github'), |
|
234 | 234 | 'manage_options', |
235 | 235 | Writing_On_GitHub::$text_domain, |
236 | - array( $this, 'settings_page' ) |
|
236 | + array($this, 'settings_page') |
|
237 | 237 | ); |
238 | 238 | } |
239 | 239 | |
@@ -241,32 +241,32 @@ discard block |
||
241 | 241 | * Admin callback to trigger import/export because WordPress admin routing lol |
242 | 242 | */ |
243 | 243 | public function trigger_cron() { |
244 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
244 | + if ( ! current_user_can('manage_options')) { |
|
245 | 245 | return; |
246 | 246 | } |
247 | 247 | |
248 | - if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { |
|
248 | + if (get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain) { |
|
249 | 249 | return; |
250 | 250 | } |
251 | 251 | |
252 | - if ( ! isset( $_GET['action'] ) ) { |
|
252 | + if ( ! isset($_GET['action'])) { |
|
253 | 253 | return; |
254 | 254 | } |
255 | 255 | |
256 | - if ( 'export' === $_GET['action'] ) { |
|
256 | + if ('export' === $_GET['action']) { |
|
257 | 257 | Writing_On_GitHub::$instance->start_export(); |
258 | 258 | } |
259 | - if ( 'force_export' === $_GET['action'] ) { |
|
259 | + if ('force_export' === $_GET['action']) { |
|
260 | 260 | Writing_On_GitHub::$instance->start_export(true); |
261 | 261 | } |
262 | - if ( 'import' === $_GET['action'] ) { |
|
262 | + if ('import' === $_GET['action']) { |
|
263 | 263 | Writing_On_GitHub::$instance->start_import(); |
264 | 264 | } |
265 | - if ( 'force_import' === $_GET['action'] ) { |
|
265 | + if ('force_import' === $_GET['action']) { |
|
266 | 266 | Writing_On_GitHub::$instance->start_import(true); |
267 | 267 | } |
268 | 268 | |
269 | - wp_redirect( admin_url( 'options-general.php?page=writing-on-github' ) ); |
|
269 | + wp_redirect(admin_url('options-general.php?page=writing-on-github')); |
|
270 | 270 | die; |
271 | 271 | } |
272 | 272 | } |
@@ -6,35 +6,35 @@ |
||
6 | 6 | |
7 | 7 | ?> |
8 | 8 | <div class="wrap"> |
9 | - <h2><?php esc_html_e( 'Writing On GitHub', 'writing-on-github' ); ?></h2> |
|
9 | + <h2><?php esc_html_e('Writing On GitHub', 'writing-on-github'); ?></h2> |
|
10 | 10 | |
11 | 11 | <form method="post" action="options.php"> |
12 | - <?php settings_fields( Writing_On_GitHub::$text_domain ); ?> |
|
13 | - <?php do_settings_sections( Writing_On_GitHub::$text_domain ); ?> |
|
12 | + <?php settings_fields(Writing_On_GitHub::$text_domain); ?> |
|
13 | + <?php do_settings_sections(Writing_On_GitHub::$text_domain); ?> |
|
14 | 14 | <table class="form-table"> |
15 | 15 | <tr> |
16 | - <th scope="row"><?php esc_html_e( 'Webhook callback', 'writing-on-github' ); ?></th> |
|
17 | - <td><code><?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>?action=wogh_push_request</code></td> |
|
16 | + <th scope="row"><?php esc_html_e('Webhook callback', 'writing-on-github'); ?></th> |
|
17 | + <td><code><?php echo esc_url(admin_url('admin-ajax.php')); ?>?action=wogh_push_request</code></td> |
|
18 | 18 | </tr> |
19 | 19 | <tr> |
20 | - <th scope="row"><?php esc_html_e( 'Export', 'writing-on-github' ); ?></th> |
|
20 | + <th scope="row"><?php esc_html_e('Export', 'writing-on-github'); ?></th> |
|
21 | 21 | <td> |
22 | - <p><a href="<?php echo esc_url( add_query_arg( array( 'action' => 'export' ) ) ); ?>"> |
|
23 | - <input type="button" class="button button-secondary" value="<?php esc_html_e( 'Export to GitHub', 'writing-on-github' ); ?>" /> |
|
22 | + <p><a href="<?php echo esc_url(add_query_arg(array('action' => 'export'))); ?>"> |
|
23 | + <input type="button" class="button button-secondary" value="<?php esc_html_e('Export to GitHub', 'writing-on-github'); ?>" /> |
|
24 | 24 | </a></p> |
25 | - <p><a href="<?php echo esc_url( add_query_arg( array( 'action' => 'force_export' ) ) ); ?>"> |
|
26 | - <input type="button" class="button button-secondary" value="<?php esc_html_e( 'Force export to GitHub', 'writing-on-github' ); ?>" /> |
|
25 | + <p><a href="<?php echo esc_url(add_query_arg(array('action' => 'force_export'))); ?>"> |
|
26 | + <input type="button" class="button button-secondary" value="<?php esc_html_e('Force export to GitHub', 'writing-on-github'); ?>" /> |
|
27 | 27 | </a></p> |
28 | 28 | </td> |
29 | 29 | </tr> |
30 | 30 | <tr> |
31 | - <th scope="row"><?php esc_html_e( 'Import', 'writing-on-github' ); ?></th> |
|
31 | + <th scope="row"><?php esc_html_e('Import', 'writing-on-github'); ?></th> |
|
32 | 32 | <td> |
33 | - <p><a href="<?php echo esc_url( add_query_arg( array( 'action' => 'import' ) ) ); ?>"> |
|
34 | - <input type="button" class="button button-secondary" value="<?php esc_html_e( 'Import from GitHub', 'writing-on-github' ); ?>" /> |
|
33 | + <p><a href="<?php echo esc_url(add_query_arg(array('action' => 'import'))); ?>"> |
|
34 | + <input type="button" class="button button-secondary" value="<?php esc_html_e('Import from GitHub', 'writing-on-github'); ?>" /> |
|
35 | 35 | </a></p> |
36 | - <p><a href="<?php echo esc_url( add_query_arg( array( 'action' => 'force_import' ) ) ); ?>"> |
|
37 | - <input type="button" class="button button-secondary" value="<?php esc_html_e( 'Force Import from GitHub', 'writing-on-github' ); ?>" /> |
|
36 | + <p><a href="<?php echo esc_url(add_query_arg(array('action' => 'force_import'))); ?>"> |
|
37 | + <input type="button" class="button button-secondary" value="<?php esc_html_e('Force Import from GitHub', 'writing-on-github'); ?>" /> |
|
38 | 38 | </a></p> |
39 | 39 | </td> |
40 | 40 | </tr> |
@@ -9,134 +9,134 @@ |
||
9 | 9 | */ |
10 | 10 | class Writing_On_GitHub_Payload { |
11 | 11 | |
12 | - /** |
|
13 | - * Application container. |
|
14 | - * |
|
15 | - * @var Writing_On_GitHub |
|
16 | - */ |
|
17 | - protected $app; |
|
18 | - |
|
19 | - /** |
|
20 | - * Payload data. |
|
21 | - * |
|
22 | - * @var stdClass |
|
23 | - */ |
|
24 | - protected $data; |
|
25 | - |
|
26 | - /** |
|
27 | - * Writing_On_GitHub_Payload constructor. |
|
28 | - * |
|
29 | - * @param Writing_On_GitHub $app Application container. |
|
30 | - * @param string $raw_data Raw request data. |
|
31 | - */ |
|
32 | - public function __construct( Writing_On_GitHub $app, $raw_data ) { |
|
33 | - $this->app = $app; |
|
34 | - $this->data = json_decode( $raw_data ); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Returns whether payload should be imported. |
|
39 | - * |
|
40 | - * @return true|WP_Error |
|
41 | - */ |
|
42 | - public function should_import() { |
|
43 | - // @todo how do we get this without importing the whole api object just for this? |
|
44 | - if ( strtolower( $this->data->repository->full_name ) !== strtolower( $this->app->api()->fetch()->repository() ) ) { |
|
45 | - return new WP_Error( |
|
46 | - 'incorrect_repository', |
|
47 | - sprintf( 'Incorrect repository, %s -> %s .', |
|
48 | - $this->data->repository->full_name, |
|
49 | - $this->app->api()->fetch()->repository() |
|
50 | - ) |
|
51 | - ); |
|
52 | - } |
|
53 | - |
|
54 | - // The last term in the ref is the payload_branch name. |
|
55 | - $refs = explode( '/', $this->data->ref ); |
|
56 | - $payload_branch = array_pop( $refs ); |
|
57 | - |
|
58 | - $branch = $this->app->api()->fetch()->branch(); |
|
59 | - |
|
60 | - if ( $branch !== $payload_branch ) { |
|
61 | - return new WP_Error( |
|
62 | - 'incorrect_branch', |
|
63 | - sprintf( 'Incorrect branch, %s -> %s .', |
|
64 | - $payload_branch, |
|
65 | - $branch |
|
66 | - ) |
|
67 | - ); |
|
68 | - } |
|
69 | - |
|
70 | - // We add a tag to commits we push out, so we shouldn't pull them in again. |
|
71 | - $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
72 | - |
|
73 | - if ( ! $tag ) { |
|
74 | - throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
75 | - } |
|
76 | - |
|
77 | - if ( $tag === substr( $this->message(), -1 * strlen( $tag ) ) ) { |
|
78 | - return new WP_Error( |
|
79 | - 'skip_import', |
|
80 | - 'Skip import on auto export post.' |
|
81 | - ); |
|
82 | - } |
|
83 | - |
|
84 | - if ( ! $this->get_commit_id() ) { |
|
85 | - return new WP_Error( |
|
86 | - 'invalid_payload', |
|
87 | - "[Missing Commit ID] won't be imported." |
|
88 | - ); |
|
89 | - } |
|
90 | - |
|
91 | - return true; |
|
92 | - } |
|
93 | - |
|
94 | - public function get_before_commit_id() { |
|
95 | - return $this->data->before ? $this->data->before : null; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Returns the sha of the head commit. |
|
100 | - * |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - public function get_commit_id() { |
|
104 | - return $this->data->head_commit ? $this->data->head_commit->id : null; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Returns the email address for the commit author. |
|
109 | - * |
|
110 | - * @return string |
|
111 | - */ |
|
112 | - public function get_author_email() { |
|
113 | - return $this->data->head_commit->author->email; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Returns array commits for the payload. |
|
118 | - * |
|
119 | - * @return array |
|
120 | - */ |
|
121 | - public function get_commits() { |
|
122 | - return $this->data->commits; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Returns the repository's full name. |
|
127 | - * |
|
128 | - * @return string |
|
129 | - */ |
|
130 | - public function get_repository_name() { |
|
131 | - return $this->data->repository->full_name; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Returns the payload's commit message. |
|
136 | - * |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - protected function message() { |
|
140 | - return $this->data->head_commit->message; |
|
141 | - } |
|
12 | + /** |
|
13 | + * Application container. |
|
14 | + * |
|
15 | + * @var Writing_On_GitHub |
|
16 | + */ |
|
17 | + protected $app; |
|
18 | + |
|
19 | + /** |
|
20 | + * Payload data. |
|
21 | + * |
|
22 | + * @var stdClass |
|
23 | + */ |
|
24 | + protected $data; |
|
25 | + |
|
26 | + /** |
|
27 | + * Writing_On_GitHub_Payload constructor. |
|
28 | + * |
|
29 | + * @param Writing_On_GitHub $app Application container. |
|
30 | + * @param string $raw_data Raw request data. |
|
31 | + */ |
|
32 | + public function __construct( Writing_On_GitHub $app, $raw_data ) { |
|
33 | + $this->app = $app; |
|
34 | + $this->data = json_decode( $raw_data ); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Returns whether payload should be imported. |
|
39 | + * |
|
40 | + * @return true|WP_Error |
|
41 | + */ |
|
42 | + public function should_import() { |
|
43 | + // @todo how do we get this without importing the whole api object just for this? |
|
44 | + if ( strtolower( $this->data->repository->full_name ) !== strtolower( $this->app->api()->fetch()->repository() ) ) { |
|
45 | + return new WP_Error( |
|
46 | + 'incorrect_repository', |
|
47 | + sprintf( 'Incorrect repository, %s -> %s .', |
|
48 | + $this->data->repository->full_name, |
|
49 | + $this->app->api()->fetch()->repository() |
|
50 | + ) |
|
51 | + ); |
|
52 | + } |
|
53 | + |
|
54 | + // The last term in the ref is the payload_branch name. |
|
55 | + $refs = explode( '/', $this->data->ref ); |
|
56 | + $payload_branch = array_pop( $refs ); |
|
57 | + |
|
58 | + $branch = $this->app->api()->fetch()->branch(); |
|
59 | + |
|
60 | + if ( $branch !== $payload_branch ) { |
|
61 | + return new WP_Error( |
|
62 | + 'incorrect_branch', |
|
63 | + sprintf( 'Incorrect branch, %s -> %s .', |
|
64 | + $payload_branch, |
|
65 | + $branch |
|
66 | + ) |
|
67 | + ); |
|
68 | + } |
|
69 | + |
|
70 | + // We add a tag to commits we push out, so we shouldn't pull them in again. |
|
71 | + $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
72 | + |
|
73 | + if ( ! $tag ) { |
|
74 | + throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
75 | + } |
|
76 | + |
|
77 | + if ( $tag === substr( $this->message(), -1 * strlen( $tag ) ) ) { |
|
78 | + return new WP_Error( |
|
79 | + 'skip_import', |
|
80 | + 'Skip import on auto export post.' |
|
81 | + ); |
|
82 | + } |
|
83 | + |
|
84 | + if ( ! $this->get_commit_id() ) { |
|
85 | + return new WP_Error( |
|
86 | + 'invalid_payload', |
|
87 | + "[Missing Commit ID] won't be imported." |
|
88 | + ); |
|
89 | + } |
|
90 | + |
|
91 | + return true; |
|
92 | + } |
|
93 | + |
|
94 | + public function get_before_commit_id() { |
|
95 | + return $this->data->before ? $this->data->before : null; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Returns the sha of the head commit. |
|
100 | + * |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + public function get_commit_id() { |
|
104 | + return $this->data->head_commit ? $this->data->head_commit->id : null; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Returns the email address for the commit author. |
|
109 | + * |
|
110 | + * @return string |
|
111 | + */ |
|
112 | + public function get_author_email() { |
|
113 | + return $this->data->head_commit->author->email; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Returns array commits for the payload. |
|
118 | + * |
|
119 | + * @return array |
|
120 | + */ |
|
121 | + public function get_commits() { |
|
122 | + return $this->data->commits; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Returns the repository's full name. |
|
127 | + * |
|
128 | + * @return string |
|
129 | + */ |
|
130 | + public function get_repository_name() { |
|
131 | + return $this->data->repository->full_name; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Returns the payload's commit message. |
|
136 | + * |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + protected function message() { |
|
140 | + return $this->data->head_commit->message; |
|
141 | + } |
|
142 | 142 | } |
@@ -29,9 +29,9 @@ discard block |
||
29 | 29 | * @param Writing_On_GitHub $app Application container. |
30 | 30 | * @param string $raw_data Raw request data. |
31 | 31 | */ |
32 | - public function __construct( Writing_On_GitHub $app, $raw_data ) { |
|
32 | + public function __construct(Writing_On_GitHub $app, $raw_data) { |
|
33 | 33 | $this->app = $app; |
34 | - $this->data = json_decode( $raw_data ); |
|
34 | + $this->data = json_decode($raw_data); |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | /** |
@@ -41,10 +41,10 @@ discard block |
||
41 | 41 | */ |
42 | 42 | public function should_import() { |
43 | 43 | // @todo how do we get this without importing the whole api object just for this? |
44 | - if ( strtolower( $this->data->repository->full_name ) !== strtolower( $this->app->api()->fetch()->repository() ) ) { |
|
44 | + if (strtolower($this->data->repository->full_name) !== strtolower($this->app->api()->fetch()->repository())) { |
|
45 | 45 | return new WP_Error( |
46 | 46 | 'incorrect_repository', |
47 | - sprintf( 'Incorrect repository, %s -> %s .', |
|
47 | + sprintf('Incorrect repository, %s -> %s .', |
|
48 | 48 | $this->data->repository->full_name, |
49 | 49 | $this->app->api()->fetch()->repository() |
50 | 50 | ) |
@@ -52,15 +52,15 @@ discard block |
||
52 | 52 | } |
53 | 53 | |
54 | 54 | // The last term in the ref is the payload_branch name. |
55 | - $refs = explode( '/', $this->data->ref ); |
|
56 | - $payload_branch = array_pop( $refs ); |
|
55 | + $refs = explode('/', $this->data->ref); |
|
56 | + $payload_branch = array_pop($refs); |
|
57 | 57 | |
58 | 58 | $branch = $this->app->api()->fetch()->branch(); |
59 | 59 | |
60 | - if ( $branch !== $payload_branch ) { |
|
60 | + if ($branch !== $payload_branch) { |
|
61 | 61 | return new WP_Error( |
62 | 62 | 'incorrect_branch', |
63 | - sprintf( 'Incorrect branch, %s -> %s .', |
|
63 | + sprintf('Incorrect branch, %s -> %s .', |
|
64 | 64 | $payload_branch, |
65 | 65 | $branch |
66 | 66 | ) |
@@ -68,20 +68,20 @@ discard block |
||
68 | 68 | } |
69 | 69 | |
70 | 70 | // We add a tag to commits we push out, so we shouldn't pull them in again. |
71 | - $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
71 | + $tag = apply_filters('wogh_commit_msg_tag', 'wogh'); |
|
72 | 72 | |
73 | - if ( ! $tag ) { |
|
74 | - throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
73 | + if ( ! $tag) { |
|
74 | + throw new Exception(__('Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github')); |
|
75 | 75 | } |
76 | 76 | |
77 | - if ( $tag === substr( $this->message(), -1 * strlen( $tag ) ) ) { |
|
77 | + if ($tag === substr($this->message(), -1 * strlen($tag))) { |
|
78 | 78 | return new WP_Error( |
79 | 79 | 'skip_import', |
80 | 80 | 'Skip import on auto export post.' |
81 | 81 | ); |
82 | 82 | } |
83 | 83 | |
84 | - if ( ! $this->get_commit_id() ) { |
|
84 | + if ( ! $this->get_commit_id()) { |
|
85 | 85 | return new WP_Error( |
86 | 86 | 'invalid_payload', |
87 | 87 | "[Missing Commit ID] won't be imported." |
@@ -9,232 +9,232 @@ |
||
9 | 9 | */ |
10 | 10 | class Writing_On_GitHub_Controller { |
11 | 11 | |
12 | - /** |
|
13 | - * Application container. |
|
14 | - * |
|
15 | - * @var Writing_On_GitHub |
|
16 | - */ |
|
17 | - public $app; |
|
18 | - |
|
19 | - /** |
|
20 | - * Instantiates a new Controller object |
|
21 | - * |
|
22 | - * @param Writing_On_GitHub $app Applicatio container. |
|
23 | - */ |
|
24 | - public function __construct( Writing_On_GitHub $app ) { |
|
25 | - $this->app = $app; |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * Webhook callback as triggered from GitHub push. |
|
30 | - * |
|
31 | - * Reads the Webhook payload and syncs posts as necessary. |
|
32 | - * |
|
33 | - * @return boolean |
|
34 | - */ |
|
35 | - public function pull_posts() { |
|
36 | - $this->set_ajax(); |
|
37 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
38 | - return $this->app->response()->error( new WP_Error( |
|
39 | - 'semaphore_locked', |
|
40 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::pull_posts()' ) |
|
41 | - ) ); |
|
42 | - } |
|
43 | - |
|
44 | - if ( ! $this->app->request()->is_secret_valid() ) { |
|
45 | - return $this->app->response()->error( new WP_Error( |
|
46 | - 'invalid_headers', |
|
47 | - __( 'Failed to validate secret.', 'writing-on-github' ) |
|
48 | - ) ); |
|
49 | - } |
|
50 | - |
|
51 | - // ping |
|
52 | - if ( $this->app->request()->is_ping() ) { |
|
53 | - return $this->app->response()->success( __( 'Wordpress is ready.', 'writing-on-github' ) ); |
|
54 | - } |
|
55 | - |
|
56 | - // push |
|
57 | - if ( ! $this->app->request()->is_push() ) { |
|
58 | - return $this->app->response()->error( new WP_Error( |
|
59 | - 'invalid_headers', |
|
60 | - sprintf( 'Failed to validate webhook event: %s.', |
|
61 | - $this->app->request()->webhook_event() ) |
|
62 | - ) ); |
|
63 | - } |
|
64 | - $payload = $this->app->request()->payload(); |
|
65 | - |
|
66 | - $error = $payload->should_import(); |
|
67 | - if ( is_wp_error( $error ) ) { |
|
68 | - /* @var WP_Error $error */ |
|
69 | - return $this->app->response()->error( $error ); |
|
70 | - } |
|
71 | - |
|
72 | - $this->app->semaphore()->lock(); |
|
73 | - remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
74 | - remove_action( 'delete_post', array( $this, 'delete_post' ) ); |
|
75 | - |
|
76 | - $result = $this->app->import()->payload( $payload ); |
|
77 | - |
|
78 | - $this->app->semaphore()->unlock(); |
|
79 | - |
|
80 | - if ( is_wp_error( $result ) ) { |
|
81 | - /* @var WP_Error $result */ |
|
82 | - return $this->app->response()->error( $result ); |
|
83 | - } |
|
84 | - |
|
85 | - return $this->app->response()->success( $result ); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Imports posts from the current master branch. |
|
90 | - * @param integer $user_id |
|
91 | - * @param boolean $force |
|
92 | - * @return boolean |
|
93 | - */ |
|
94 | - public function import_master( $user_id = 0, $force = false ) { |
|
95 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
96 | - return $this->app->response()->error( new WP_Error( |
|
97 | - 'semaphore_locked', |
|
98 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::import_master()' ) |
|
99 | - ) ); |
|
100 | - } |
|
101 | - |
|
102 | - $this->app->semaphore()->lock(); |
|
103 | - remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
104 | - remove_action( 'save_post', array( $this, 'delete_post' ) ); |
|
105 | - |
|
106 | - if ( $user_id ) { |
|
107 | - wp_set_current_user( $user_id ); |
|
108 | - } |
|
109 | - |
|
110 | - $result = $this->app->import()->master( $force ); |
|
111 | - |
|
112 | - $this->app->semaphore()->unlock(); |
|
113 | - |
|
114 | - if ( is_wp_error( $result ) ) { |
|
115 | - /* @var WP_Error $result */ |
|
116 | - update_option( '_wogh_import_error', $result->get_error_message() ); |
|
117 | - |
|
118 | - return $this->app->response()->error( $result ); |
|
119 | - } |
|
120 | - |
|
121 | - update_option( '_wogh_import_complete', 'yes' ); |
|
122 | - |
|
123 | - return $this->app->response()->success( $result ); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Export all the posts in the database to GitHub. |
|
128 | - * |
|
129 | - * @param int $user_id |
|
130 | - * @param boolean $force |
|
131 | - * @return boolean |
|
132 | - */ |
|
133 | - public function export_all( $user_id = 0, $force = false ) { |
|
134 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
135 | - return $this->app->response()->error( new WP_Error( |
|
136 | - 'semaphore_locked', |
|
137 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_all()' ) |
|
138 | - ) ); |
|
139 | - } |
|
140 | - |
|
141 | - $this->app->semaphore()->lock(); |
|
142 | - |
|
143 | - if ( $user_id ) { |
|
144 | - wp_set_current_user( $user_id ); |
|
145 | - } |
|
146 | - |
|
147 | - $result = $this->app->export()->full($force); |
|
148 | - $this->app->semaphore()->unlock(); |
|
149 | - |
|
150 | - // Maybe move option updating out of this class/upgrade message display? |
|
151 | - if ( is_wp_error( $result ) ) { |
|
152 | - /* @var WP_Error $result */ |
|
153 | - update_option( '_wogh_export_error', $result->get_error_message() ); |
|
154 | - |
|
155 | - return $this->app->response()->error( $result ); |
|
156 | - } else { |
|
157 | - update_option( '_wogh_export_complete', 'yes' ); |
|
158 | - update_option( '_wogh_fully_exported', 'yes' ); |
|
159 | - |
|
160 | - return $this->app->response()->success( $result ); |
|
161 | - } |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Exports a single post to GitHub by ID. |
|
166 | - * |
|
167 | - * Called on the save_post hook. |
|
168 | - * |
|
169 | - * @param int $post_id Post ID. |
|
170 | - * |
|
171 | - * @return boolean |
|
172 | - */ |
|
173 | - public function export_post( $post_id ) { |
|
174 | - if ( wp_is_post_revision( $post_id ) ) { |
|
175 | - return; |
|
176 | - } |
|
177 | - |
|
178 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
179 | - return $this->app->response()->error( new WP_Error( |
|
180 | - 'semaphore_locked', |
|
181 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_post()' ) |
|
182 | - ) ); |
|
183 | - } |
|
184 | - |
|
185 | - $this->app->semaphore()->lock(); |
|
186 | - $result = $this->app->export()->update( $post_id ); |
|
187 | - $this->app->semaphore()->unlock(); |
|
188 | - |
|
189 | - if ( is_wp_error( $result ) ) { |
|
190 | - /* @var WP_Error $result */ |
|
191 | - return $this->app->response()->error( $result ); |
|
192 | - } |
|
193 | - |
|
194 | - return $this->app->response()->success( $result ); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Removes the post from the tree. |
|
199 | - * |
|
200 | - * Called the delete_post hook. |
|
201 | - * |
|
202 | - * @param int $post_id Post ID. |
|
203 | - * |
|
204 | - * @return boolean |
|
205 | - */ |
|
206 | - public function delete_post( $post_id ) { |
|
207 | - if ( wp_is_post_revision( $post_id ) ) { |
|
208 | - return; |
|
209 | - } |
|
210 | - |
|
211 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
212 | - return $this->app->response()->error( new WP_Error( |
|
213 | - 'semaphore_locked', |
|
214 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::delete_post()' ) |
|
215 | - ) ); |
|
216 | - } |
|
217 | - |
|
218 | - $this->app->semaphore()->lock(); |
|
219 | - $result = $this->app->export()->delete( $post_id ); |
|
220 | - $this->app->semaphore()->unlock(); |
|
221 | - |
|
222 | - if ( is_wp_error( $result ) ) { |
|
223 | - /* @var WP_Error $result */ |
|
224 | - return $this->app->response()->error( $result ); |
|
225 | - } |
|
226 | - |
|
227 | - return $this->app->response()->success( $result ); |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Indicates we're running our own AJAX hook |
|
232 | - * and thus should respond with JSON, rather |
|
233 | - * than just returning data. |
|
234 | - */ |
|
235 | - protected function set_ajax() { |
|
236 | - if ( ! defined( 'WOGH_AJAX' ) ) { |
|
237 | - define( 'WOGH_AJAX', true ); |
|
238 | - } |
|
239 | - } |
|
12 | + /** |
|
13 | + * Application container. |
|
14 | + * |
|
15 | + * @var Writing_On_GitHub |
|
16 | + */ |
|
17 | + public $app; |
|
18 | + |
|
19 | + /** |
|
20 | + * Instantiates a new Controller object |
|
21 | + * |
|
22 | + * @param Writing_On_GitHub $app Applicatio container. |
|
23 | + */ |
|
24 | + public function __construct( Writing_On_GitHub $app ) { |
|
25 | + $this->app = $app; |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * Webhook callback as triggered from GitHub push. |
|
30 | + * |
|
31 | + * Reads the Webhook payload and syncs posts as necessary. |
|
32 | + * |
|
33 | + * @return boolean |
|
34 | + */ |
|
35 | + public function pull_posts() { |
|
36 | + $this->set_ajax(); |
|
37 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
38 | + return $this->app->response()->error( new WP_Error( |
|
39 | + 'semaphore_locked', |
|
40 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::pull_posts()' ) |
|
41 | + ) ); |
|
42 | + } |
|
43 | + |
|
44 | + if ( ! $this->app->request()->is_secret_valid() ) { |
|
45 | + return $this->app->response()->error( new WP_Error( |
|
46 | + 'invalid_headers', |
|
47 | + __( 'Failed to validate secret.', 'writing-on-github' ) |
|
48 | + ) ); |
|
49 | + } |
|
50 | + |
|
51 | + // ping |
|
52 | + if ( $this->app->request()->is_ping() ) { |
|
53 | + return $this->app->response()->success( __( 'Wordpress is ready.', 'writing-on-github' ) ); |
|
54 | + } |
|
55 | + |
|
56 | + // push |
|
57 | + if ( ! $this->app->request()->is_push() ) { |
|
58 | + return $this->app->response()->error( new WP_Error( |
|
59 | + 'invalid_headers', |
|
60 | + sprintf( 'Failed to validate webhook event: %s.', |
|
61 | + $this->app->request()->webhook_event() ) |
|
62 | + ) ); |
|
63 | + } |
|
64 | + $payload = $this->app->request()->payload(); |
|
65 | + |
|
66 | + $error = $payload->should_import(); |
|
67 | + if ( is_wp_error( $error ) ) { |
|
68 | + /* @var WP_Error $error */ |
|
69 | + return $this->app->response()->error( $error ); |
|
70 | + } |
|
71 | + |
|
72 | + $this->app->semaphore()->lock(); |
|
73 | + remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
74 | + remove_action( 'delete_post', array( $this, 'delete_post' ) ); |
|
75 | + |
|
76 | + $result = $this->app->import()->payload( $payload ); |
|
77 | + |
|
78 | + $this->app->semaphore()->unlock(); |
|
79 | + |
|
80 | + if ( is_wp_error( $result ) ) { |
|
81 | + /* @var WP_Error $result */ |
|
82 | + return $this->app->response()->error( $result ); |
|
83 | + } |
|
84 | + |
|
85 | + return $this->app->response()->success( $result ); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Imports posts from the current master branch. |
|
90 | + * @param integer $user_id |
|
91 | + * @param boolean $force |
|
92 | + * @return boolean |
|
93 | + */ |
|
94 | + public function import_master( $user_id = 0, $force = false ) { |
|
95 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
96 | + return $this->app->response()->error( new WP_Error( |
|
97 | + 'semaphore_locked', |
|
98 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::import_master()' ) |
|
99 | + ) ); |
|
100 | + } |
|
101 | + |
|
102 | + $this->app->semaphore()->lock(); |
|
103 | + remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
104 | + remove_action( 'save_post', array( $this, 'delete_post' ) ); |
|
105 | + |
|
106 | + if ( $user_id ) { |
|
107 | + wp_set_current_user( $user_id ); |
|
108 | + } |
|
109 | + |
|
110 | + $result = $this->app->import()->master( $force ); |
|
111 | + |
|
112 | + $this->app->semaphore()->unlock(); |
|
113 | + |
|
114 | + if ( is_wp_error( $result ) ) { |
|
115 | + /* @var WP_Error $result */ |
|
116 | + update_option( '_wogh_import_error', $result->get_error_message() ); |
|
117 | + |
|
118 | + return $this->app->response()->error( $result ); |
|
119 | + } |
|
120 | + |
|
121 | + update_option( '_wogh_import_complete', 'yes' ); |
|
122 | + |
|
123 | + return $this->app->response()->success( $result ); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Export all the posts in the database to GitHub. |
|
128 | + * |
|
129 | + * @param int $user_id |
|
130 | + * @param boolean $force |
|
131 | + * @return boolean |
|
132 | + */ |
|
133 | + public function export_all( $user_id = 0, $force = false ) { |
|
134 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
135 | + return $this->app->response()->error( new WP_Error( |
|
136 | + 'semaphore_locked', |
|
137 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_all()' ) |
|
138 | + ) ); |
|
139 | + } |
|
140 | + |
|
141 | + $this->app->semaphore()->lock(); |
|
142 | + |
|
143 | + if ( $user_id ) { |
|
144 | + wp_set_current_user( $user_id ); |
|
145 | + } |
|
146 | + |
|
147 | + $result = $this->app->export()->full($force); |
|
148 | + $this->app->semaphore()->unlock(); |
|
149 | + |
|
150 | + // Maybe move option updating out of this class/upgrade message display? |
|
151 | + if ( is_wp_error( $result ) ) { |
|
152 | + /* @var WP_Error $result */ |
|
153 | + update_option( '_wogh_export_error', $result->get_error_message() ); |
|
154 | + |
|
155 | + return $this->app->response()->error( $result ); |
|
156 | + } else { |
|
157 | + update_option( '_wogh_export_complete', 'yes' ); |
|
158 | + update_option( '_wogh_fully_exported', 'yes' ); |
|
159 | + |
|
160 | + return $this->app->response()->success( $result ); |
|
161 | + } |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Exports a single post to GitHub by ID. |
|
166 | + * |
|
167 | + * Called on the save_post hook. |
|
168 | + * |
|
169 | + * @param int $post_id Post ID. |
|
170 | + * |
|
171 | + * @return boolean |
|
172 | + */ |
|
173 | + public function export_post( $post_id ) { |
|
174 | + if ( wp_is_post_revision( $post_id ) ) { |
|
175 | + return; |
|
176 | + } |
|
177 | + |
|
178 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
179 | + return $this->app->response()->error( new WP_Error( |
|
180 | + 'semaphore_locked', |
|
181 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_post()' ) |
|
182 | + ) ); |
|
183 | + } |
|
184 | + |
|
185 | + $this->app->semaphore()->lock(); |
|
186 | + $result = $this->app->export()->update( $post_id ); |
|
187 | + $this->app->semaphore()->unlock(); |
|
188 | + |
|
189 | + if ( is_wp_error( $result ) ) { |
|
190 | + /* @var WP_Error $result */ |
|
191 | + return $this->app->response()->error( $result ); |
|
192 | + } |
|
193 | + |
|
194 | + return $this->app->response()->success( $result ); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Removes the post from the tree. |
|
199 | + * |
|
200 | + * Called the delete_post hook. |
|
201 | + * |
|
202 | + * @param int $post_id Post ID. |
|
203 | + * |
|
204 | + * @return boolean |
|
205 | + */ |
|
206 | + public function delete_post( $post_id ) { |
|
207 | + if ( wp_is_post_revision( $post_id ) ) { |
|
208 | + return; |
|
209 | + } |
|
210 | + |
|
211 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
212 | + return $this->app->response()->error( new WP_Error( |
|
213 | + 'semaphore_locked', |
|
214 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::delete_post()' ) |
|
215 | + ) ); |
|
216 | + } |
|
217 | + |
|
218 | + $this->app->semaphore()->lock(); |
|
219 | + $result = $this->app->export()->delete( $post_id ); |
|
220 | + $this->app->semaphore()->unlock(); |
|
221 | + |
|
222 | + if ( is_wp_error( $result ) ) { |
|
223 | + /* @var WP_Error $result */ |
|
224 | + return $this->app->response()->error( $result ); |
|
225 | + } |
|
226 | + |
|
227 | + return $this->app->response()->success( $result ); |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Indicates we're running our own AJAX hook |
|
232 | + * and thus should respond with JSON, rather |
|
233 | + * than just returning data. |
|
234 | + */ |
|
235 | + protected function set_ajax() { |
|
236 | + if ( ! defined( 'WOGH_AJAX' ) ) { |
|
237 | + define( 'WOGH_AJAX', true ); |
|
238 | + } |
|
239 | + } |
|
240 | 240 | } |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | * |
22 | 22 | * @param Writing_On_GitHub $app Applicatio container. |
23 | 23 | */ |
24 | - public function __construct( Writing_On_GitHub $app ) { |
|
24 | + public function __construct(Writing_On_GitHub $app) { |
|
25 | 25 | $this->app = $app; |
26 | 26 | } |
27 | 27 | |
@@ -34,55 +34,55 @@ discard block |
||
34 | 34 | */ |
35 | 35 | public function pull_posts() { |
36 | 36 | $this->set_ajax(); |
37 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
38 | - return $this->app->response()->error( new WP_Error( |
|
37 | + if ( ! $this->app->semaphore()->is_open()) { |
|
38 | + return $this->app->response()->error(new WP_Error( |
|
39 | 39 | 'semaphore_locked', |
40 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::pull_posts()' ) |
|
41 | - ) ); |
|
40 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::pull_posts()') |
|
41 | + )); |
|
42 | 42 | } |
43 | 43 | |
44 | - if ( ! $this->app->request()->is_secret_valid() ) { |
|
45 | - return $this->app->response()->error( new WP_Error( |
|
44 | + if ( ! $this->app->request()->is_secret_valid()) { |
|
45 | + return $this->app->response()->error(new WP_Error( |
|
46 | 46 | 'invalid_headers', |
47 | - __( 'Failed to validate secret.', 'writing-on-github' ) |
|
48 | - ) ); |
|
47 | + __('Failed to validate secret.', 'writing-on-github') |
|
48 | + )); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | // ping |
52 | - if ( $this->app->request()->is_ping() ) { |
|
53 | - return $this->app->response()->success( __( 'Wordpress is ready.', 'writing-on-github' ) ); |
|
52 | + if ($this->app->request()->is_ping()) { |
|
53 | + return $this->app->response()->success(__('Wordpress is ready.', 'writing-on-github')); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | // push |
57 | - if ( ! $this->app->request()->is_push() ) { |
|
58 | - return $this->app->response()->error( new WP_Error( |
|
57 | + if ( ! $this->app->request()->is_push()) { |
|
58 | + return $this->app->response()->error(new WP_Error( |
|
59 | 59 | 'invalid_headers', |
60 | - sprintf( 'Failed to validate webhook event: %s.', |
|
61 | - $this->app->request()->webhook_event() ) |
|
62 | - ) ); |
|
60 | + sprintf('Failed to validate webhook event: %s.', |
|
61 | + $this->app->request()->webhook_event()) |
|
62 | + )); |
|
63 | 63 | } |
64 | 64 | $payload = $this->app->request()->payload(); |
65 | 65 | |
66 | 66 | $error = $payload->should_import(); |
67 | - if ( is_wp_error( $error ) ) { |
|
67 | + if (is_wp_error($error)) { |
|
68 | 68 | /* @var WP_Error $error */ |
69 | - return $this->app->response()->error( $error ); |
|
69 | + return $this->app->response()->error($error); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | $this->app->semaphore()->lock(); |
73 | - remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
74 | - remove_action( 'delete_post', array( $this, 'delete_post' ) ); |
|
73 | + remove_action('save_post', array($this, 'export_post')); |
|
74 | + remove_action('delete_post', array($this, 'delete_post')); |
|
75 | 75 | |
76 | - $result = $this->app->import()->payload( $payload ); |
|
76 | + $result = $this->app->import()->payload($payload); |
|
77 | 77 | |
78 | 78 | $this->app->semaphore()->unlock(); |
79 | 79 | |
80 | - if ( is_wp_error( $result ) ) { |
|
80 | + if (is_wp_error($result)) { |
|
81 | 81 | /* @var WP_Error $result */ |
82 | - return $this->app->response()->error( $result ); |
|
82 | + return $this->app->response()->error($result); |
|
83 | 83 | } |
84 | 84 | |
85 | - return $this->app->response()->success( $result ); |
|
85 | + return $this->app->response()->success($result); |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
@@ -91,36 +91,36 @@ discard block |
||
91 | 91 | * @param boolean $force |
92 | 92 | * @return boolean |
93 | 93 | */ |
94 | - public function import_master( $user_id = 0, $force = false ) { |
|
95 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
96 | - return $this->app->response()->error( new WP_Error( |
|
94 | + public function import_master($user_id = 0, $force = false) { |
|
95 | + if ( ! $this->app->semaphore()->is_open()) { |
|
96 | + return $this->app->response()->error(new WP_Error( |
|
97 | 97 | 'semaphore_locked', |
98 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::import_master()' ) |
|
99 | - ) ); |
|
98 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::import_master()') |
|
99 | + )); |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | $this->app->semaphore()->lock(); |
103 | - remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
104 | - remove_action( 'save_post', array( $this, 'delete_post' ) ); |
|
103 | + remove_action('save_post', array($this, 'export_post')); |
|
104 | + remove_action('save_post', array($this, 'delete_post')); |
|
105 | 105 | |
106 | - if ( $user_id ) { |
|
107 | - wp_set_current_user( $user_id ); |
|
106 | + if ($user_id) { |
|
107 | + wp_set_current_user($user_id); |
|
108 | 108 | } |
109 | 109 | |
110 | - $result = $this->app->import()->master( $force ); |
|
110 | + $result = $this->app->import()->master($force); |
|
111 | 111 | |
112 | 112 | $this->app->semaphore()->unlock(); |
113 | 113 | |
114 | - if ( is_wp_error( $result ) ) { |
|
114 | + if (is_wp_error($result)) { |
|
115 | 115 | /* @var WP_Error $result */ |
116 | - update_option( '_wogh_import_error', $result->get_error_message() ); |
|
116 | + update_option('_wogh_import_error', $result->get_error_message()); |
|
117 | 117 | |
118 | - return $this->app->response()->error( $result ); |
|
118 | + return $this->app->response()->error($result); |
|
119 | 119 | } |
120 | 120 | |
121 | - update_option( '_wogh_import_complete', 'yes' ); |
|
121 | + update_option('_wogh_import_complete', 'yes'); |
|
122 | 122 | |
123 | - return $this->app->response()->success( $result ); |
|
123 | + return $this->app->response()->success($result); |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | /** |
@@ -130,34 +130,34 @@ discard block |
||
130 | 130 | * @param boolean $force |
131 | 131 | * @return boolean |
132 | 132 | */ |
133 | - public function export_all( $user_id = 0, $force = false ) { |
|
134 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
135 | - return $this->app->response()->error( new WP_Error( |
|
133 | + public function export_all($user_id = 0, $force = false) { |
|
134 | + if ( ! $this->app->semaphore()->is_open()) { |
|
135 | + return $this->app->response()->error(new WP_Error( |
|
136 | 136 | 'semaphore_locked', |
137 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_all()' ) |
|
138 | - ) ); |
|
137 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::export_all()') |
|
138 | + )); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | $this->app->semaphore()->lock(); |
142 | 142 | |
143 | - if ( $user_id ) { |
|
144 | - wp_set_current_user( $user_id ); |
|
143 | + if ($user_id) { |
|
144 | + wp_set_current_user($user_id); |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | $result = $this->app->export()->full($force); |
148 | 148 | $this->app->semaphore()->unlock(); |
149 | 149 | |
150 | 150 | // Maybe move option updating out of this class/upgrade message display? |
151 | - if ( is_wp_error( $result ) ) { |
|
151 | + if (is_wp_error($result)) { |
|
152 | 152 | /* @var WP_Error $result */ |
153 | - update_option( '_wogh_export_error', $result->get_error_message() ); |
|
153 | + update_option('_wogh_export_error', $result->get_error_message()); |
|
154 | 154 | |
155 | - return $this->app->response()->error( $result ); |
|
155 | + return $this->app->response()->error($result); |
|
156 | 156 | } else { |
157 | - update_option( '_wogh_export_complete', 'yes' ); |
|
158 | - update_option( '_wogh_fully_exported', 'yes' ); |
|
157 | + update_option('_wogh_export_complete', 'yes'); |
|
158 | + update_option('_wogh_fully_exported', 'yes'); |
|
159 | 159 | |
160 | - return $this->app->response()->success( $result ); |
|
160 | + return $this->app->response()->success($result); |
|
161 | 161 | } |
162 | 162 | } |
163 | 163 | |
@@ -170,28 +170,28 @@ discard block |
||
170 | 170 | * |
171 | 171 | * @return boolean |
172 | 172 | */ |
173 | - public function export_post( $post_id ) { |
|
174 | - if ( wp_is_post_revision( $post_id ) ) { |
|
173 | + public function export_post($post_id) { |
|
174 | + if (wp_is_post_revision($post_id)) { |
|
175 | 175 | return; |
176 | 176 | } |
177 | 177 | |
178 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
179 | - return $this->app->response()->error( new WP_Error( |
|
178 | + if ( ! $this->app->semaphore()->is_open()) { |
|
179 | + return $this->app->response()->error(new WP_Error( |
|
180 | 180 | 'semaphore_locked', |
181 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_post()' ) |
|
182 | - ) ); |
|
181 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::export_post()') |
|
182 | + )); |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | $this->app->semaphore()->lock(); |
186 | - $result = $this->app->export()->update( $post_id ); |
|
186 | + $result = $this->app->export()->update($post_id); |
|
187 | 187 | $this->app->semaphore()->unlock(); |
188 | 188 | |
189 | - if ( is_wp_error( $result ) ) { |
|
189 | + if (is_wp_error($result)) { |
|
190 | 190 | /* @var WP_Error $result */ |
191 | - return $this->app->response()->error( $result ); |
|
191 | + return $this->app->response()->error($result); |
|
192 | 192 | } |
193 | 193 | |
194 | - return $this->app->response()->success( $result ); |
|
194 | + return $this->app->response()->success($result); |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | /** |
@@ -203,28 +203,28 @@ discard block |
||
203 | 203 | * |
204 | 204 | * @return boolean |
205 | 205 | */ |
206 | - public function delete_post( $post_id ) { |
|
207 | - if ( wp_is_post_revision( $post_id ) ) { |
|
206 | + public function delete_post($post_id) { |
|
207 | + if (wp_is_post_revision($post_id)) { |
|
208 | 208 | return; |
209 | 209 | } |
210 | 210 | |
211 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
212 | - return $this->app->response()->error( new WP_Error( |
|
211 | + if ( ! $this->app->semaphore()->is_open()) { |
|
212 | + return $this->app->response()->error(new WP_Error( |
|
213 | 213 | 'semaphore_locked', |
214 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::delete_post()' ) |
|
215 | - ) ); |
|
214 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::delete_post()') |
|
215 | + )); |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | $this->app->semaphore()->lock(); |
219 | - $result = $this->app->export()->delete( $post_id ); |
|
219 | + $result = $this->app->export()->delete($post_id); |
|
220 | 220 | $this->app->semaphore()->unlock(); |
221 | 221 | |
222 | - if ( is_wp_error( $result ) ) { |
|
222 | + if (is_wp_error($result)) { |
|
223 | 223 | /* @var WP_Error $result */ |
224 | - return $this->app->response()->error( $result ); |
|
224 | + return $this->app->response()->error($result); |
|
225 | 225 | } |
226 | 226 | |
227 | - return $this->app->response()->success( $result ); |
|
227 | + return $this->app->response()->success($result); |
|
228 | 228 | } |
229 | 229 | |
230 | 230 | /** |
@@ -233,8 +233,8 @@ discard block |
||
233 | 233 | * than just returning data. |
234 | 234 | */ |
235 | 235 | protected function set_ajax() { |
236 | - if ( ! defined( 'WOGH_AJAX' ) ) { |
|
237 | - define( 'WOGH_AJAX', true ); |
|
236 | + if ( ! defined('WOGH_AJAX')) { |
|
237 | + define('WOGH_AJAX', true); |
|
238 | 238 | } |
239 | 239 | } |
240 | 240 | } |
@@ -10,330 +10,330 @@ |
||
10 | 10 | */ |
11 | 11 | class Writing_On_GitHub_Import { |
12 | 12 | |
13 | - /** |
|
14 | - * Application container. |
|
15 | - * |
|
16 | - * @var Writing_On_GitHub |
|
17 | - */ |
|
18 | - protected $app; |
|
19 | - |
|
20 | - /** |
|
21 | - * Initializes a new import 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 | - * Imports a payload. |
|
31 | - * @param Writing_On_GitHub_Payload $payload |
|
32 | - * |
|
33 | - * @return string|WP_Error |
|
34 | - */ |
|
35 | - public function payload( Writing_On_GitHub_Payload $payload ) { |
|
36 | - |
|
37 | - $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() ); |
|
38 | - |
|
39 | - if ( is_wp_error( $result ) ) { |
|
40 | - /* @var WP_Error $result */ |
|
41 | - return $result; |
|
42 | - } |
|
43 | - |
|
44 | - if ( is_array( $result ) ) { |
|
45 | - $result = $this->import_files( $result ); |
|
46 | - } |
|
47 | - |
|
48 | - if ( is_wp_error( $result ) ) { |
|
49 | - return $files; |
|
50 | - } |
|
51 | - |
|
52 | - return __( 'Payload processed', 'writing-on-github' ); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * import blob by files |
|
57 | - * @param Writing_On_GitHub_File_Info[] $files |
|
58 | - * @param boolean $force |
|
59 | - * |
|
60 | - * @return true|WP_Error |
|
61 | - */ |
|
62 | - protected function import_files( $files, $force = false ) { |
|
63 | - |
|
64 | - $error = true; |
|
65 | - |
|
66 | - foreach ( $files as $file ) { |
|
67 | - if ( ! $this->importable_file( $file ) ) { |
|
68 | - continue; |
|
69 | - } |
|
70 | - |
|
71 | - $blob = $this->app->api()->fetch()->blob( $file ); |
|
72 | - // network error ? |
|
73 | - if ( ! $blob instanceof Writing_On_GitHub_Blob ) { |
|
74 | - continue; |
|
75 | - } |
|
76 | - |
|
77 | - $is_remove = 'removed' == $file->status; |
|
78 | - |
|
79 | - $result = false; |
|
80 | - if ( $this->importable_raw_file( $blob ) ) { |
|
81 | - $result = $this->import_raw_file( $blob, $is_remove ); |
|
82 | - } elseif ( $this->importable_post( $blob ) ) { |
|
83 | - if ( $is_remove ) { |
|
84 | - $result = $this->delete_post( $blob ); |
|
85 | - } else { |
|
86 | - $result = $this->import_post( $blob, $force ); |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - if ( is_wp_error( $result ) ) { |
|
91 | - /* @var WP_Error $result */ |
|
92 | - $error = wogh_append_error( $error, $result ); |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - return $error; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Imports the latest commit on the master branch. |
|
101 | - * |
|
102 | - * @param boolean $force |
|
103 | - * @return string|WP_Error |
|
104 | - */ |
|
105 | - public function master( $force = false ) { |
|
106 | - $result = $this->app->api()->fetch()->tree_recursive(); |
|
107 | - |
|
108 | - if ( is_wp_error( $result ) ) { |
|
109 | - /* @var WP_Error $result */ |
|
110 | - return $result; |
|
111 | - } |
|
112 | - |
|
113 | - if ( is_array( $result ) ) { |
|
114 | - $result = $this->import_files( $result, $force ); |
|
115 | - } |
|
116 | - |
|
117 | - if ( is_wp_error( $result ) ) { |
|
118 | - /* @var WP_Error $result */ |
|
119 | - return $result; |
|
120 | - } |
|
121 | - |
|
122 | - return __( 'Payload processed', 'writing-on-github' ); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Checks whether the provided blob should be imported. |
|
127 | - * |
|
128 | - * @param Writing_On_GitHub_File_Info $file |
|
129 | - * |
|
130 | - * @return bool |
|
131 | - */ |
|
132 | - protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
133 | - |
|
134 | - $path = $file->path; |
|
135 | - |
|
136 | - // only _pages, _posts and images |
|
137 | - $prefixs = array( '_pages/', '_posts/', 'images/'); |
|
138 | - foreach ($prefixs as $prefix) { |
|
139 | - if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { |
|
140 | - return true; |
|
141 | - } |
|
142 | - } |
|
143 | - return false; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Checks whether the provided blob should be imported. |
|
148 | - * |
|
149 | - * @param Writing_On_GitHub_Blob $blob Blob to validate. |
|
150 | - * |
|
151 | - * @return bool |
|
152 | - */ |
|
153 | - protected function importable_post( Writing_On_GitHub_Blob $blob ) { |
|
154 | - // global $wpdb; |
|
155 | - |
|
156 | - // // Skip the repo's readme. |
|
157 | - // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { |
|
158 | - // return false; |
|
159 | - // } |
|
160 | - |
|
161 | - // // If the blob sha already matches a post, then move on. |
|
162 | - // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { |
|
163 | - // return false; |
|
164 | - // } |
|
165 | - |
|
166 | - if ( ! $blob->has_frontmatter() ) { |
|
167 | - return false; |
|
168 | - } |
|
169 | - |
|
170 | - return true; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Delete post |
|
175 | - * @param Writing_On_GitHub_Blob $blob |
|
176 | - * @return WP_Error|bool |
|
177 | - */ |
|
178 | - protected function delete_post( Writing_On_GitHub_Blob $blob ) { |
|
179 | - $id = $blob->id(); |
|
180 | - if ( empty( $id ) ) { |
|
181 | - return false; |
|
182 | - } |
|
183 | - $result = $this->app->database()->delete_post( $id ); |
|
184 | - if ( is_wp_error( $result ) ) { |
|
185 | - /* @var WP_Error $result */ |
|
186 | - return $result; |
|
187 | - } |
|
188 | - return true; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Imports a post into wordpress |
|
193 | - * @param Writing_On_GitHub_Blob $blob |
|
194 | - * @param boolean $force |
|
195 | - * @return WP_Error|bool |
|
196 | - */ |
|
197 | - protected function import_post( Writing_On_GitHub_Blob $blob, $force = false ) { |
|
198 | - $post = $this->blob_to_post( $blob, $force ); |
|
199 | - |
|
200 | - if ( ! $post instanceof Writing_On_GitHub_Post ) { |
|
201 | - return false; |
|
202 | - } |
|
203 | - |
|
204 | - $result = $this->app->database()->save_post( $post ); |
|
205 | - if ( is_wp_error( $result ) ) { |
|
206 | - /** @var WP_Error $result */ |
|
207 | - return $result; |
|
208 | - } |
|
209 | - |
|
210 | - if ( $post->is_new() || |
|
211 | - ! wogh_equal_front_matter( $post, $blob ) ) { |
|
212 | - |
|
213 | - $result = $this->app->export()->export_post( $post ); |
|
214 | - |
|
215 | - if ( is_wp_error( $result ) ) { |
|
216 | - /** @var WP_Error $result */ |
|
217 | - return $result; |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - clean_post_cache( $post->id() ); |
|
222 | - |
|
223 | - return true; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * import raw file |
|
228 | - * @param Writing_On_GitHub_Blob $blob |
|
229 | - * @return bool |
|
230 | - */ |
|
231 | - protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
232 | - if ( $blob->has_frontmatter() ) { |
|
233 | - return false; |
|
234 | - } |
|
235 | - |
|
236 | - // only images |
|
237 | - if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
238 | - return false; |
|
239 | - } |
|
240 | - |
|
241 | - return true; |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Imports a raw file content into file system. |
|
246 | - * @param Writing_On_GitHub_Blob $blob |
|
247 | - * @param bool $is_remove |
|
248 | - */ |
|
249 | - protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
250 | - $arr = wp_upload_dir(); |
|
251 | - $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
|
252 | - if ( $is_remove ) { |
|
253 | - if ( file_exists($path) ) { |
|
254 | - unlink($path); |
|
255 | - } |
|
256 | - } else { |
|
257 | - $dirname = dirname($path); |
|
258 | - if ( ! file_exists($dirname) ) { |
|
259 | - wp_mkdir_p($dirname); |
|
260 | - } |
|
261 | - |
|
262 | - file_put_contents($path, $blob->content()); |
|
263 | - } |
|
264 | - return true; |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * Imports a single blob content into matching post. |
|
269 | - * |
|
270 | - * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. |
|
271 | - * @param boolean $force |
|
272 | - * |
|
273 | - * @return Writing_On_GitHub_Post|false |
|
274 | - */ |
|
275 | - protected function blob_to_post( Writing_On_GitHub_Blob $blob, $force = false ) { |
|
276 | - $args = array( 'post_content' => $blob->content_import() ); |
|
277 | - $meta = $blob->meta(); |
|
278 | - |
|
279 | - $id = false; |
|
280 | - |
|
281 | - if ( ! empty( $meta ) ) { |
|
282 | - if ( array_key_exists( 'layout', $meta ) ) { |
|
283 | - $args['post_type'] = $meta['layout']; |
|
284 | - unset( $meta['layout'] ); |
|
285 | - } |
|
286 | - |
|
287 | - if ( array_key_exists( 'published', $meta ) ) { |
|
288 | - $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
|
289 | - unset( $meta['published'] ); |
|
290 | - } |
|
291 | - |
|
292 | - if ( array_key_exists( 'post_title', $meta ) ) { |
|
293 | - $args['post_title'] = $meta['post_title']; |
|
294 | - unset( $meta['post_title'] ); |
|
295 | - } |
|
296 | - |
|
297 | - if ( array_key_exists( 'post_name', $meta ) ) { |
|
298 | - $args['post_name'] = $meta['post_name']; |
|
299 | - unset( $meta['post_name'] ); |
|
300 | - } |
|
301 | - |
|
302 | - if ( array_key_exists( 'ID', $meta ) ) { |
|
303 | - $id = $args['ID'] = $meta['ID']; |
|
304 | - $blob->set_id($id); |
|
305 | - unset( $meta['ID'] ); |
|
306 | - } |
|
307 | - |
|
308 | - if ( array_key_exists( 'post_date', $meta ) ) { |
|
309 | - if ( empty( $meta['post_date'] ) ) { |
|
310 | - $meta['post_date'] = current_time( 'mysql' ); |
|
311 | - } |
|
312 | - |
|
313 | - $args['post_date'] = $meta['post_date']; |
|
314 | - unset( $meta['post_date'] ); |
|
315 | - } |
|
316 | - } |
|
317 | - |
|
318 | - $meta['_wogh_sha'] = $blob->sha(); |
|
319 | - |
|
320 | - if ( ! $force && $id ) { |
|
321 | - $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
322 | - $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
323 | - |
|
324 | - // dont save post when has same sha |
|
325 | - if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
326 | - $old_github_path && $old_github_path == $blob->path() ) { |
|
327 | - return false; |
|
328 | - } |
|
329 | - } |
|
330 | - |
|
331 | - $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
332 | - $post->set_old_github_path( $blob->path() ); |
|
333 | - $post->set_meta( $meta ); |
|
334 | - $post->set_blob( $blob ); |
|
335 | - $blob->set_id( $post->id() ); |
|
336 | - |
|
337 | - return $post; |
|
338 | - } |
|
13 | + /** |
|
14 | + * Application container. |
|
15 | + * |
|
16 | + * @var Writing_On_GitHub |
|
17 | + */ |
|
18 | + protected $app; |
|
19 | + |
|
20 | + /** |
|
21 | + * Initializes a new import 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 | + * Imports a payload. |
|
31 | + * @param Writing_On_GitHub_Payload $payload |
|
32 | + * |
|
33 | + * @return string|WP_Error |
|
34 | + */ |
|
35 | + public function payload( Writing_On_GitHub_Payload $payload ) { |
|
36 | + |
|
37 | + $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() ); |
|
38 | + |
|
39 | + if ( is_wp_error( $result ) ) { |
|
40 | + /* @var WP_Error $result */ |
|
41 | + return $result; |
|
42 | + } |
|
43 | + |
|
44 | + if ( is_array( $result ) ) { |
|
45 | + $result = $this->import_files( $result ); |
|
46 | + } |
|
47 | + |
|
48 | + if ( is_wp_error( $result ) ) { |
|
49 | + return $files; |
|
50 | + } |
|
51 | + |
|
52 | + return __( 'Payload processed', 'writing-on-github' ); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * import blob by files |
|
57 | + * @param Writing_On_GitHub_File_Info[] $files |
|
58 | + * @param boolean $force |
|
59 | + * |
|
60 | + * @return true|WP_Error |
|
61 | + */ |
|
62 | + protected function import_files( $files, $force = false ) { |
|
63 | + |
|
64 | + $error = true; |
|
65 | + |
|
66 | + foreach ( $files as $file ) { |
|
67 | + if ( ! $this->importable_file( $file ) ) { |
|
68 | + continue; |
|
69 | + } |
|
70 | + |
|
71 | + $blob = $this->app->api()->fetch()->blob( $file ); |
|
72 | + // network error ? |
|
73 | + if ( ! $blob instanceof Writing_On_GitHub_Blob ) { |
|
74 | + continue; |
|
75 | + } |
|
76 | + |
|
77 | + $is_remove = 'removed' == $file->status; |
|
78 | + |
|
79 | + $result = false; |
|
80 | + if ( $this->importable_raw_file( $blob ) ) { |
|
81 | + $result = $this->import_raw_file( $blob, $is_remove ); |
|
82 | + } elseif ( $this->importable_post( $blob ) ) { |
|
83 | + if ( $is_remove ) { |
|
84 | + $result = $this->delete_post( $blob ); |
|
85 | + } else { |
|
86 | + $result = $this->import_post( $blob, $force ); |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + if ( is_wp_error( $result ) ) { |
|
91 | + /* @var WP_Error $result */ |
|
92 | + $error = wogh_append_error( $error, $result ); |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + return $error; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Imports the latest commit on the master branch. |
|
101 | + * |
|
102 | + * @param boolean $force |
|
103 | + * @return string|WP_Error |
|
104 | + */ |
|
105 | + public function master( $force = false ) { |
|
106 | + $result = $this->app->api()->fetch()->tree_recursive(); |
|
107 | + |
|
108 | + if ( is_wp_error( $result ) ) { |
|
109 | + /* @var WP_Error $result */ |
|
110 | + return $result; |
|
111 | + } |
|
112 | + |
|
113 | + if ( is_array( $result ) ) { |
|
114 | + $result = $this->import_files( $result, $force ); |
|
115 | + } |
|
116 | + |
|
117 | + if ( is_wp_error( $result ) ) { |
|
118 | + /* @var WP_Error $result */ |
|
119 | + return $result; |
|
120 | + } |
|
121 | + |
|
122 | + return __( 'Payload processed', 'writing-on-github' ); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Checks whether the provided blob should be imported. |
|
127 | + * |
|
128 | + * @param Writing_On_GitHub_File_Info $file |
|
129 | + * |
|
130 | + * @return bool |
|
131 | + */ |
|
132 | + protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
133 | + |
|
134 | + $path = $file->path; |
|
135 | + |
|
136 | + // only _pages, _posts and images |
|
137 | + $prefixs = array( '_pages/', '_posts/', 'images/'); |
|
138 | + foreach ($prefixs as $prefix) { |
|
139 | + if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { |
|
140 | + return true; |
|
141 | + } |
|
142 | + } |
|
143 | + return false; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Checks whether the provided blob should be imported. |
|
148 | + * |
|
149 | + * @param Writing_On_GitHub_Blob $blob Blob to validate. |
|
150 | + * |
|
151 | + * @return bool |
|
152 | + */ |
|
153 | + protected function importable_post( Writing_On_GitHub_Blob $blob ) { |
|
154 | + // global $wpdb; |
|
155 | + |
|
156 | + // // Skip the repo's readme. |
|
157 | + // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { |
|
158 | + // return false; |
|
159 | + // } |
|
160 | + |
|
161 | + // // If the blob sha already matches a post, then move on. |
|
162 | + // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { |
|
163 | + // return false; |
|
164 | + // } |
|
165 | + |
|
166 | + if ( ! $blob->has_frontmatter() ) { |
|
167 | + return false; |
|
168 | + } |
|
169 | + |
|
170 | + return true; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Delete post |
|
175 | + * @param Writing_On_GitHub_Blob $blob |
|
176 | + * @return WP_Error|bool |
|
177 | + */ |
|
178 | + protected function delete_post( Writing_On_GitHub_Blob $blob ) { |
|
179 | + $id = $blob->id(); |
|
180 | + if ( empty( $id ) ) { |
|
181 | + return false; |
|
182 | + } |
|
183 | + $result = $this->app->database()->delete_post( $id ); |
|
184 | + if ( is_wp_error( $result ) ) { |
|
185 | + /* @var WP_Error $result */ |
|
186 | + return $result; |
|
187 | + } |
|
188 | + return true; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Imports a post into wordpress |
|
193 | + * @param Writing_On_GitHub_Blob $blob |
|
194 | + * @param boolean $force |
|
195 | + * @return WP_Error|bool |
|
196 | + */ |
|
197 | + protected function import_post( Writing_On_GitHub_Blob $blob, $force = false ) { |
|
198 | + $post = $this->blob_to_post( $blob, $force ); |
|
199 | + |
|
200 | + if ( ! $post instanceof Writing_On_GitHub_Post ) { |
|
201 | + return false; |
|
202 | + } |
|
203 | + |
|
204 | + $result = $this->app->database()->save_post( $post ); |
|
205 | + if ( is_wp_error( $result ) ) { |
|
206 | + /** @var WP_Error $result */ |
|
207 | + return $result; |
|
208 | + } |
|
209 | + |
|
210 | + if ( $post->is_new() || |
|
211 | + ! wogh_equal_front_matter( $post, $blob ) ) { |
|
212 | + |
|
213 | + $result = $this->app->export()->export_post( $post ); |
|
214 | + |
|
215 | + if ( is_wp_error( $result ) ) { |
|
216 | + /** @var WP_Error $result */ |
|
217 | + return $result; |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + clean_post_cache( $post->id() ); |
|
222 | + |
|
223 | + return true; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * import raw file |
|
228 | + * @param Writing_On_GitHub_Blob $blob |
|
229 | + * @return bool |
|
230 | + */ |
|
231 | + protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
232 | + if ( $blob->has_frontmatter() ) { |
|
233 | + return false; |
|
234 | + } |
|
235 | + |
|
236 | + // only images |
|
237 | + if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
238 | + return false; |
|
239 | + } |
|
240 | + |
|
241 | + return true; |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Imports a raw file content into file system. |
|
246 | + * @param Writing_On_GitHub_Blob $blob |
|
247 | + * @param bool $is_remove |
|
248 | + */ |
|
249 | + protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
250 | + $arr = wp_upload_dir(); |
|
251 | + $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
|
252 | + if ( $is_remove ) { |
|
253 | + if ( file_exists($path) ) { |
|
254 | + unlink($path); |
|
255 | + } |
|
256 | + } else { |
|
257 | + $dirname = dirname($path); |
|
258 | + if ( ! file_exists($dirname) ) { |
|
259 | + wp_mkdir_p($dirname); |
|
260 | + } |
|
261 | + |
|
262 | + file_put_contents($path, $blob->content()); |
|
263 | + } |
|
264 | + return true; |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * Imports a single blob content into matching post. |
|
269 | + * |
|
270 | + * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. |
|
271 | + * @param boolean $force |
|
272 | + * |
|
273 | + * @return Writing_On_GitHub_Post|false |
|
274 | + */ |
|
275 | + protected function blob_to_post( Writing_On_GitHub_Blob $blob, $force = false ) { |
|
276 | + $args = array( 'post_content' => $blob->content_import() ); |
|
277 | + $meta = $blob->meta(); |
|
278 | + |
|
279 | + $id = false; |
|
280 | + |
|
281 | + if ( ! empty( $meta ) ) { |
|
282 | + if ( array_key_exists( 'layout', $meta ) ) { |
|
283 | + $args['post_type'] = $meta['layout']; |
|
284 | + unset( $meta['layout'] ); |
|
285 | + } |
|
286 | + |
|
287 | + if ( array_key_exists( 'published', $meta ) ) { |
|
288 | + $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
|
289 | + unset( $meta['published'] ); |
|
290 | + } |
|
291 | + |
|
292 | + if ( array_key_exists( 'post_title', $meta ) ) { |
|
293 | + $args['post_title'] = $meta['post_title']; |
|
294 | + unset( $meta['post_title'] ); |
|
295 | + } |
|
296 | + |
|
297 | + if ( array_key_exists( 'post_name', $meta ) ) { |
|
298 | + $args['post_name'] = $meta['post_name']; |
|
299 | + unset( $meta['post_name'] ); |
|
300 | + } |
|
301 | + |
|
302 | + if ( array_key_exists( 'ID', $meta ) ) { |
|
303 | + $id = $args['ID'] = $meta['ID']; |
|
304 | + $blob->set_id($id); |
|
305 | + unset( $meta['ID'] ); |
|
306 | + } |
|
307 | + |
|
308 | + if ( array_key_exists( 'post_date', $meta ) ) { |
|
309 | + if ( empty( $meta['post_date'] ) ) { |
|
310 | + $meta['post_date'] = current_time( 'mysql' ); |
|
311 | + } |
|
312 | + |
|
313 | + $args['post_date'] = $meta['post_date']; |
|
314 | + unset( $meta['post_date'] ); |
|
315 | + } |
|
316 | + } |
|
317 | + |
|
318 | + $meta['_wogh_sha'] = $blob->sha(); |
|
319 | + |
|
320 | + if ( ! $force && $id ) { |
|
321 | + $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
322 | + $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
323 | + |
|
324 | + // dont save post when has same sha |
|
325 | + if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
326 | + $old_github_path && $old_github_path == $blob->path() ) { |
|
327 | + return false; |
|
328 | + } |
|
329 | + } |
|
330 | + |
|
331 | + $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
332 | + $post->set_old_github_path( $blob->path() ); |
|
333 | + $post->set_meta( $meta ); |
|
334 | + $post->set_blob( $blob ); |
|
335 | + $blob->set_id( $post->id() ); |
|
336 | + |
|
337 | + return $post; |
|
338 | + } |
|
339 | 339 | } |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | * |
23 | 23 | * @param Writing_On_GitHub $app Application container. |
24 | 24 | */ |
25 | - public function __construct( Writing_On_GitHub $app ) { |
|
25 | + public function __construct(Writing_On_GitHub $app) { |
|
26 | 26 | $this->app = $app; |
27 | 27 | } |
28 | 28 | |
@@ -32,24 +32,24 @@ discard block |
||
32 | 32 | * |
33 | 33 | * @return string|WP_Error |
34 | 34 | */ |
35 | - public function payload( Writing_On_GitHub_Payload $payload ) { |
|
35 | + public function payload(Writing_On_GitHub_Payload $payload) { |
|
36 | 36 | |
37 | - $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() ); |
|
37 | + $result = $this->app->api()->fetch()->compare($payload->get_before_commit_id()); |
|
38 | 38 | |
39 | - if ( is_wp_error( $result ) ) { |
|
39 | + if (is_wp_error($result)) { |
|
40 | 40 | /* @var WP_Error $result */ |
41 | 41 | return $result; |
42 | 42 | } |
43 | 43 | |
44 | - if ( is_array( $result ) ) { |
|
45 | - $result = $this->import_files( $result ); |
|
44 | + if (is_array($result)) { |
|
45 | + $result = $this->import_files($result); |
|
46 | 46 | } |
47 | 47 | |
48 | - if ( is_wp_error( $result ) ) { |
|
48 | + if (is_wp_error($result)) { |
|
49 | 49 | return $files; |
50 | 50 | } |
51 | 51 | |
52 | - return __( 'Payload processed', 'writing-on-github' ); |
|
52 | + return __('Payload processed', 'writing-on-github'); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
@@ -59,37 +59,37 @@ discard block |
||
59 | 59 | * |
60 | 60 | * @return true|WP_Error |
61 | 61 | */ |
62 | - protected function import_files( $files, $force = false ) { |
|
62 | + protected function import_files($files, $force = false) { |
|
63 | 63 | |
64 | 64 | $error = true; |
65 | 65 | |
66 | - foreach ( $files as $file ) { |
|
67 | - if ( ! $this->importable_file( $file ) ) { |
|
66 | + foreach ($files as $file) { |
|
67 | + if ( ! $this->importable_file($file)) { |
|
68 | 68 | continue; |
69 | 69 | } |
70 | 70 | |
71 | - $blob = $this->app->api()->fetch()->blob( $file ); |
|
71 | + $blob = $this->app->api()->fetch()->blob($file); |
|
72 | 72 | // network error ? |
73 | - if ( ! $blob instanceof Writing_On_GitHub_Blob ) { |
|
73 | + if ( ! $blob instanceof Writing_On_GitHub_Blob) { |
|
74 | 74 | continue; |
75 | 75 | } |
76 | 76 | |
77 | 77 | $is_remove = 'removed' == $file->status; |
78 | 78 | |
79 | 79 | $result = false; |
80 | - if ( $this->importable_raw_file( $blob ) ) { |
|
81 | - $result = $this->import_raw_file( $blob, $is_remove ); |
|
82 | - } elseif ( $this->importable_post( $blob ) ) { |
|
83 | - if ( $is_remove ) { |
|
84 | - $result = $this->delete_post( $blob ); |
|
80 | + if ($this->importable_raw_file($blob)) { |
|
81 | + $result = $this->import_raw_file($blob, $is_remove); |
|
82 | + } elseif ($this->importable_post($blob)) { |
|
83 | + if ($is_remove) { |
|
84 | + $result = $this->delete_post($blob); |
|
85 | 85 | } else { |
86 | - $result = $this->import_post( $blob, $force ); |
|
86 | + $result = $this->import_post($blob, $force); |
|
87 | 87 | } |
88 | 88 | } |
89 | 89 | |
90 | - if ( is_wp_error( $result ) ) { |
|
90 | + if (is_wp_error($result)) { |
|
91 | 91 | /* @var WP_Error $result */ |
92 | - $error = wogh_append_error( $error, $result ); |
|
92 | + $error = wogh_append_error($error, $result); |
|
93 | 93 | } |
94 | 94 | } |
95 | 95 | |
@@ -102,24 +102,24 @@ discard block |
||
102 | 102 | * @param boolean $force |
103 | 103 | * @return string|WP_Error |
104 | 104 | */ |
105 | - public function master( $force = false ) { |
|
105 | + public function master($force = false) { |
|
106 | 106 | $result = $this->app->api()->fetch()->tree_recursive(); |
107 | 107 | |
108 | - if ( is_wp_error( $result ) ) { |
|
108 | + if (is_wp_error($result)) { |
|
109 | 109 | /* @var WP_Error $result */ |
110 | 110 | return $result; |
111 | 111 | } |
112 | 112 | |
113 | - if ( is_array( $result ) ) { |
|
114 | - $result = $this->import_files( $result, $force ); |
|
113 | + if (is_array($result)) { |
|
114 | + $result = $this->import_files($result, $force); |
|
115 | 115 | } |
116 | 116 | |
117 | - if ( is_wp_error( $result ) ) { |
|
117 | + if (is_wp_error($result)) { |
|
118 | 118 | /* @var WP_Error $result */ |
119 | 119 | return $result; |
120 | 120 | } |
121 | 121 | |
122 | - return __( 'Payload processed', 'writing-on-github' ); |
|
122 | + return __('Payload processed', 'writing-on-github'); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | /** |
@@ -129,14 +129,14 @@ discard block |
||
129 | 129 | * |
130 | 130 | * @return bool |
131 | 131 | */ |
132 | - protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
132 | + protected function importable_file(Writing_On_GitHub_File_Info $file) { |
|
133 | 133 | |
134 | 134 | $path = $file->path; |
135 | 135 | |
136 | 136 | // only _pages, _posts and images |
137 | - $prefixs = array( '_pages/', '_posts/', 'images/'); |
|
137 | + $prefixs = array('_pages/', '_posts/', 'images/'); |
|
138 | 138 | foreach ($prefixs as $prefix) { |
139 | - if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { |
|
139 | + if ( ! strncasecmp($path, $prefix, strlen($prefix))) { |
|
140 | 140 | return true; |
141 | 141 | } |
142 | 142 | } |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | * |
151 | 151 | * @return bool |
152 | 152 | */ |
153 | - protected function importable_post( Writing_On_GitHub_Blob $blob ) { |
|
153 | + protected function importable_post(Writing_On_GitHub_Blob $blob) { |
|
154 | 154 | // global $wpdb; |
155 | 155 | |
156 | 156 | // // Skip the repo's readme. |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | // return false; |
164 | 164 | // } |
165 | 165 | |
166 | - if ( ! $blob->has_frontmatter() ) { |
|
166 | + if ( ! $blob->has_frontmatter()) { |
|
167 | 167 | return false; |
168 | 168 | } |
169 | 169 | |
@@ -175,13 +175,13 @@ discard block |
||
175 | 175 | * @param Writing_On_GitHub_Blob $blob |
176 | 176 | * @return WP_Error|bool |
177 | 177 | */ |
178 | - protected function delete_post( Writing_On_GitHub_Blob $blob ) { |
|
178 | + protected function delete_post(Writing_On_GitHub_Blob $blob) { |
|
179 | 179 | $id = $blob->id(); |
180 | - if ( empty( $id ) ) { |
|
180 | + if (empty($id)) { |
|
181 | 181 | return false; |
182 | 182 | } |
183 | - $result = $this->app->database()->delete_post( $id ); |
|
184 | - if ( is_wp_error( $result ) ) { |
|
183 | + $result = $this->app->database()->delete_post($id); |
|
184 | + if (is_wp_error($result)) { |
|
185 | 185 | /* @var WP_Error $result */ |
186 | 186 | return $result; |
187 | 187 | } |
@@ -194,31 +194,31 @@ discard block |
||
194 | 194 | * @param boolean $force |
195 | 195 | * @return WP_Error|bool |
196 | 196 | */ |
197 | - protected function import_post( Writing_On_GitHub_Blob $blob, $force = false ) { |
|
198 | - $post = $this->blob_to_post( $blob, $force ); |
|
197 | + protected function import_post(Writing_On_GitHub_Blob $blob, $force = false) { |
|
198 | + $post = $this->blob_to_post($blob, $force); |
|
199 | 199 | |
200 | - if ( ! $post instanceof Writing_On_GitHub_Post ) { |
|
200 | + if ( ! $post instanceof Writing_On_GitHub_Post) { |
|
201 | 201 | return false; |
202 | 202 | } |
203 | 203 | |
204 | - $result = $this->app->database()->save_post( $post ); |
|
205 | - if ( is_wp_error( $result ) ) { |
|
204 | + $result = $this->app->database()->save_post($post); |
|
205 | + if (is_wp_error($result)) { |
|
206 | 206 | /** @var WP_Error $result */ |
207 | 207 | return $result; |
208 | 208 | } |
209 | 209 | |
210 | - if ( $post->is_new() || |
|
211 | - ! wogh_equal_front_matter( $post, $blob ) ) { |
|
210 | + if ($post->is_new() || |
|
211 | + ! wogh_equal_front_matter($post, $blob)) { |
|
212 | 212 | |
213 | - $result = $this->app->export()->export_post( $post ); |
|
213 | + $result = $this->app->export()->export_post($post); |
|
214 | 214 | |
215 | - if ( is_wp_error( $result ) ) { |
|
215 | + if (is_wp_error($result)) { |
|
216 | 216 | /** @var WP_Error $result */ |
217 | 217 | return $result; |
218 | 218 | } |
219 | 219 | } |
220 | 220 | |
221 | - clean_post_cache( $post->id() ); |
|
221 | + clean_post_cache($post->id()); |
|
222 | 222 | |
223 | 223 | return true; |
224 | 224 | } |
@@ -228,13 +228,13 @@ discard block |
||
228 | 228 | * @param Writing_On_GitHub_Blob $blob |
229 | 229 | * @return bool |
230 | 230 | */ |
231 | - protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
232 | - if ( $blob->has_frontmatter() ) { |
|
231 | + protected function importable_raw_file(Writing_On_GitHub_Blob $blob) { |
|
232 | + if ($blob->has_frontmatter()) { |
|
233 | 233 | return false; |
234 | 234 | } |
235 | 235 | |
236 | 236 | // only images |
237 | - if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
237 | + if (strncasecmp($blob->path(), 'images/', strlen('images/')) != 0) { |
|
238 | 238 | return false; |
239 | 239 | } |
240 | 240 | |
@@ -246,16 +246,16 @@ discard block |
||
246 | 246 | * @param Writing_On_GitHub_Blob $blob |
247 | 247 | * @param bool $is_remove |
248 | 248 | */ |
249 | - protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
249 | + protected function import_raw_file(Writing_On_GitHub_Blob $blob, $is_remove) { |
|
250 | 250 | $arr = wp_upload_dir(); |
251 | 251 | $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
252 | - if ( $is_remove ) { |
|
253 | - if ( file_exists($path) ) { |
|
252 | + if ($is_remove) { |
|
253 | + if (file_exists($path)) { |
|
254 | 254 | unlink($path); |
255 | 255 | } |
256 | 256 | } else { |
257 | 257 | $dirname = dirname($path); |
258 | - if ( ! file_exists($dirname) ) { |
|
258 | + if ( ! file_exists($dirname)) { |
|
259 | 259 | wp_mkdir_p($dirname); |
260 | 260 | } |
261 | 261 | |
@@ -272,67 +272,67 @@ discard block |
||
272 | 272 | * |
273 | 273 | * @return Writing_On_GitHub_Post|false |
274 | 274 | */ |
275 | - protected function blob_to_post( Writing_On_GitHub_Blob $blob, $force = false ) { |
|
276 | - $args = array( 'post_content' => $blob->content_import() ); |
|
275 | + protected function blob_to_post(Writing_On_GitHub_Blob $blob, $force = false) { |
|
276 | + $args = array('post_content' => $blob->content_import()); |
|
277 | 277 | $meta = $blob->meta(); |
278 | 278 | |
279 | 279 | $id = false; |
280 | 280 | |
281 | - if ( ! empty( $meta ) ) { |
|
282 | - if ( array_key_exists( 'layout', $meta ) ) { |
|
281 | + if ( ! empty($meta)) { |
|
282 | + if (array_key_exists('layout', $meta)) { |
|
283 | 283 | $args['post_type'] = $meta['layout']; |
284 | - unset( $meta['layout'] ); |
|
284 | + unset($meta['layout']); |
|
285 | 285 | } |
286 | 286 | |
287 | - if ( array_key_exists( 'published', $meta ) ) { |
|
287 | + if (array_key_exists('published', $meta)) { |
|
288 | 288 | $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
289 | - unset( $meta['published'] ); |
|
289 | + unset($meta['published']); |
|
290 | 290 | } |
291 | 291 | |
292 | - if ( array_key_exists( 'post_title', $meta ) ) { |
|
292 | + if (array_key_exists('post_title', $meta)) { |
|
293 | 293 | $args['post_title'] = $meta['post_title']; |
294 | - unset( $meta['post_title'] ); |
|
294 | + unset($meta['post_title']); |
|
295 | 295 | } |
296 | 296 | |
297 | - if ( array_key_exists( 'post_name', $meta ) ) { |
|
297 | + if (array_key_exists('post_name', $meta)) { |
|
298 | 298 | $args['post_name'] = $meta['post_name']; |
299 | - unset( $meta['post_name'] ); |
|
299 | + unset($meta['post_name']); |
|
300 | 300 | } |
301 | 301 | |
302 | - if ( array_key_exists( 'ID', $meta ) ) { |
|
302 | + if (array_key_exists('ID', $meta)) { |
|
303 | 303 | $id = $args['ID'] = $meta['ID']; |
304 | 304 | $blob->set_id($id); |
305 | - unset( $meta['ID'] ); |
|
305 | + unset($meta['ID']); |
|
306 | 306 | } |
307 | 307 | |
308 | - if ( array_key_exists( 'post_date', $meta ) ) { |
|
309 | - if ( empty( $meta['post_date'] ) ) { |
|
310 | - $meta['post_date'] = current_time( 'mysql' ); |
|
308 | + if (array_key_exists('post_date', $meta)) { |
|
309 | + if (empty($meta['post_date'])) { |
|
310 | + $meta['post_date'] = current_time('mysql'); |
|
311 | 311 | } |
312 | 312 | |
313 | 313 | $args['post_date'] = $meta['post_date']; |
314 | - unset( $meta['post_date'] ); |
|
314 | + unset($meta['post_date']); |
|
315 | 315 | } |
316 | 316 | } |
317 | 317 | |
318 | 318 | $meta['_wogh_sha'] = $blob->sha(); |
319 | 319 | |
320 | - if ( ! $force && $id ) { |
|
321 | - $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
322 | - $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
320 | + if ( ! $force && $id) { |
|
321 | + $old_sha = get_post_meta($id, '_wogh_sha', true); |
|
322 | + $old_github_path = get_post_meta($id, '_wogh_github_path', true); |
|
323 | 323 | |
324 | 324 | // dont save post when has same sha |
325 | - if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
326 | - $old_github_path && $old_github_path == $blob->path() ) { |
|
325 | + if ($old_sha && $old_sha == $meta['_wogh_sha'] && |
|
326 | + $old_github_path && $old_github_path == $blob->path()) { |
|
327 | 327 | return false; |
328 | 328 | } |
329 | 329 | } |
330 | 330 | |
331 | - $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
332 | - $post->set_old_github_path( $blob->path() ); |
|
333 | - $post->set_meta( $meta ); |
|
334 | - $post->set_blob( $blob ); |
|
335 | - $blob->set_id( $post->id() ); |
|
331 | + $post = new Writing_On_GitHub_Post($args, $this->app->api()); |
|
332 | + $post->set_old_github_path($blob->path()); |
|
333 | + $post->set_meta($meta); |
|
334 | + $post->set_blob($blob); |
|
335 | + $blob->set_id($post->id()); |
|
336 | 336 | |
337 | 337 | return $post; |
338 | 338 | } |
@@ -5,216 +5,216 @@ |
||
5 | 5 | */ |
6 | 6 | class Writing_On_GitHub_Persist_Client_Test extends Writing_On_GitHub_Base_Client_Test { |
7 | 7 | |
8 | - public function setUp() { |
|
9 | - parent::setUp(); |
|
10 | - |
|
11 | - $this->persist = new Writing_On_GitHub_Persist_Client( $this->app ); |
|
12 | - // $this->commit |
|
13 | - // ->shouldReceive( 'tree' ) |
|
14 | - // ->andReturn( $this->tree ); |
|
15 | - } |
|
16 | - |
|
17 | - public function test_should_delete_file() { |
|
18 | - // $this->tree->shouldReceive(); |
|
19 | - |
|
20 | - $this->set_delete_contents( true, '_posts_test.md' ); |
|
21 | - $this->persist->delete_file( '_posts_test.md', 'mysha', 'this is message for delete file' ); |
|
22 | - } |
|
23 | - |
|
24 | - // public function test_should_not_add_commit_if_no_change() { |
|
25 | - // $this->tree |
|
26 | - // ->shouldReceive( 'is_changed' ) |
|
27 | - // ->once() |
|
28 | - // ->andReturn( false ); |
|
29 | - |
|
30 | - // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
31 | - // $this->assertSame( 'no_commit', $error->get_error_code() ); |
|
32 | - // } |
|
33 | - |
|
34 | - // public function test_should_fail_if_create_tree_fails() { |
|
35 | - // $this->tree |
|
36 | - // ->shouldReceive( 'is_changed' ) |
|
37 | - // ->once() |
|
38 | - // ->andReturn( true ); |
|
39 | - // $this->tree |
|
40 | - // ->shouldReceive( 'to_body' ) |
|
41 | - // ->once() |
|
42 | - // ->andReturn( |
|
43 | - // array( |
|
44 | - // 'tree' => array( |
|
45 | - // array( |
|
46 | - // 'path' => '_posts/2015-10-23-new-post.md', |
|
47 | - // 'type' => 'blob', |
|
48 | - // 'content' => 'Post content 1', |
|
49 | - // 'mode' => '100644', |
|
50 | - // ) |
|
51 | - // ) |
|
52 | - // ) |
|
53 | - // ); |
|
54 | - // $this->set_post_trees( false ); |
|
55 | - |
|
56 | - // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
57 | - // $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
58 | - // } |
|
59 | - |
|
60 | - // public function test_should_fail_if_create_commit_fails() { |
|
61 | - // $this->tree |
|
62 | - // ->shouldReceive( 'is_changed' ) |
|
63 | - // ->once() |
|
64 | - // ->andReturn( true ); |
|
65 | - // $this->tree |
|
66 | - // ->shouldReceive( 'to_body' ) |
|
67 | - // ->once() |
|
68 | - // ->andReturn( |
|
69 | - // array( |
|
70 | - // 'tree' => array( |
|
71 | - // array( |
|
72 | - // 'path' => '_posts/2015-10-23-new-post.md', |
|
73 | - // 'type' => 'blob', |
|
74 | - // 'content' => 'Post content 1', |
|
75 | - // 'mode' => '100644', |
|
76 | - // ) |
|
77 | - // ) |
|
78 | - // ) |
|
79 | - // ); |
|
80 | - // $this->tree |
|
81 | - // ->shouldReceive( 'set_sha' ) |
|
82 | - // ->once() |
|
83 | - // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
84 | - // $this->commit |
|
85 | - // ->shouldReceive( 'to_body' ) |
|
86 | - // ->once() |
|
87 | - // ->andReturn( array( |
|
88 | - // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
89 | - // 'message' => 'Commit message', |
|
90 | - // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
91 | - // ) ); |
|
92 | - // $this->set_post_trees( true ); |
|
93 | - // $this->set_post_commits( false ); |
|
94 | - |
|
95 | - // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
96 | - // $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
97 | - // } |
|
98 | - |
|
99 | - // public function test_should_fail_if_update_master_fails() { |
|
100 | - // $this->tree |
|
101 | - // ->shouldReceive( 'is_changed' ) |
|
102 | - // ->once() |
|
103 | - // ->andReturn( true ); |
|
104 | - // $this->tree |
|
105 | - // ->shouldReceive( 'to_body' ) |
|
106 | - // ->once() |
|
107 | - // ->andReturn( |
|
108 | - // array( |
|
109 | - // 'tree' => array( |
|
110 | - // array( |
|
111 | - // 'path' => '_posts/2015-10-23-new-post.md', |
|
112 | - // 'type' => 'blob', |
|
113 | - // 'content' => 'Post content 1', |
|
114 | - // 'mode' => '100644', |
|
115 | - // ) |
|
116 | - // ) |
|
117 | - // ) |
|
118 | - // ); |
|
119 | - // $this->tree |
|
120 | - // ->shouldReceive( 'set_sha' ) |
|
121 | - // ->once() |
|
122 | - // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
123 | - // $this->commit |
|
124 | - // ->shouldReceive( 'to_body' ) |
|
125 | - // ->once() |
|
126 | - // ->andReturn( array( |
|
127 | - // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
128 | - // 'message' => 'Commit message', |
|
129 | - // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
130 | - // ) ); |
|
131 | - // $this->set_post_trees( true ); |
|
132 | - // $this->set_post_commits( true ); |
|
133 | - // $this->set_patch_refs_heads_master( false ); |
|
134 | - |
|
135 | - // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
136 | - // $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
137 | - // } |
|
138 | - |
|
139 | - // public function test_should_create_anonymous_commit() { |
|
140 | - // $this->tree |
|
141 | - // ->shouldReceive( 'is_changed' ) |
|
142 | - // ->once() |
|
143 | - // ->andReturn( true ); |
|
144 | - // $this->tree |
|
145 | - // ->shouldReceive( 'to_body' ) |
|
146 | - // ->once() |
|
147 | - // ->andReturn( |
|
148 | - // array( |
|
149 | - // 'tree' => array( |
|
150 | - // array( |
|
151 | - // 'path' => '_posts/2015-10-23-new-post.md', |
|
152 | - // 'type' => 'blob', |
|
153 | - // 'content' => 'Post content 1', |
|
154 | - // 'mode' => '100644', |
|
155 | - // ) |
|
156 | - // ) |
|
157 | - // ) |
|
158 | - // ); |
|
159 | - // $this->tree |
|
160 | - // ->shouldReceive( 'set_sha' ) |
|
161 | - // ->once() |
|
162 | - // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
163 | - // $this->commit |
|
164 | - // ->shouldReceive( 'to_body' ) |
|
165 | - // ->once() |
|
166 | - // ->andReturn( array( |
|
167 | - // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
168 | - // 'message' => 'Commit message', |
|
169 | - // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
170 | - // ) ); |
|
171 | - // $this->set_post_trees( true ); |
|
172 | - // $this->set_post_commits( true ); |
|
173 | - // $this->set_patch_refs_heads_master( true ); |
|
174 | - |
|
175 | - // $this->assertTrue( $this->persist->commit( $this->commit ) ); |
|
176 | - // } |
|
177 | - |
|
178 | - // public function test_should_create_authored_commit() { |
|
179 | - // $this->tree |
|
180 | - // ->shouldReceive( 'is_changed' ) |
|
181 | - // ->once() |
|
182 | - // ->andReturn( true ); |
|
183 | - // $this->tree |
|
184 | - // ->shouldReceive( 'to_body' ) |
|
185 | - // ->once() |
|
186 | - // ->andReturn( |
|
187 | - // array( |
|
188 | - // 'tree' => array( |
|
189 | - // array( |
|
190 | - // 'path' => '_posts/2015-10-23-new-post.md', |
|
191 | - // 'type' => 'blob', |
|
192 | - // 'content' => 'Post content 1', |
|
193 | - // 'mode' => '100644', |
|
194 | - // ) |
|
195 | - // ) |
|
196 | - // ) |
|
197 | - // ); |
|
198 | - // $this->tree |
|
199 | - // ->shouldReceive( 'set_sha' ) |
|
200 | - // ->once() |
|
201 | - // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
202 | - // $this->commit |
|
203 | - // ->shouldReceive( 'to_body' ) |
|
204 | - // ->once() |
|
205 | - // ->andReturn( array( |
|
206 | - // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
207 | - // 'message' => 'Commit message', |
|
208 | - // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
209 | - // ) ); |
|
210 | - // $this->set_post_trees( true ); |
|
211 | - // update_option( '_wogh_export_user_id', $this->factory->user->create( array( |
|
212 | - // 'display_name' => 'James DiGioia', |
|
213 | - // 'user_email' => '[email protected]', |
|
214 | - // ) ) ); |
|
215 | - // $this->set_post_commits( true, false ); |
|
216 | - // $this->set_patch_refs_heads_master( true ); |
|
217 | - |
|
218 | - // $this->assertTrue( $this->persist->commit( $this->commit ) ); |
|
219 | - // } |
|
8 | + public function setUp() { |
|
9 | + parent::setUp(); |
|
10 | + |
|
11 | + $this->persist = new Writing_On_GitHub_Persist_Client( $this->app ); |
|
12 | + // $this->commit |
|
13 | + // ->shouldReceive( 'tree' ) |
|
14 | + // ->andReturn( $this->tree ); |
|
15 | + } |
|
16 | + |
|
17 | + public function test_should_delete_file() { |
|
18 | + // $this->tree->shouldReceive(); |
|
19 | + |
|
20 | + $this->set_delete_contents( true, '_posts_test.md' ); |
|
21 | + $this->persist->delete_file( '_posts_test.md', 'mysha', 'this is message for delete file' ); |
|
22 | + } |
|
23 | + |
|
24 | + // public function test_should_not_add_commit_if_no_change() { |
|
25 | + // $this->tree |
|
26 | + // ->shouldReceive( 'is_changed' ) |
|
27 | + // ->once() |
|
28 | + // ->andReturn( false ); |
|
29 | + |
|
30 | + // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
31 | + // $this->assertSame( 'no_commit', $error->get_error_code() ); |
|
32 | + // } |
|
33 | + |
|
34 | + // public function test_should_fail_if_create_tree_fails() { |
|
35 | + // $this->tree |
|
36 | + // ->shouldReceive( 'is_changed' ) |
|
37 | + // ->once() |
|
38 | + // ->andReturn( true ); |
|
39 | + // $this->tree |
|
40 | + // ->shouldReceive( 'to_body' ) |
|
41 | + // ->once() |
|
42 | + // ->andReturn( |
|
43 | + // array( |
|
44 | + // 'tree' => array( |
|
45 | + // array( |
|
46 | + // 'path' => '_posts/2015-10-23-new-post.md', |
|
47 | + // 'type' => 'blob', |
|
48 | + // 'content' => 'Post content 1', |
|
49 | + // 'mode' => '100644', |
|
50 | + // ) |
|
51 | + // ) |
|
52 | + // ) |
|
53 | + // ); |
|
54 | + // $this->set_post_trees( false ); |
|
55 | + |
|
56 | + // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
57 | + // $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
58 | + // } |
|
59 | + |
|
60 | + // public function test_should_fail_if_create_commit_fails() { |
|
61 | + // $this->tree |
|
62 | + // ->shouldReceive( 'is_changed' ) |
|
63 | + // ->once() |
|
64 | + // ->andReturn( true ); |
|
65 | + // $this->tree |
|
66 | + // ->shouldReceive( 'to_body' ) |
|
67 | + // ->once() |
|
68 | + // ->andReturn( |
|
69 | + // array( |
|
70 | + // 'tree' => array( |
|
71 | + // array( |
|
72 | + // 'path' => '_posts/2015-10-23-new-post.md', |
|
73 | + // 'type' => 'blob', |
|
74 | + // 'content' => 'Post content 1', |
|
75 | + // 'mode' => '100644', |
|
76 | + // ) |
|
77 | + // ) |
|
78 | + // ) |
|
79 | + // ); |
|
80 | + // $this->tree |
|
81 | + // ->shouldReceive( 'set_sha' ) |
|
82 | + // ->once() |
|
83 | + // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
84 | + // $this->commit |
|
85 | + // ->shouldReceive( 'to_body' ) |
|
86 | + // ->once() |
|
87 | + // ->andReturn( array( |
|
88 | + // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
89 | + // 'message' => 'Commit message', |
|
90 | + // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
91 | + // ) ); |
|
92 | + // $this->set_post_trees( true ); |
|
93 | + // $this->set_post_commits( false ); |
|
94 | + |
|
95 | + // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
96 | + // $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
97 | + // } |
|
98 | + |
|
99 | + // public function test_should_fail_if_update_master_fails() { |
|
100 | + // $this->tree |
|
101 | + // ->shouldReceive( 'is_changed' ) |
|
102 | + // ->once() |
|
103 | + // ->andReturn( true ); |
|
104 | + // $this->tree |
|
105 | + // ->shouldReceive( 'to_body' ) |
|
106 | + // ->once() |
|
107 | + // ->andReturn( |
|
108 | + // array( |
|
109 | + // 'tree' => array( |
|
110 | + // array( |
|
111 | + // 'path' => '_posts/2015-10-23-new-post.md', |
|
112 | + // 'type' => 'blob', |
|
113 | + // 'content' => 'Post content 1', |
|
114 | + // 'mode' => '100644', |
|
115 | + // ) |
|
116 | + // ) |
|
117 | + // ) |
|
118 | + // ); |
|
119 | + // $this->tree |
|
120 | + // ->shouldReceive( 'set_sha' ) |
|
121 | + // ->once() |
|
122 | + // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
123 | + // $this->commit |
|
124 | + // ->shouldReceive( 'to_body' ) |
|
125 | + // ->once() |
|
126 | + // ->andReturn( array( |
|
127 | + // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
128 | + // 'message' => 'Commit message', |
|
129 | + // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
130 | + // ) ); |
|
131 | + // $this->set_post_trees( true ); |
|
132 | + // $this->set_post_commits( true ); |
|
133 | + // $this->set_patch_refs_heads_master( false ); |
|
134 | + |
|
135 | + // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
136 | + // $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
137 | + // } |
|
138 | + |
|
139 | + // public function test_should_create_anonymous_commit() { |
|
140 | + // $this->tree |
|
141 | + // ->shouldReceive( 'is_changed' ) |
|
142 | + // ->once() |
|
143 | + // ->andReturn( true ); |
|
144 | + // $this->tree |
|
145 | + // ->shouldReceive( 'to_body' ) |
|
146 | + // ->once() |
|
147 | + // ->andReturn( |
|
148 | + // array( |
|
149 | + // 'tree' => array( |
|
150 | + // array( |
|
151 | + // 'path' => '_posts/2015-10-23-new-post.md', |
|
152 | + // 'type' => 'blob', |
|
153 | + // 'content' => 'Post content 1', |
|
154 | + // 'mode' => '100644', |
|
155 | + // ) |
|
156 | + // ) |
|
157 | + // ) |
|
158 | + // ); |
|
159 | + // $this->tree |
|
160 | + // ->shouldReceive( 'set_sha' ) |
|
161 | + // ->once() |
|
162 | + // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
163 | + // $this->commit |
|
164 | + // ->shouldReceive( 'to_body' ) |
|
165 | + // ->once() |
|
166 | + // ->andReturn( array( |
|
167 | + // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
168 | + // 'message' => 'Commit message', |
|
169 | + // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
170 | + // ) ); |
|
171 | + // $this->set_post_trees( true ); |
|
172 | + // $this->set_post_commits( true ); |
|
173 | + // $this->set_patch_refs_heads_master( true ); |
|
174 | + |
|
175 | + // $this->assertTrue( $this->persist->commit( $this->commit ) ); |
|
176 | + // } |
|
177 | + |
|
178 | + // public function test_should_create_authored_commit() { |
|
179 | + // $this->tree |
|
180 | + // ->shouldReceive( 'is_changed' ) |
|
181 | + // ->once() |
|
182 | + // ->andReturn( true ); |
|
183 | + // $this->tree |
|
184 | + // ->shouldReceive( 'to_body' ) |
|
185 | + // ->once() |
|
186 | + // ->andReturn( |
|
187 | + // array( |
|
188 | + // 'tree' => array( |
|
189 | + // array( |
|
190 | + // 'path' => '_posts/2015-10-23-new-post.md', |
|
191 | + // 'type' => 'blob', |
|
192 | + // 'content' => 'Post content 1', |
|
193 | + // 'mode' => '100644', |
|
194 | + // ) |
|
195 | + // ) |
|
196 | + // ) |
|
197 | + // ); |
|
198 | + // $this->tree |
|
199 | + // ->shouldReceive( 'set_sha' ) |
|
200 | + // ->once() |
|
201 | + // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
202 | + // $this->commit |
|
203 | + // ->shouldReceive( 'to_body' ) |
|
204 | + // ->once() |
|
205 | + // ->andReturn( array( |
|
206 | + // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
207 | + // 'message' => 'Commit message', |
|
208 | + // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
209 | + // ) ); |
|
210 | + // $this->set_post_trees( true ); |
|
211 | + // update_option( '_wogh_export_user_id', $this->factory->user->create( array( |
|
212 | + // 'display_name' => 'James DiGioia', |
|
213 | + // 'user_email' => '[email protected]', |
|
214 | + // ) ) ); |
|
215 | + // $this->set_post_commits( true, false ); |
|
216 | + // $this->set_patch_refs_heads_master( true ); |
|
217 | + |
|
218 | + // $this->assertTrue( $this->persist->commit( $this->commit ) ); |
|
219 | + // } |
|
220 | 220 | } |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | public function setUp() { |
9 | 9 | parent::setUp(); |
10 | 10 | |
11 | - $this->persist = new Writing_On_GitHub_Persist_Client( $this->app ); |
|
11 | + $this->persist = new Writing_On_GitHub_Persist_Client($this->app); |
|
12 | 12 | // $this->commit |
13 | 13 | // ->shouldReceive( 'tree' ) |
14 | 14 | // ->andReturn( $this->tree ); |
@@ -17,8 +17,8 @@ discard block |
||
17 | 17 | public function test_should_delete_file() { |
18 | 18 | // $this->tree->shouldReceive(); |
19 | 19 | |
20 | - $this->set_delete_contents( true, '_posts_test.md' ); |
|
21 | - $this->persist->delete_file( '_posts_test.md', 'mysha', 'this is message for delete file' ); |
|
20 | + $this->set_delete_contents(true, '_posts_test.md'); |
|
21 | + $this->persist->delete_file('_posts_test.md', 'mysha', 'this is message for delete file'); |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | // public function test_should_not_add_commit_if_no_change() { |
@@ -6,147 +6,147 @@ |
||
6 | 6 | */ |
7 | 7 | class Writing_On_GitHub_Fetch_Client_Test extends Writing_On_GitHub_Base_Client_Test { |
8 | 8 | |
9 | - public function setUp() { |
|
10 | - parent::setUp(); |
|
11 | - |
|
12 | - $this->fetch = new Writing_On_GitHub_Fetch_Client( $this->app ); |
|
13 | - } |
|
14 | - |
|
15 | - public function test_should_fail_if_missing_token() { |
|
16 | - delete_option( Base::TOKEN_OPTION_KEY ); |
|
17 | - |
|
18 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
19 | - $this->assertSame( 'missing_token', $error->get_error_code() ); |
|
20 | - } |
|
21 | - |
|
22 | - public function test_should_fail_if_missing_repo() { |
|
23 | - delete_option( Base::REPO_OPTION_KEY ); |
|
24 | - |
|
25 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
26 | - $this->assertSame( 'missing_repository', $error->get_error_code() ); |
|
27 | - } |
|
28 | - |
|
29 | - public function test_should_fail_if_malformed_repo() { |
|
30 | - // If you find a particular name passing that shouldn't, |
|
31 | - // add it to the list here and make it pass. |
|
32 | - $this->malformed_repo( 'repositoryname' ); |
|
33 | - } |
|
34 | - |
|
35 | - public function test_compare() { |
|
36 | - $this->set_get_trees( true, 'master' ); |
|
37 | - $this->set_get_compare( true ); |
|
38 | - $infos = $this->fetch->compare( '861f87e8851b8debb78db548269d29f8da4d94ac' ); |
|
39 | - $this->assertCount( 1, $infos ); |
|
40 | - $this->assertInstanceOf( 'Writing_On_GitHub_File_Info', $infos[0] ); |
|
41 | - } |
|
42 | - |
|
43 | - public function test_should_return_files_with_tree() { |
|
44 | - // $this->set_get_refs_heads_master( true ); |
|
45 | - // $this->set_get_commits( true ); |
|
46 | - $this->set_get_trees( true, 'master' ); |
|
47 | - |
|
48 | - $this->assertCount( 3, $this->fetch->tree_recursive() ); |
|
49 | - } |
|
50 | - |
|
51 | - public function test_should_fail_if_cant_fetch_tree() { |
|
52 | - $this->set_get_trees( false, 'master' ); |
|
53 | - |
|
54 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
55 | - $this->assertSame( '422_unprocessable_entity', $error->get_error_code() ); |
|
56 | - } |
|
57 | - |
|
58 | - public function test_should_return_commit_with_no_blobs_if_api_fails() { |
|
59 | - $this->set_get_trees( true, 'master' ); |
|
60 | - $this->set_get_blobs( false ); |
|
61 | - |
|
62 | - $this->assertCount( 3, $files = $this->fetch->tree_recursive() ); |
|
63 | - |
|
64 | - foreach ( $files as $file ) { |
|
65 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->blob( $file ) ); |
|
66 | - $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - // public function test_should_return_and_validate_full_commit() { |
|
71 | - // $this->set_get_refs_heads_master( true ); |
|
72 | - // $this->set_get_commits( true ); |
|
73 | - // $this->set_get_trees( true ); |
|
74 | - // $this->set_get_blobs( true ); |
|
75 | - // $this->api_cache |
|
76 | - // ->shouldReceive( 'set_blob' ) |
|
77 | - // ->times( 3 ) |
|
78 | - // ->with( |
|
79 | - // Mockery::anyOf( |
|
80 | - // '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
|
81 | - // '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
|
82 | - // '2d73165945b0ccbe4932f1363457986b0ed49f19' |
|
83 | - // ), |
|
84 | - // Mockery::type( 'Writing_On_GitHub_Blob' ) |
|
85 | - // ) |
|
86 | - // ->andReturnUsing( function ( $sha, $blob ) { |
|
87 | - // return $blob; |
|
88 | - // } ); |
|
89 | - // $this->api_cache |
|
90 | - // ->shouldReceive( 'set_tree' ) |
|
91 | - // ->once() |
|
92 | - // ->with( '9108868e3800bec6763e51beb0d33e15036c3626', Mockery::type( 'Writing_On_GitHub_Tree' ) ) |
|
93 | - // ->andReturnUsing( function ( $sha, $tree ) { |
|
94 | - // return $tree; |
|
95 | - // } ); |
|
96 | - // $this->api_cache |
|
97 | - // ->shouldReceive( 'set_commit' ) |
|
98 | - // ->once() |
|
99 | - // ->with( 'db2510854e6aeab68ead26b48328b19f4bdf926e', Mockery::type( 'Writing_On_GitHub_Commit' ) ) |
|
100 | - // ->andReturnUsing( function ( $sha, $commit ) { |
|
101 | - // return $commit; |
|
102 | - // } ); |
|
103 | - |
|
104 | - // $this->assertInstanceOf( 'Writing_On_GitHub_Commit', $master = $this->fetch->master() ); |
|
105 | - |
|
106 | - // /** |
|
107 | - // * Validate the commit's api data mapped correctly. |
|
108 | - // */ |
|
109 | - // $this->assertSame( '7497c0574b9430ff5e5521b4572b7452ea36a056', $master->sha() ); |
|
110 | - // $this->assertSame( '[email protected]', $master->author()->email ); |
|
111 | - // $this->assertSame( '2015-11-02T00:36:54Z', $master->author()->date ); |
|
112 | - // $this->assertSame( '[email protected]', $master->committer()->email ); |
|
113 | - // $this->assertSame( '2015-11-02T00:36:54Z', $master->committer()->date ); |
|
114 | - // $this->assertSame( 'Initial full site export - wogh', $master->message() ); |
|
115 | - // $this->assertCount( 1, $parents = $master->parents() ); |
|
116 | - // $this->assertSame( 'db2510854e6aeab68ead26b48328b19f4bdf926e', $parents[0]->sha ); |
|
117 | - |
|
118 | - // $this->assertInstanceOf( 'Writing_On_GitHub_Tree', $tree = $master->tree() ); |
|
119 | - |
|
120 | - // /** |
|
121 | - // * Validate the tree's api data mapped correctly. |
|
122 | - // */ |
|
123 | - // $this->assertSame( '9108868e3800bec6763e51beb0d33e15036c3626', $tree->sha() ); |
|
124 | - |
|
125 | - // $this->assertCount( 3, $blobs = $tree->blobs() ); |
|
126 | - |
|
127 | - // /** |
|
128 | - // * Validate the blobs' api data mapped correctly. |
|
129 | - // */ |
|
130 | - // $blobs = $tree->blobs(); |
|
131 | - // $this->assertCount( 3, $blobs ); |
|
132 | - // foreach ( $blobs as $blob ) { |
|
133 | - // $this->assertTrue( in_array( $blob->sha(), array( |
|
134 | - // '2d73165945b0ccbe4932f1363457986b0ed49f19', |
|
135 | - // '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
|
136 | - // '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
|
137 | - // ) ) ); |
|
138 | - // $this->assertTrue( in_array( $blob->path(), array( |
|
139 | - // '_pages/sample-page.md', |
|
140 | - // '_posts/2015-11-02-hello-world.md', |
|
141 | - // 'README.md', |
|
142 | - // ) ) ); |
|
143 | - // } |
|
144 | - // } |
|
145 | - |
|
146 | - protected function malformed_repo( $repo ) { |
|
147 | - update_option( Base::REPO_OPTION_KEY, $repo ); |
|
148 | - |
|
149 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
150 | - $this->assertSame( 'malformed_repository', $error->get_error_code() ); |
|
151 | - } |
|
9 | + public function setUp() { |
|
10 | + parent::setUp(); |
|
11 | + |
|
12 | + $this->fetch = new Writing_On_GitHub_Fetch_Client( $this->app ); |
|
13 | + } |
|
14 | + |
|
15 | + public function test_should_fail_if_missing_token() { |
|
16 | + delete_option( Base::TOKEN_OPTION_KEY ); |
|
17 | + |
|
18 | + $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
19 | + $this->assertSame( 'missing_token', $error->get_error_code() ); |
|
20 | + } |
|
21 | + |
|
22 | + public function test_should_fail_if_missing_repo() { |
|
23 | + delete_option( Base::REPO_OPTION_KEY ); |
|
24 | + |
|
25 | + $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
26 | + $this->assertSame( 'missing_repository', $error->get_error_code() ); |
|
27 | + } |
|
28 | + |
|
29 | + public function test_should_fail_if_malformed_repo() { |
|
30 | + // If you find a particular name passing that shouldn't, |
|
31 | + // add it to the list here and make it pass. |
|
32 | + $this->malformed_repo( 'repositoryname' ); |
|
33 | + } |
|
34 | + |
|
35 | + public function test_compare() { |
|
36 | + $this->set_get_trees( true, 'master' ); |
|
37 | + $this->set_get_compare( true ); |
|
38 | + $infos = $this->fetch->compare( '861f87e8851b8debb78db548269d29f8da4d94ac' ); |
|
39 | + $this->assertCount( 1, $infos ); |
|
40 | + $this->assertInstanceOf( 'Writing_On_GitHub_File_Info', $infos[0] ); |
|
41 | + } |
|
42 | + |
|
43 | + public function test_should_return_files_with_tree() { |
|
44 | + // $this->set_get_refs_heads_master( true ); |
|
45 | + // $this->set_get_commits( true ); |
|
46 | + $this->set_get_trees( true, 'master' ); |
|
47 | + |
|
48 | + $this->assertCount( 3, $this->fetch->tree_recursive() ); |
|
49 | + } |
|
50 | + |
|
51 | + public function test_should_fail_if_cant_fetch_tree() { |
|
52 | + $this->set_get_trees( false, 'master' ); |
|
53 | + |
|
54 | + $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
55 | + $this->assertSame( '422_unprocessable_entity', $error->get_error_code() ); |
|
56 | + } |
|
57 | + |
|
58 | + public function test_should_return_commit_with_no_blobs_if_api_fails() { |
|
59 | + $this->set_get_trees( true, 'master' ); |
|
60 | + $this->set_get_blobs( false ); |
|
61 | + |
|
62 | + $this->assertCount( 3, $files = $this->fetch->tree_recursive() ); |
|
63 | + |
|
64 | + foreach ( $files as $file ) { |
|
65 | + $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->blob( $file ) ); |
|
66 | + $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + // public function test_should_return_and_validate_full_commit() { |
|
71 | + // $this->set_get_refs_heads_master( true ); |
|
72 | + // $this->set_get_commits( true ); |
|
73 | + // $this->set_get_trees( true ); |
|
74 | + // $this->set_get_blobs( true ); |
|
75 | + // $this->api_cache |
|
76 | + // ->shouldReceive( 'set_blob' ) |
|
77 | + // ->times( 3 ) |
|
78 | + // ->with( |
|
79 | + // Mockery::anyOf( |
|
80 | + // '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
|
81 | + // '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
|
82 | + // '2d73165945b0ccbe4932f1363457986b0ed49f19' |
|
83 | + // ), |
|
84 | + // Mockery::type( 'Writing_On_GitHub_Blob' ) |
|
85 | + // ) |
|
86 | + // ->andReturnUsing( function ( $sha, $blob ) { |
|
87 | + // return $blob; |
|
88 | + // } ); |
|
89 | + // $this->api_cache |
|
90 | + // ->shouldReceive( 'set_tree' ) |
|
91 | + // ->once() |
|
92 | + // ->with( '9108868e3800bec6763e51beb0d33e15036c3626', Mockery::type( 'Writing_On_GitHub_Tree' ) ) |
|
93 | + // ->andReturnUsing( function ( $sha, $tree ) { |
|
94 | + // return $tree; |
|
95 | + // } ); |
|
96 | + // $this->api_cache |
|
97 | + // ->shouldReceive( 'set_commit' ) |
|
98 | + // ->once() |
|
99 | + // ->with( 'db2510854e6aeab68ead26b48328b19f4bdf926e', Mockery::type( 'Writing_On_GitHub_Commit' ) ) |
|
100 | + // ->andReturnUsing( function ( $sha, $commit ) { |
|
101 | + // return $commit; |
|
102 | + // } ); |
|
103 | + |
|
104 | + // $this->assertInstanceOf( 'Writing_On_GitHub_Commit', $master = $this->fetch->master() ); |
|
105 | + |
|
106 | + // /** |
|
107 | + // * Validate the commit's api data mapped correctly. |
|
108 | + // */ |
|
109 | + // $this->assertSame( '7497c0574b9430ff5e5521b4572b7452ea36a056', $master->sha() ); |
|
110 | + // $this->assertSame( '[email protected]', $master->author()->email ); |
|
111 | + // $this->assertSame( '2015-11-02T00:36:54Z', $master->author()->date ); |
|
112 | + // $this->assertSame( '[email protected]', $master->committer()->email ); |
|
113 | + // $this->assertSame( '2015-11-02T00:36:54Z', $master->committer()->date ); |
|
114 | + // $this->assertSame( 'Initial full site export - wogh', $master->message() ); |
|
115 | + // $this->assertCount( 1, $parents = $master->parents() ); |
|
116 | + // $this->assertSame( 'db2510854e6aeab68ead26b48328b19f4bdf926e', $parents[0]->sha ); |
|
117 | + |
|
118 | + // $this->assertInstanceOf( 'Writing_On_GitHub_Tree', $tree = $master->tree() ); |
|
119 | + |
|
120 | + // /** |
|
121 | + // * Validate the tree's api data mapped correctly. |
|
122 | + // */ |
|
123 | + // $this->assertSame( '9108868e3800bec6763e51beb0d33e15036c3626', $tree->sha() ); |
|
124 | + |
|
125 | + // $this->assertCount( 3, $blobs = $tree->blobs() ); |
|
126 | + |
|
127 | + // /** |
|
128 | + // * Validate the blobs' api data mapped correctly. |
|
129 | + // */ |
|
130 | + // $blobs = $tree->blobs(); |
|
131 | + // $this->assertCount( 3, $blobs ); |
|
132 | + // foreach ( $blobs as $blob ) { |
|
133 | + // $this->assertTrue( in_array( $blob->sha(), array( |
|
134 | + // '2d73165945b0ccbe4932f1363457986b0ed49f19', |
|
135 | + // '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
|
136 | + // '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
|
137 | + // ) ) ); |
|
138 | + // $this->assertTrue( in_array( $blob->path(), array( |
|
139 | + // '_pages/sample-page.md', |
|
140 | + // '_posts/2015-11-02-hello-world.md', |
|
141 | + // 'README.md', |
|
142 | + // ) ) ); |
|
143 | + // } |
|
144 | + // } |
|
145 | + |
|
146 | + protected function malformed_repo( $repo ) { |
|
147 | + update_option( Base::REPO_OPTION_KEY, $repo ); |
|
148 | + |
|
149 | + $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
150 | + $this->assertSame( 'malformed_repository', $error->get_error_code() ); |
|
151 | + } |
|
152 | 152 | } |
@@ -9,61 +9,61 @@ discard block |
||
9 | 9 | public function setUp() { |
10 | 10 | parent::setUp(); |
11 | 11 | |
12 | - $this->fetch = new Writing_On_GitHub_Fetch_Client( $this->app ); |
|
12 | + $this->fetch = new Writing_On_GitHub_Fetch_Client($this->app); |
|
13 | 13 | } |
14 | 14 | |
15 | 15 | public function test_should_fail_if_missing_token() { |
16 | - delete_option( Base::TOKEN_OPTION_KEY ); |
|
16 | + delete_option(Base::TOKEN_OPTION_KEY); |
|
17 | 17 | |
18 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
19 | - $this->assertSame( 'missing_token', $error->get_error_code() ); |
|
18 | + $this->assertInstanceOf('WP_Error', $error = $this->fetch->tree_recursive()); |
|
19 | + $this->assertSame('missing_token', $error->get_error_code()); |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | public function test_should_fail_if_missing_repo() { |
23 | - delete_option( Base::REPO_OPTION_KEY ); |
|
23 | + delete_option(Base::REPO_OPTION_KEY); |
|
24 | 24 | |
25 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
26 | - $this->assertSame( 'missing_repository', $error->get_error_code() ); |
|
25 | + $this->assertInstanceOf('WP_Error', $error = $this->fetch->tree_recursive()); |
|
26 | + $this->assertSame('missing_repository', $error->get_error_code()); |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | public function test_should_fail_if_malformed_repo() { |
30 | 30 | // If you find a particular name passing that shouldn't, |
31 | 31 | // add it to the list here and make it pass. |
32 | - $this->malformed_repo( 'repositoryname' ); |
|
32 | + $this->malformed_repo('repositoryname'); |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | public function test_compare() { |
36 | - $this->set_get_trees( true, 'master' ); |
|
37 | - $this->set_get_compare( true ); |
|
38 | - $infos = $this->fetch->compare( '861f87e8851b8debb78db548269d29f8da4d94ac' ); |
|
39 | - $this->assertCount( 1, $infos ); |
|
40 | - $this->assertInstanceOf( 'Writing_On_GitHub_File_Info', $infos[0] ); |
|
36 | + $this->set_get_trees(true, 'master'); |
|
37 | + $this->set_get_compare(true); |
|
38 | + $infos = $this->fetch->compare('861f87e8851b8debb78db548269d29f8da4d94ac'); |
|
39 | + $this->assertCount(1, $infos); |
|
40 | + $this->assertInstanceOf('Writing_On_GitHub_File_Info', $infos[0]); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | public function test_should_return_files_with_tree() { |
44 | 44 | // $this->set_get_refs_heads_master( true ); |
45 | 45 | // $this->set_get_commits( true ); |
46 | - $this->set_get_trees( true, 'master' ); |
|
46 | + $this->set_get_trees(true, 'master'); |
|
47 | 47 | |
48 | - $this->assertCount( 3, $this->fetch->tree_recursive() ); |
|
48 | + $this->assertCount(3, $this->fetch->tree_recursive()); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | public function test_should_fail_if_cant_fetch_tree() { |
52 | - $this->set_get_trees( false, 'master' ); |
|
52 | + $this->set_get_trees(false, 'master'); |
|
53 | 53 | |
54 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
55 | - $this->assertSame( '422_unprocessable_entity', $error->get_error_code() ); |
|
54 | + $this->assertInstanceOf('WP_Error', $error = $this->fetch->tree_recursive()); |
|
55 | + $this->assertSame('422_unprocessable_entity', $error->get_error_code()); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | public function test_should_return_commit_with_no_blobs_if_api_fails() { |
59 | - $this->set_get_trees( true, 'master' ); |
|
60 | - $this->set_get_blobs( false ); |
|
59 | + $this->set_get_trees(true, 'master'); |
|
60 | + $this->set_get_blobs(false); |
|
61 | 61 | |
62 | - $this->assertCount( 3, $files = $this->fetch->tree_recursive() ); |
|
62 | + $this->assertCount(3, $files = $this->fetch->tree_recursive()); |
|
63 | 63 | |
64 | - foreach ( $files as $file ) { |
|
65 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->blob( $file ) ); |
|
66 | - $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
64 | + foreach ($files as $file) { |
|
65 | + $this->assertInstanceOf('WP_Error', $error = $this->fetch->blob($file)); |
|
66 | + $this->assertSame('404_not_found', $error->get_error_code()); |
|
67 | 67 | } |
68 | 68 | } |
69 | 69 | |
@@ -143,10 +143,10 @@ discard block |
||
143 | 143 | // } |
144 | 144 | // } |
145 | 145 | |
146 | - protected function malformed_repo( $repo ) { |
|
147 | - update_option( Base::REPO_OPTION_KEY, $repo ); |
|
146 | + protected function malformed_repo($repo) { |
|
147 | + update_option(Base::REPO_OPTION_KEY, $repo); |
|
148 | 148 | |
149 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
150 | - $this->assertSame( 'malformed_repository', $error->get_error_code() ); |
|
149 | + $this->assertInstanceOf('WP_Error', $error = $this->fetch->tree_recursive()); |
|
150 | + $this->assertSame('malformed_repository', $error->get_error_code()); |
|
151 | 151 | } |
152 | 152 | } |
@@ -2,275 +2,275 @@ |
||
2 | 2 | |
3 | 3 | abstract class Writing_On_GitHub_Base_Client_Test extends Writing_On_GitHub_TestCase { |
4 | 4 | |
5 | - /** |
|
6 | - * @var string |
|
7 | - */ |
|
8 | - const HOST_OPTION_VALUE = 'https://api.github.com'; |
|
9 | - |
|
10 | - /** |
|
11 | - * @var string |
|
12 | - */ |
|
13 | - const REPO_OPTION_VALUE = 'woghtest/wogh-test'; |
|
14 | - |
|
15 | - /** |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - const TOKEN_OPTION_VALUE = 'the-token'; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - const BRANCH_OPTION_VALUE = 'master'; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - protected static $responses = array(); |
|
29 | - |
|
30 | - /** |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - protected static $validations = array(); |
|
34 | - |
|
35 | - public function setUp() { |
|
36 | - parent::setUp(); |
|
37 | - |
|
38 | - WP_HTTP_TestCase::init(); |
|
39 | - update_option( 'wogh_repository', self::REPO_OPTION_VALUE ); |
|
40 | - update_option( 'wogh_oauth_token', self::TOKEN_OPTION_VALUE ); |
|
41 | - update_option( 'wogh_host', self::HOST_OPTION_VALUE ); |
|
42 | - update_option( 'wogh_branch', self::BRANCH_OPTION_VALUE ); |
|
43 | - $this->http_responder = array( $this, 'mock_github_api' ); |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * This does some checks and fails the test if something is wrong |
|
48 | - * or returns intended mock data for the given endpoint + method. |
|
49 | - * |
|
50 | - * @return void|string |
|
51 | - */ |
|
52 | - public function mock_github_api( $request, $url ) { |
|
53 | - $host_length = strlen( self::HOST_OPTION_VALUE ); |
|
54 | - |
|
55 | - if ( self::HOST_OPTION_VALUE !== substr( $url, 0, $host_length ) ) { |
|
56 | - $this->assertTrue( false, 'Called wrong host.' ); |
|
57 | - } |
|
58 | - |
|
59 | - if ( |
|
60 | - ! isset( $request['headers']['Authorization'] ) || |
|
61 | - 'token ' . self::TOKEN_OPTION_VALUE !== $request['headers']['Authorization'] |
|
62 | - ) { |
|
63 | - $this->assertTrue( false, 'Missing authorization key.' ); |
|
64 | - } |
|
65 | - |
|
66 | - $url = explode( '/', substr( $url, $host_length + 1 ) ); |
|
67 | - |
|
68 | - if ( 'repos' !== $url[0] ) { |
|
69 | - $this->assertTrue( false, 'Called wrong endpoint.' ); |
|
70 | - } |
|
71 | - |
|
72 | - $repo = $url[1] . '/' . $url[2]; |
|
73 | - |
|
74 | - if ( self::REPO_OPTION_VALUE !== $repo ) { |
|
75 | - $this->assertTrue( false, 'Called wrong repo.' ); |
|
76 | - } |
|
77 | - |
|
78 | - // git api |
|
79 | - $parts = array_slice( $url, $url[3] === 'git' ? 4 : 3 ); |
|
80 | - array_unshift( $parts, strtolower( $request['method'] ) ); |
|
81 | - $endpoint = implode( '_', $parts ); |
|
82 | - $endpoint = str_replace( '?recursive=1', '', $endpoint ); |
|
83 | - $this->assertTrue( call_user_func( static::$validations[ $endpoint ], $request ), 'Request did not validate.' ); |
|
84 | - |
|
85 | - return static::$responses[ $endpoint ]; |
|
86 | - } |
|
87 | - |
|
88 | - protected function set_get_refs_heads_master( $succeed ) { |
|
89 | - $this->set_endpoint( |
|
90 | - function ( $request ) { |
|
91 | - if ( '[]' === $request['body'] ) { |
|
92 | - return false; |
|
93 | - } |
|
94 | - |
|
95 | - return true; |
|
96 | - }, $succeed ? '200 OK' : '404 Not Found', $succeed |
|
97 | - ); |
|
98 | - } |
|
99 | - |
|
100 | - protected function set_get_commits( $succeed ) { |
|
101 | - $this->set_endpoint( |
|
102 | - function ( $request ) { |
|
103 | - if ( '[]' === $request['body'] ) { |
|
104 | - return false; |
|
105 | - } |
|
106 | - |
|
107 | - return true; |
|
108 | - }, $succeed ? '200 OK' : '404 Not Found', $succeed, 'db2510854e6aeab68ead26b48328b19f4bdf926e' |
|
109 | - ); |
|
110 | - } |
|
111 | - |
|
112 | - protected function set_get_trees( $succeed, $sha = '' ) { |
|
113 | - $this->set_endpoint( |
|
114 | - function ( $request ) { |
|
115 | - if ( '[]' === $request['body'] ) { |
|
116 | - return false; |
|
117 | - } |
|
118 | - |
|
119 | - return true; |
|
120 | - }, $succeed ? '200 OK' : '422 Unprocessable Entity', $succeed, |
|
121 | - $sha ? $sha : '9108868e3800bec6763e51beb0d33e15036c3626' |
|
122 | - ); |
|
123 | - } |
|
124 | - |
|
125 | - protected function set_get_blobs( $succeed ) { |
|
126 | - $shas = array( |
|
127 | - '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
|
128 | - '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
|
129 | - '2d73165945b0ccbe4932f1363457986b0ed49f19', |
|
130 | - ); |
|
131 | - |
|
132 | - foreach ( $shas as $sha ) { |
|
133 | - $this->set_endpoint( |
|
134 | - function ( $request ) { |
|
135 | - if ( '[]' === $request['body'] ) { |
|
136 | - return false; |
|
137 | - } |
|
138 | - |
|
139 | - return true; |
|
140 | - }, $succeed ? '200 OK' : '404 Not Found', $succeed, $sha |
|
141 | - ); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - protected function set_post_trees( $succeed ) { |
|
146 | - $this->set_endpoint( |
|
147 | - function ( $request ) { |
|
148 | - $body = json_decode( $request['body'], true ); |
|
149 | - |
|
150 | - if ( ! isset( $body['tree'] ) ) { |
|
151 | - return false; |
|
152 | - } |
|
153 | - |
|
154 | - if ( 1 !== count( $body['tree'] ) ) { |
|
155 | - return false; |
|
156 | - } |
|
157 | - |
|
158 | - $blob = reset( $body['tree'] ); |
|
159 | - |
|
160 | - if ( |
|
161 | - ! isset( $blob['path'] ) || |
|
162 | - ! isset( $blob['type'] ) || |
|
163 | - ! isset( $blob['content'] ) || |
|
164 | - ! isset( $blob['mode'] ) |
|
165 | - ) { |
|
166 | - return false; |
|
167 | - } |
|
168 | - |
|
169 | - return true; |
|
170 | - }, |
|
171 | - $succeed ? '201 Created' : '404 Not Found', |
|
172 | - $succeed |
|
173 | - ); |
|
174 | - } |
|
175 | - |
|
176 | - protected function set_post_commits( $succeed, $anonymous = true ) { |
|
177 | - $this->set_endpoint( |
|
178 | - function ( $request ) use ( $anonymous ) { |
|
179 | - $body = json_decode( $request['body'], true ); |
|
180 | - |
|
181 | - if ( |
|
182 | - ! isset( $body['tree'] ) || |
|
183 | - ! isset( $body['message'] ) || |
|
184 | - ! isset( $body['parents'] ) || |
|
185 | - ! isset( $body['author'] ) |
|
186 | - ) { |
|
187 | - return false; |
|
188 | - } |
|
189 | - |
|
190 | - if ( 1 !== count( $body['parents'] ) ) { |
|
191 | - return false; |
|
192 | - } |
|
193 | - |
|
194 | - if ( ! $anonymous ) { |
|
195 | - if ( |
|
196 | - 'James DiGioia' !== $body['author']['name'] || |
|
197 | - '[email protected]' !== $body['author']['email'] |
|
198 | - ) { |
|
199 | - return false; |
|
200 | - } |
|
201 | - } else { |
|
202 | - if ( |
|
203 | - 'Anonymous' !== $body['author']['name'] || |
|
204 | - '[email protected]' !== $body['author']['email'] |
|
205 | - ) { |
|
206 | - return false; |
|
207 | - } |
|
208 | - } |
|
209 | - |
|
210 | - return true; |
|
211 | - }, |
|
212 | - $succeed ? '201 Created' : '404 Not Found', |
|
213 | - $succeed |
|
214 | - ); |
|
215 | - } |
|
216 | - |
|
217 | - protected function set_get_compare( $succeed ) { |
|
218 | - $this->set_endpoint( |
|
219 | - function ( $request ) { |
|
220 | - if ( '[]' === $request['body'] ) { |
|
221 | - return false; |
|
222 | - } |
|
223 | - return true; |
|
224 | - }, |
|
225 | - $succeed ? '200 OK' : '404 Not Found', |
|
226 | - $succeed, |
|
227 | - '861f87e8851b8debb78db548269d29f8da4d94ac...master' |
|
228 | - ); |
|
229 | - } |
|
230 | - |
|
231 | - protected function set_delete_contents( $succeed, $filepath ) { |
|
232 | - $this->set_endpoint( |
|
233 | - function ( $request ) { |
|
234 | - if ( '[]' === $request['body'] ) { |
|
235 | - return false; |
|
236 | - } |
|
237 | - return true; |
|
238 | - }, |
|
239 | - $succeed ? '200 OK' : '404 Not Found', |
|
240 | - $succeed, |
|
241 | - $filepath |
|
242 | - ); |
|
243 | - } |
|
244 | - |
|
245 | - protected function set_patch_refs_heads_master( $succeed ) { |
|
246 | - $this->set_endpoint( |
|
247 | - function ( $request ) { |
|
248 | - $body = json_decode( $request['body'], true ); |
|
249 | - |
|
250 | - if ( ! isset( $body['sha'] ) ) { |
|
251 | - return false; |
|
252 | - } |
|
253 | - |
|
254 | - return true; |
|
255 | - }, |
|
256 | - $succeed ? '201 Created' : '404 Not Found', |
|
257 | - $succeed |
|
258 | - ); |
|
259 | - } |
|
260 | - |
|
261 | - private function set_endpoint( $validation, $status, $succeed, $sha = '' ) { |
|
262 | - list( , $caller ) = debug_backtrace( false ); |
|
263 | - $endpoint = substr( $caller['function'], 4 ) . ( $sha ? "_$sha" : '' ); |
|
264 | - |
|
265 | - static::$validations[ $endpoint ] = $validation; |
|
266 | - |
|
267 | - static::$responses[ $endpoint ] = array( |
|
268 | - 'headers' => array( |
|
269 | - 'status' => $status, |
|
270 | - ), |
|
271 | - 'body' => file_get_contents( |
|
272 | - $this->data_dir . $endpoint . '_' . ( $succeed ? 'succeed' : 'fail' ) . '.json' |
|
273 | - ), |
|
274 | - ); |
|
275 | - } |
|
5 | + /** |
|
6 | + * @var string |
|
7 | + */ |
|
8 | + const HOST_OPTION_VALUE = 'https://api.github.com'; |
|
9 | + |
|
10 | + /** |
|
11 | + * @var string |
|
12 | + */ |
|
13 | + const REPO_OPTION_VALUE = 'woghtest/wogh-test'; |
|
14 | + |
|
15 | + /** |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + const TOKEN_OPTION_VALUE = 'the-token'; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + const BRANCH_OPTION_VALUE = 'master'; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + protected static $responses = array(); |
|
29 | + |
|
30 | + /** |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + protected static $validations = array(); |
|
34 | + |
|
35 | + public function setUp() { |
|
36 | + parent::setUp(); |
|
37 | + |
|
38 | + WP_HTTP_TestCase::init(); |
|
39 | + update_option( 'wogh_repository', self::REPO_OPTION_VALUE ); |
|
40 | + update_option( 'wogh_oauth_token', self::TOKEN_OPTION_VALUE ); |
|
41 | + update_option( 'wogh_host', self::HOST_OPTION_VALUE ); |
|
42 | + update_option( 'wogh_branch', self::BRANCH_OPTION_VALUE ); |
|
43 | + $this->http_responder = array( $this, 'mock_github_api' ); |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * This does some checks and fails the test if something is wrong |
|
48 | + * or returns intended mock data for the given endpoint + method. |
|
49 | + * |
|
50 | + * @return void|string |
|
51 | + */ |
|
52 | + public function mock_github_api( $request, $url ) { |
|
53 | + $host_length = strlen( self::HOST_OPTION_VALUE ); |
|
54 | + |
|
55 | + if ( self::HOST_OPTION_VALUE !== substr( $url, 0, $host_length ) ) { |
|
56 | + $this->assertTrue( false, 'Called wrong host.' ); |
|
57 | + } |
|
58 | + |
|
59 | + if ( |
|
60 | + ! isset( $request['headers']['Authorization'] ) || |
|
61 | + 'token ' . self::TOKEN_OPTION_VALUE !== $request['headers']['Authorization'] |
|
62 | + ) { |
|
63 | + $this->assertTrue( false, 'Missing authorization key.' ); |
|
64 | + } |
|
65 | + |
|
66 | + $url = explode( '/', substr( $url, $host_length + 1 ) ); |
|
67 | + |
|
68 | + if ( 'repos' !== $url[0] ) { |
|
69 | + $this->assertTrue( false, 'Called wrong endpoint.' ); |
|
70 | + } |
|
71 | + |
|
72 | + $repo = $url[1] . '/' . $url[2]; |
|
73 | + |
|
74 | + if ( self::REPO_OPTION_VALUE !== $repo ) { |
|
75 | + $this->assertTrue( false, 'Called wrong repo.' ); |
|
76 | + } |
|
77 | + |
|
78 | + // git api |
|
79 | + $parts = array_slice( $url, $url[3] === 'git' ? 4 : 3 ); |
|
80 | + array_unshift( $parts, strtolower( $request['method'] ) ); |
|
81 | + $endpoint = implode( '_', $parts ); |
|
82 | + $endpoint = str_replace( '?recursive=1', '', $endpoint ); |
|
83 | + $this->assertTrue( call_user_func( static::$validations[ $endpoint ], $request ), 'Request did not validate.' ); |
|
84 | + |
|
85 | + return static::$responses[ $endpoint ]; |
|
86 | + } |
|
87 | + |
|
88 | + protected function set_get_refs_heads_master( $succeed ) { |
|
89 | + $this->set_endpoint( |
|
90 | + function ( $request ) { |
|
91 | + if ( '[]' === $request['body'] ) { |
|
92 | + return false; |
|
93 | + } |
|
94 | + |
|
95 | + return true; |
|
96 | + }, $succeed ? '200 OK' : '404 Not Found', $succeed |
|
97 | + ); |
|
98 | + } |
|
99 | + |
|
100 | + protected function set_get_commits( $succeed ) { |
|
101 | + $this->set_endpoint( |
|
102 | + function ( $request ) { |
|
103 | + if ( '[]' === $request['body'] ) { |
|
104 | + return false; |
|
105 | + } |
|
106 | + |
|
107 | + return true; |
|
108 | + }, $succeed ? '200 OK' : '404 Not Found', $succeed, 'db2510854e6aeab68ead26b48328b19f4bdf926e' |
|
109 | + ); |
|
110 | + } |
|
111 | + |
|
112 | + protected function set_get_trees( $succeed, $sha = '' ) { |
|
113 | + $this->set_endpoint( |
|
114 | + function ( $request ) { |
|
115 | + if ( '[]' === $request['body'] ) { |
|
116 | + return false; |
|
117 | + } |
|
118 | + |
|
119 | + return true; |
|
120 | + }, $succeed ? '200 OK' : '422 Unprocessable Entity', $succeed, |
|
121 | + $sha ? $sha : '9108868e3800bec6763e51beb0d33e15036c3626' |
|
122 | + ); |
|
123 | + } |
|
124 | + |
|
125 | + protected function set_get_blobs( $succeed ) { |
|
126 | + $shas = array( |
|
127 | + '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
|
128 | + '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
|
129 | + '2d73165945b0ccbe4932f1363457986b0ed49f19', |
|
130 | + ); |
|
131 | + |
|
132 | + foreach ( $shas as $sha ) { |
|
133 | + $this->set_endpoint( |
|
134 | + function ( $request ) { |
|
135 | + if ( '[]' === $request['body'] ) { |
|
136 | + return false; |
|
137 | + } |
|
138 | + |
|
139 | + return true; |
|
140 | + }, $succeed ? '200 OK' : '404 Not Found', $succeed, $sha |
|
141 | + ); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + protected function set_post_trees( $succeed ) { |
|
146 | + $this->set_endpoint( |
|
147 | + function ( $request ) { |
|
148 | + $body = json_decode( $request['body'], true ); |
|
149 | + |
|
150 | + if ( ! isset( $body['tree'] ) ) { |
|
151 | + return false; |
|
152 | + } |
|
153 | + |
|
154 | + if ( 1 !== count( $body['tree'] ) ) { |
|
155 | + return false; |
|
156 | + } |
|
157 | + |
|
158 | + $blob = reset( $body['tree'] ); |
|
159 | + |
|
160 | + if ( |
|
161 | + ! isset( $blob['path'] ) || |
|
162 | + ! isset( $blob['type'] ) || |
|
163 | + ! isset( $blob['content'] ) || |
|
164 | + ! isset( $blob['mode'] ) |
|
165 | + ) { |
|
166 | + return false; |
|
167 | + } |
|
168 | + |
|
169 | + return true; |
|
170 | + }, |
|
171 | + $succeed ? '201 Created' : '404 Not Found', |
|
172 | + $succeed |
|
173 | + ); |
|
174 | + } |
|
175 | + |
|
176 | + protected function set_post_commits( $succeed, $anonymous = true ) { |
|
177 | + $this->set_endpoint( |
|
178 | + function ( $request ) use ( $anonymous ) { |
|
179 | + $body = json_decode( $request['body'], true ); |
|
180 | + |
|
181 | + if ( |
|
182 | + ! isset( $body['tree'] ) || |
|
183 | + ! isset( $body['message'] ) || |
|
184 | + ! isset( $body['parents'] ) || |
|
185 | + ! isset( $body['author'] ) |
|
186 | + ) { |
|
187 | + return false; |
|
188 | + } |
|
189 | + |
|
190 | + if ( 1 !== count( $body['parents'] ) ) { |
|
191 | + return false; |
|
192 | + } |
|
193 | + |
|
194 | + if ( ! $anonymous ) { |
|
195 | + if ( |
|
196 | + 'James DiGioia' !== $body['author']['name'] || |
|
197 | + '[email protected]' !== $body['author']['email'] |
|
198 | + ) { |
|
199 | + return false; |
|
200 | + } |
|
201 | + } else { |
|
202 | + if ( |
|
203 | + 'Anonymous' !== $body['author']['name'] || |
|
204 | + '[email protected]' !== $body['author']['email'] |
|
205 | + ) { |
|
206 | + return false; |
|
207 | + } |
|
208 | + } |
|
209 | + |
|
210 | + return true; |
|
211 | + }, |
|
212 | + $succeed ? '201 Created' : '404 Not Found', |
|
213 | + $succeed |
|
214 | + ); |
|
215 | + } |
|
216 | + |
|
217 | + protected function set_get_compare( $succeed ) { |
|
218 | + $this->set_endpoint( |
|
219 | + function ( $request ) { |
|
220 | + if ( '[]' === $request['body'] ) { |
|
221 | + return false; |
|
222 | + } |
|
223 | + return true; |
|
224 | + }, |
|
225 | + $succeed ? '200 OK' : '404 Not Found', |
|
226 | + $succeed, |
|
227 | + '861f87e8851b8debb78db548269d29f8da4d94ac...master' |
|
228 | + ); |
|
229 | + } |
|
230 | + |
|
231 | + protected function set_delete_contents( $succeed, $filepath ) { |
|
232 | + $this->set_endpoint( |
|
233 | + function ( $request ) { |
|
234 | + if ( '[]' === $request['body'] ) { |
|
235 | + return false; |
|
236 | + } |
|
237 | + return true; |
|
238 | + }, |
|
239 | + $succeed ? '200 OK' : '404 Not Found', |
|
240 | + $succeed, |
|
241 | + $filepath |
|
242 | + ); |
|
243 | + } |
|
244 | + |
|
245 | + protected function set_patch_refs_heads_master( $succeed ) { |
|
246 | + $this->set_endpoint( |
|
247 | + function ( $request ) { |
|
248 | + $body = json_decode( $request['body'], true ); |
|
249 | + |
|
250 | + if ( ! isset( $body['sha'] ) ) { |
|
251 | + return false; |
|
252 | + } |
|
253 | + |
|
254 | + return true; |
|
255 | + }, |
|
256 | + $succeed ? '201 Created' : '404 Not Found', |
|
257 | + $succeed |
|
258 | + ); |
|
259 | + } |
|
260 | + |
|
261 | + private function set_endpoint( $validation, $status, $succeed, $sha = '' ) { |
|
262 | + list( , $caller ) = debug_backtrace( false ); |
|
263 | + $endpoint = substr( $caller['function'], 4 ) . ( $sha ? "_$sha" : '' ); |
|
264 | + |
|
265 | + static::$validations[ $endpoint ] = $validation; |
|
266 | + |
|
267 | + static::$responses[ $endpoint ] = array( |
|
268 | + 'headers' => array( |
|
269 | + 'status' => $status, |
|
270 | + ), |
|
271 | + 'body' => file_get_contents( |
|
272 | + $this->data_dir . $endpoint . '_' . ( $succeed ? 'succeed' : 'fail' ) . '.json' |
|
273 | + ), |
|
274 | + ); |
|
275 | + } |
|
276 | 276 | } |
@@ -36,11 +36,11 @@ discard block |
||
36 | 36 | parent::setUp(); |
37 | 37 | |
38 | 38 | WP_HTTP_TestCase::init(); |
39 | - update_option( 'wogh_repository', self::REPO_OPTION_VALUE ); |
|
40 | - update_option( 'wogh_oauth_token', self::TOKEN_OPTION_VALUE ); |
|
41 | - update_option( 'wogh_host', self::HOST_OPTION_VALUE ); |
|
42 | - update_option( 'wogh_branch', self::BRANCH_OPTION_VALUE ); |
|
43 | - $this->http_responder = array( $this, 'mock_github_api' ); |
|
39 | + update_option('wogh_repository', self::REPO_OPTION_VALUE); |
|
40 | + update_option('wogh_oauth_token', self::TOKEN_OPTION_VALUE); |
|
41 | + update_option('wogh_host', self::HOST_OPTION_VALUE); |
|
42 | + update_option('wogh_branch', self::BRANCH_OPTION_VALUE); |
|
43 | + $this->http_responder = array($this, 'mock_github_api'); |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | /** |
@@ -49,46 +49,46 @@ discard block |
||
49 | 49 | * |
50 | 50 | * @return void|string |
51 | 51 | */ |
52 | - public function mock_github_api( $request, $url ) { |
|
53 | - $host_length = strlen( self::HOST_OPTION_VALUE ); |
|
52 | + public function mock_github_api($request, $url) { |
|
53 | + $host_length = strlen(self::HOST_OPTION_VALUE); |
|
54 | 54 | |
55 | - if ( self::HOST_OPTION_VALUE !== substr( $url, 0, $host_length ) ) { |
|
56 | - $this->assertTrue( false, 'Called wrong host.' ); |
|
55 | + if (self::HOST_OPTION_VALUE !== substr($url, 0, $host_length)) { |
|
56 | + $this->assertTrue(false, 'Called wrong host.'); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | if ( |
60 | - ! isset( $request['headers']['Authorization'] ) || |
|
60 | + ! isset($request['headers']['Authorization']) || |
|
61 | 61 | 'token ' . self::TOKEN_OPTION_VALUE !== $request['headers']['Authorization'] |
62 | 62 | ) { |
63 | - $this->assertTrue( false, 'Missing authorization key.' ); |
|
63 | + $this->assertTrue(false, 'Missing authorization key.'); |
|
64 | 64 | } |
65 | 65 | |
66 | - $url = explode( '/', substr( $url, $host_length + 1 ) ); |
|
66 | + $url = explode('/', substr($url, $host_length + 1)); |
|
67 | 67 | |
68 | - if ( 'repos' !== $url[0] ) { |
|
69 | - $this->assertTrue( false, 'Called wrong endpoint.' ); |
|
68 | + if ('repos' !== $url[0]) { |
|
69 | + $this->assertTrue(false, 'Called wrong endpoint.'); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | $repo = $url[1] . '/' . $url[2]; |
73 | 73 | |
74 | - if ( self::REPO_OPTION_VALUE !== $repo ) { |
|
75 | - $this->assertTrue( false, 'Called wrong repo.' ); |
|
74 | + if (self::REPO_OPTION_VALUE !== $repo) { |
|
75 | + $this->assertTrue(false, 'Called wrong repo.'); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | // git api |
79 | - $parts = array_slice( $url, $url[3] === 'git' ? 4 : 3 ); |
|
80 | - array_unshift( $parts, strtolower( $request['method'] ) ); |
|
81 | - $endpoint = implode( '_', $parts ); |
|
82 | - $endpoint = str_replace( '?recursive=1', '', $endpoint ); |
|
83 | - $this->assertTrue( call_user_func( static::$validations[ $endpoint ], $request ), 'Request did not validate.' ); |
|
79 | + $parts = array_slice($url, $url[3] === 'git' ? 4 : 3); |
|
80 | + array_unshift($parts, strtolower($request['method'])); |
|
81 | + $endpoint = implode('_', $parts); |
|
82 | + $endpoint = str_replace('?recursive=1', '', $endpoint); |
|
83 | + $this->assertTrue(call_user_func(static::$validations[$endpoint], $request), 'Request did not validate.'); |
|
84 | 84 | |
85 | - return static::$responses[ $endpoint ]; |
|
85 | + return static::$responses[$endpoint]; |
|
86 | 86 | } |
87 | 87 | |
88 | - protected function set_get_refs_heads_master( $succeed ) { |
|
88 | + protected function set_get_refs_heads_master($succeed) { |
|
89 | 89 | $this->set_endpoint( |
90 | - function ( $request ) { |
|
91 | - if ( '[]' === $request['body'] ) { |
|
90 | + function($request) { |
|
91 | + if ('[]' === $request['body']) { |
|
92 | 92 | return false; |
93 | 93 | } |
94 | 94 | |
@@ -97,10 +97,10 @@ discard block |
||
97 | 97 | ); |
98 | 98 | } |
99 | 99 | |
100 | - protected function set_get_commits( $succeed ) { |
|
100 | + protected function set_get_commits($succeed) { |
|
101 | 101 | $this->set_endpoint( |
102 | - function ( $request ) { |
|
103 | - if ( '[]' === $request['body'] ) { |
|
102 | + function($request) { |
|
103 | + if ('[]' === $request['body']) { |
|
104 | 104 | return false; |
105 | 105 | } |
106 | 106 | |
@@ -109,10 +109,10 @@ discard block |
||
109 | 109 | ); |
110 | 110 | } |
111 | 111 | |
112 | - protected function set_get_trees( $succeed, $sha = '' ) { |
|
112 | + protected function set_get_trees($succeed, $sha = '') { |
|
113 | 113 | $this->set_endpoint( |
114 | - function ( $request ) { |
|
115 | - if ( '[]' === $request['body'] ) { |
|
114 | + function($request) { |
|
115 | + if ('[]' === $request['body']) { |
|
116 | 116 | return false; |
117 | 117 | } |
118 | 118 | |
@@ -122,17 +122,17 @@ discard block |
||
122 | 122 | ); |
123 | 123 | } |
124 | 124 | |
125 | - protected function set_get_blobs( $succeed ) { |
|
125 | + protected function set_get_blobs($succeed) { |
|
126 | 126 | $shas = array( |
127 | 127 | '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
128 | 128 | '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
129 | 129 | '2d73165945b0ccbe4932f1363457986b0ed49f19', |
130 | 130 | ); |
131 | 131 | |
132 | - foreach ( $shas as $sha ) { |
|
132 | + foreach ($shas as $sha) { |
|
133 | 133 | $this->set_endpoint( |
134 | - function ( $request ) { |
|
135 | - if ( '[]' === $request['body'] ) { |
|
134 | + function($request) { |
|
135 | + if ('[]' === $request['body']) { |
|
136 | 136 | return false; |
137 | 137 | } |
138 | 138 | |
@@ -142,26 +142,26 @@ discard block |
||
142 | 142 | } |
143 | 143 | } |
144 | 144 | |
145 | - protected function set_post_trees( $succeed ) { |
|
145 | + protected function set_post_trees($succeed) { |
|
146 | 146 | $this->set_endpoint( |
147 | - function ( $request ) { |
|
148 | - $body = json_decode( $request['body'], true ); |
|
147 | + function($request) { |
|
148 | + $body = json_decode($request['body'], true); |
|
149 | 149 | |
150 | - if ( ! isset( $body['tree'] ) ) { |
|
150 | + if ( ! isset($body['tree'])) { |
|
151 | 151 | return false; |
152 | 152 | } |
153 | 153 | |
154 | - if ( 1 !== count( $body['tree'] ) ) { |
|
154 | + if (1 !== count($body['tree'])) { |
|
155 | 155 | return false; |
156 | 156 | } |
157 | 157 | |
158 | - $blob = reset( $body['tree'] ); |
|
158 | + $blob = reset($body['tree']); |
|
159 | 159 | |
160 | 160 | if ( |
161 | - ! isset( $blob['path'] ) || |
|
162 | - ! isset( $blob['type'] ) || |
|
163 | - ! isset( $blob['content'] ) || |
|
164 | - ! isset( $blob['mode'] ) |
|
161 | + ! isset($blob['path']) || |
|
162 | + ! isset($blob['type']) || |
|
163 | + ! isset($blob['content']) || |
|
164 | + ! isset($blob['mode']) |
|
165 | 165 | ) { |
166 | 166 | return false; |
167 | 167 | } |
@@ -173,25 +173,25 @@ discard block |
||
173 | 173 | ); |
174 | 174 | } |
175 | 175 | |
176 | - protected function set_post_commits( $succeed, $anonymous = true ) { |
|
176 | + protected function set_post_commits($succeed, $anonymous = true) { |
|
177 | 177 | $this->set_endpoint( |
178 | - function ( $request ) use ( $anonymous ) { |
|
179 | - $body = json_decode( $request['body'], true ); |
|
178 | + function($request) use ($anonymous) { |
|
179 | + $body = json_decode($request['body'], true); |
|
180 | 180 | |
181 | 181 | if ( |
182 | - ! isset( $body['tree'] ) || |
|
183 | - ! isset( $body['message'] ) || |
|
184 | - ! isset( $body['parents'] ) || |
|
185 | - ! isset( $body['author'] ) |
|
182 | + ! isset($body['tree']) || |
|
183 | + ! isset($body['message']) || |
|
184 | + ! isset($body['parents']) || |
|
185 | + ! isset($body['author']) |
|
186 | 186 | ) { |
187 | 187 | return false; |
188 | 188 | } |
189 | 189 | |
190 | - if ( 1 !== count( $body['parents'] ) ) { |
|
190 | + if (1 !== count($body['parents'])) { |
|
191 | 191 | return false; |
192 | 192 | } |
193 | 193 | |
194 | - if ( ! $anonymous ) { |
|
194 | + if ( ! $anonymous) { |
|
195 | 195 | if ( |
196 | 196 | 'James DiGioia' !== $body['author']['name'] || |
197 | 197 | '[email protected]' !== $body['author']['email'] |
@@ -214,10 +214,10 @@ discard block |
||
214 | 214 | ); |
215 | 215 | } |
216 | 216 | |
217 | - protected function set_get_compare( $succeed ) { |
|
217 | + protected function set_get_compare($succeed) { |
|
218 | 218 | $this->set_endpoint( |
219 | - function ( $request ) { |
|
220 | - if ( '[]' === $request['body'] ) { |
|
219 | + function($request) { |
|
220 | + if ('[]' === $request['body']) { |
|
221 | 221 | return false; |
222 | 222 | } |
223 | 223 | return true; |
@@ -228,10 +228,10 @@ discard block |
||
228 | 228 | ); |
229 | 229 | } |
230 | 230 | |
231 | - protected function set_delete_contents( $succeed, $filepath ) { |
|
231 | + protected function set_delete_contents($succeed, $filepath) { |
|
232 | 232 | $this->set_endpoint( |
233 | - function ( $request ) { |
|
234 | - if ( '[]' === $request['body'] ) { |
|
233 | + function($request) { |
|
234 | + if ('[]' === $request['body']) { |
|
235 | 235 | return false; |
236 | 236 | } |
237 | 237 | return true; |
@@ -242,12 +242,12 @@ discard block |
||
242 | 242 | ); |
243 | 243 | } |
244 | 244 | |
245 | - protected function set_patch_refs_heads_master( $succeed ) { |
|
245 | + protected function set_patch_refs_heads_master($succeed) { |
|
246 | 246 | $this->set_endpoint( |
247 | - function ( $request ) { |
|
248 | - $body = json_decode( $request['body'], true ); |
|
247 | + function($request) { |
|
248 | + $body = json_decode($request['body'], true); |
|
249 | 249 | |
250 | - if ( ! isset( $body['sha'] ) ) { |
|
250 | + if ( ! isset($body['sha'])) { |
|
251 | 251 | return false; |
252 | 252 | } |
253 | 253 | |
@@ -258,18 +258,18 @@ discard block |
||
258 | 258 | ); |
259 | 259 | } |
260 | 260 | |
261 | - private function set_endpoint( $validation, $status, $succeed, $sha = '' ) { |
|
262 | - list( , $caller ) = debug_backtrace( false ); |
|
263 | - $endpoint = substr( $caller['function'], 4 ) . ( $sha ? "_$sha" : '' ); |
|
261 | + private function set_endpoint($validation, $status, $succeed, $sha = '') { |
|
262 | + list(, $caller) = debug_backtrace(false); |
|
263 | + $endpoint = substr($caller['function'], 4) . ($sha ? "_$sha" : ''); |
|
264 | 264 | |
265 | - static::$validations[ $endpoint ] = $validation; |
|
265 | + static::$validations[$endpoint] = $validation; |
|
266 | 266 | |
267 | - static::$responses[ $endpoint ] = array( |
|
267 | + static::$responses[$endpoint] = array( |
|
268 | 268 | 'headers' => array( |
269 | 269 | 'status' => $status, |
270 | 270 | ), |
271 | 271 | 'body' => file_get_contents( |
272 | - $this->data_dir . $endpoint . '_' . ( $succeed ? 'succeed' : 'fail' ) . '.json' |
|
272 | + $this->data_dir . $endpoint . '_' . ($succeed ? 'succeed' : 'fail') . '.json' |
|
273 | 273 | ), |
274 | 274 | ); |
275 | 275 | } |
@@ -2,146 +2,146 @@ |
||
2 | 2 | |
3 | 3 | abstract class Writing_On_GitHub_TestCase extends WP_HTTP_TestCase { |
4 | 4 | |
5 | - /** |
|
6 | - * @var string |
|
7 | - */ |
|
8 | - protected $data_dir; |
|
9 | - |
|
10 | - /** |
|
11 | - * @var Writing_On_GitHub|Mockery\Mock |
|
12 | - */ |
|
13 | - protected $app; |
|
14 | - |
|
15 | - /** |
|
16 | - * @var Writing_On_GitHub_Controller|Mockery\Mock |
|
17 | - */ |
|
18 | - protected $controller; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var Writing_On_GitHub_Request|Mockery\Mock |
|
22 | - */ |
|
23 | - protected $request; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var Writing_On_GitHub_Import|Mockery\Mock |
|
27 | - */ |
|
28 | - protected $import; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var Writing_On_GitHub_Export|Mockery\Mock |
|
32 | - */ |
|
33 | - protected $export; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var Writing_On_GitHub_Response|Mockery\Mock |
|
37 | - */ |
|
38 | - protected $response; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var Writing_On_GitHub_Payload|Mockery\Mock |
|
42 | - */ |
|
43 | - protected $payload; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var Writing_On_GitHub_Api|Mockery\Mock |
|
47 | - */ |
|
48 | - protected $api; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var Writing_On_GitHub_Semaphore|Mockery\Mock |
|
52 | - */ |
|
53 | - protected $semaphore; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var Writing_On_GitHub_Database|Mockery\Mock |
|
57 | - */ |
|
58 | - protected $database; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var Writing_On_GitHub_Post|Mockery\Mock |
|
62 | - */ |
|
63 | - protected $post; |
|
64 | - |
|
65 | - /** |
|
66 | - * @var Writing_On_GitHub_Blob|Mockery\Mock |
|
67 | - */ |
|
68 | - protected $blob; |
|
69 | - |
|
70 | - /** |
|
71 | - * @var Writing_On_GitHub_Fetch_Client|Mockery\Mock |
|
72 | - */ |
|
73 | - protected $fetch; |
|
74 | - |
|
75 | - /** |
|
76 | - * @var Writing_On_GitHub_Persist_Client|Mockery\Mock |
|
77 | - */ |
|
78 | - protected $persist; |
|
79 | - |
|
80 | - public function setUp() { |
|
81 | - parent::setUp(); |
|
82 | - |
|
83 | - $this->data_dir = dirname( __DIR__ ) . '/data/'; |
|
84 | - |
|
85 | - $this->app = Mockery::mock( 'Writing_On_GitHub' ); |
|
86 | - $this->controller = Mockery::mock( 'Writing_On_GitHub_Controller' ); |
|
87 | - $this->request = Mockery::mock( 'Writing_On_GitHub_Request' ); |
|
88 | - $this->import = Mockery::mock( 'Writing_On_GitHub_Import' ); |
|
89 | - $this->export = Mockery::mock( 'Writing_On_GitHub_Export' ); |
|
90 | - $this->response = Mockery::mock( 'Writing_On_GitHub_Response' ); |
|
91 | - $this->payload = Mockery::mock( 'Writing_On_GitHub_Payload' ); |
|
92 | - $this->api = Mockery::mock( 'Writing_On_GitHub_Api' ); |
|
93 | - $this->semaphore = Mockery::mock( 'Writing_On_GitHub_Semaphore' ); |
|
94 | - $this->database = Mockery::mock( 'Writing_On_GitHub_Database' ); |
|
95 | - $this->post = Mockery::mock( 'Writing_On_GitHub_Post' ); |
|
96 | - $this->blob = Mockery::mock( 'Writing_On_GitHub_Blob' ); |
|
97 | - $this->fetch = Mockery::mock( 'Writing_On_GitHub_Fetch_Client' ); |
|
98 | - $this->persist = Mockery::mock( 'Writing_On_GitHub_Persist_Client' ); |
|
99 | - |
|
100 | - Writing_On_GitHub::$instance = $this->app; |
|
101 | - |
|
102 | - $this->app |
|
103 | - ->shouldReceive( 'request' ) |
|
104 | - ->andReturn( $this->request ) |
|
105 | - ->byDefault(); |
|
106 | - $this->app |
|
107 | - ->shouldReceive( 'import' ) |
|
108 | - ->andReturn( $this->import ) |
|
109 | - ->byDefault(); |
|
110 | - $this->app |
|
111 | - ->shouldReceive( 'export' ) |
|
112 | - ->andReturn( $this->export ) |
|
113 | - ->byDefault(); |
|
114 | - $this->app |
|
115 | - ->shouldReceive( 'response' ) |
|
116 | - ->andReturn( $this->response ) |
|
117 | - ->byDefault(); |
|
118 | - $this->app |
|
119 | - ->shouldReceive( 'api' ) |
|
120 | - ->andReturn( $this->api ) |
|
121 | - ->byDefault(); |
|
122 | - $this->app |
|
123 | - ->shouldReceive( 'semaphore' ) |
|
124 | - ->andReturn( $this->semaphore ) |
|
125 | - ->byDefault(); |
|
126 | - $this->app |
|
127 | - ->shouldReceive( 'database' ) |
|
128 | - ->andReturn( $this->database ) |
|
129 | - ->byDefault(); |
|
130 | - $this->app |
|
131 | - ->shouldReceive( 'blob' ) |
|
132 | - ->andReturn( $this->blob ) |
|
133 | - ->byDefault(); |
|
134 | - $this->api |
|
135 | - ->shouldReceive( 'fetch' ) |
|
136 | - ->andReturn( $this->fetch ) |
|
137 | - ->byDefault(); |
|
138 | - $this->api |
|
139 | - ->shouldReceive( 'persist' ) |
|
140 | - ->andReturn( $this->persist ) |
|
141 | - ->byDefault(); |
|
142 | - } |
|
143 | - |
|
144 | - public function tearDown() { |
|
145 | - Mockery::close(); |
|
146 | - } |
|
5 | + /** |
|
6 | + * @var string |
|
7 | + */ |
|
8 | + protected $data_dir; |
|
9 | + |
|
10 | + /** |
|
11 | + * @var Writing_On_GitHub|Mockery\Mock |
|
12 | + */ |
|
13 | + protected $app; |
|
14 | + |
|
15 | + /** |
|
16 | + * @var Writing_On_GitHub_Controller|Mockery\Mock |
|
17 | + */ |
|
18 | + protected $controller; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var Writing_On_GitHub_Request|Mockery\Mock |
|
22 | + */ |
|
23 | + protected $request; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var Writing_On_GitHub_Import|Mockery\Mock |
|
27 | + */ |
|
28 | + protected $import; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var Writing_On_GitHub_Export|Mockery\Mock |
|
32 | + */ |
|
33 | + protected $export; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var Writing_On_GitHub_Response|Mockery\Mock |
|
37 | + */ |
|
38 | + protected $response; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var Writing_On_GitHub_Payload|Mockery\Mock |
|
42 | + */ |
|
43 | + protected $payload; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var Writing_On_GitHub_Api|Mockery\Mock |
|
47 | + */ |
|
48 | + protected $api; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var Writing_On_GitHub_Semaphore|Mockery\Mock |
|
52 | + */ |
|
53 | + protected $semaphore; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var Writing_On_GitHub_Database|Mockery\Mock |
|
57 | + */ |
|
58 | + protected $database; |
|
59 | + |
|
60 | + /** |
|
61 | + * @var Writing_On_GitHub_Post|Mockery\Mock |
|
62 | + */ |
|
63 | + protected $post; |
|
64 | + |
|
65 | + /** |
|
66 | + * @var Writing_On_GitHub_Blob|Mockery\Mock |
|
67 | + */ |
|
68 | + protected $blob; |
|
69 | + |
|
70 | + /** |
|
71 | + * @var Writing_On_GitHub_Fetch_Client|Mockery\Mock |
|
72 | + */ |
|
73 | + protected $fetch; |
|
74 | + |
|
75 | + /** |
|
76 | + * @var Writing_On_GitHub_Persist_Client|Mockery\Mock |
|
77 | + */ |
|
78 | + protected $persist; |
|
79 | + |
|
80 | + public function setUp() { |
|
81 | + parent::setUp(); |
|
82 | + |
|
83 | + $this->data_dir = dirname( __DIR__ ) . '/data/'; |
|
84 | + |
|
85 | + $this->app = Mockery::mock( 'Writing_On_GitHub' ); |
|
86 | + $this->controller = Mockery::mock( 'Writing_On_GitHub_Controller' ); |
|
87 | + $this->request = Mockery::mock( 'Writing_On_GitHub_Request' ); |
|
88 | + $this->import = Mockery::mock( 'Writing_On_GitHub_Import' ); |
|
89 | + $this->export = Mockery::mock( 'Writing_On_GitHub_Export' ); |
|
90 | + $this->response = Mockery::mock( 'Writing_On_GitHub_Response' ); |
|
91 | + $this->payload = Mockery::mock( 'Writing_On_GitHub_Payload' ); |
|
92 | + $this->api = Mockery::mock( 'Writing_On_GitHub_Api' ); |
|
93 | + $this->semaphore = Mockery::mock( 'Writing_On_GitHub_Semaphore' ); |
|
94 | + $this->database = Mockery::mock( 'Writing_On_GitHub_Database' ); |
|
95 | + $this->post = Mockery::mock( 'Writing_On_GitHub_Post' ); |
|
96 | + $this->blob = Mockery::mock( 'Writing_On_GitHub_Blob' ); |
|
97 | + $this->fetch = Mockery::mock( 'Writing_On_GitHub_Fetch_Client' ); |
|
98 | + $this->persist = Mockery::mock( 'Writing_On_GitHub_Persist_Client' ); |
|
99 | + |
|
100 | + Writing_On_GitHub::$instance = $this->app; |
|
101 | + |
|
102 | + $this->app |
|
103 | + ->shouldReceive( 'request' ) |
|
104 | + ->andReturn( $this->request ) |
|
105 | + ->byDefault(); |
|
106 | + $this->app |
|
107 | + ->shouldReceive( 'import' ) |
|
108 | + ->andReturn( $this->import ) |
|
109 | + ->byDefault(); |
|
110 | + $this->app |
|
111 | + ->shouldReceive( 'export' ) |
|
112 | + ->andReturn( $this->export ) |
|
113 | + ->byDefault(); |
|
114 | + $this->app |
|
115 | + ->shouldReceive( 'response' ) |
|
116 | + ->andReturn( $this->response ) |
|
117 | + ->byDefault(); |
|
118 | + $this->app |
|
119 | + ->shouldReceive( 'api' ) |
|
120 | + ->andReturn( $this->api ) |
|
121 | + ->byDefault(); |
|
122 | + $this->app |
|
123 | + ->shouldReceive( 'semaphore' ) |
|
124 | + ->andReturn( $this->semaphore ) |
|
125 | + ->byDefault(); |
|
126 | + $this->app |
|
127 | + ->shouldReceive( 'database' ) |
|
128 | + ->andReturn( $this->database ) |
|
129 | + ->byDefault(); |
|
130 | + $this->app |
|
131 | + ->shouldReceive( 'blob' ) |
|
132 | + ->andReturn( $this->blob ) |
|
133 | + ->byDefault(); |
|
134 | + $this->api |
|
135 | + ->shouldReceive( 'fetch' ) |
|
136 | + ->andReturn( $this->fetch ) |
|
137 | + ->byDefault(); |
|
138 | + $this->api |
|
139 | + ->shouldReceive( 'persist' ) |
|
140 | + ->andReturn( $this->persist ) |
|
141 | + ->byDefault(); |
|
142 | + } |
|
143 | + |
|
144 | + public function tearDown() { |
|
145 | + Mockery::close(); |
|
146 | + } |
|
147 | 147 | } |
@@ -80,64 +80,64 @@ |
||
80 | 80 | public function setUp() { |
81 | 81 | parent::setUp(); |
82 | 82 | |
83 | - $this->data_dir = dirname( __DIR__ ) . '/data/'; |
|
84 | - |
|
85 | - $this->app = Mockery::mock( 'Writing_On_GitHub' ); |
|
86 | - $this->controller = Mockery::mock( 'Writing_On_GitHub_Controller' ); |
|
87 | - $this->request = Mockery::mock( 'Writing_On_GitHub_Request' ); |
|
88 | - $this->import = Mockery::mock( 'Writing_On_GitHub_Import' ); |
|
89 | - $this->export = Mockery::mock( 'Writing_On_GitHub_Export' ); |
|
90 | - $this->response = Mockery::mock( 'Writing_On_GitHub_Response' ); |
|
91 | - $this->payload = Mockery::mock( 'Writing_On_GitHub_Payload' ); |
|
92 | - $this->api = Mockery::mock( 'Writing_On_GitHub_Api' ); |
|
93 | - $this->semaphore = Mockery::mock( 'Writing_On_GitHub_Semaphore' ); |
|
94 | - $this->database = Mockery::mock( 'Writing_On_GitHub_Database' ); |
|
95 | - $this->post = Mockery::mock( 'Writing_On_GitHub_Post' ); |
|
96 | - $this->blob = Mockery::mock( 'Writing_On_GitHub_Blob' ); |
|
97 | - $this->fetch = Mockery::mock( 'Writing_On_GitHub_Fetch_Client' ); |
|
98 | - $this->persist = Mockery::mock( 'Writing_On_GitHub_Persist_Client' ); |
|
83 | + $this->data_dir = dirname(__DIR__) . '/data/'; |
|
84 | + |
|
85 | + $this->app = Mockery::mock('Writing_On_GitHub'); |
|
86 | + $this->controller = Mockery::mock('Writing_On_GitHub_Controller'); |
|
87 | + $this->request = Mockery::mock('Writing_On_GitHub_Request'); |
|
88 | + $this->import = Mockery::mock('Writing_On_GitHub_Import'); |
|
89 | + $this->export = Mockery::mock('Writing_On_GitHub_Export'); |
|
90 | + $this->response = Mockery::mock('Writing_On_GitHub_Response'); |
|
91 | + $this->payload = Mockery::mock('Writing_On_GitHub_Payload'); |
|
92 | + $this->api = Mockery::mock('Writing_On_GitHub_Api'); |
|
93 | + $this->semaphore = Mockery::mock('Writing_On_GitHub_Semaphore'); |
|
94 | + $this->database = Mockery::mock('Writing_On_GitHub_Database'); |
|
95 | + $this->post = Mockery::mock('Writing_On_GitHub_Post'); |
|
96 | + $this->blob = Mockery::mock('Writing_On_GitHub_Blob'); |
|
97 | + $this->fetch = Mockery::mock('Writing_On_GitHub_Fetch_Client'); |
|
98 | + $this->persist = Mockery::mock('Writing_On_GitHub_Persist_Client'); |
|
99 | 99 | |
100 | 100 | Writing_On_GitHub::$instance = $this->app; |
101 | 101 | |
102 | 102 | $this->app |
103 | - ->shouldReceive( 'request' ) |
|
104 | - ->andReturn( $this->request ) |
|
103 | + ->shouldReceive('request') |
|
104 | + ->andReturn($this->request) |
|
105 | 105 | ->byDefault(); |
106 | 106 | $this->app |
107 | - ->shouldReceive( 'import' ) |
|
108 | - ->andReturn( $this->import ) |
|
107 | + ->shouldReceive('import') |
|
108 | + ->andReturn($this->import) |
|
109 | 109 | ->byDefault(); |
110 | 110 | $this->app |
111 | - ->shouldReceive( 'export' ) |
|
112 | - ->andReturn( $this->export ) |
|
111 | + ->shouldReceive('export') |
|
112 | + ->andReturn($this->export) |
|
113 | 113 | ->byDefault(); |
114 | 114 | $this->app |
115 | - ->shouldReceive( 'response' ) |
|
116 | - ->andReturn( $this->response ) |
|
115 | + ->shouldReceive('response') |
|
116 | + ->andReturn($this->response) |
|
117 | 117 | ->byDefault(); |
118 | 118 | $this->app |
119 | - ->shouldReceive( 'api' ) |
|
120 | - ->andReturn( $this->api ) |
|
119 | + ->shouldReceive('api') |
|
120 | + ->andReturn($this->api) |
|
121 | 121 | ->byDefault(); |
122 | 122 | $this->app |
123 | - ->shouldReceive( 'semaphore' ) |
|
124 | - ->andReturn( $this->semaphore ) |
|
123 | + ->shouldReceive('semaphore') |
|
124 | + ->andReturn($this->semaphore) |
|
125 | 125 | ->byDefault(); |
126 | 126 | $this->app |
127 | - ->shouldReceive( 'database' ) |
|
128 | - ->andReturn( $this->database ) |
|
127 | + ->shouldReceive('database') |
|
128 | + ->andReturn($this->database) |
|
129 | 129 | ->byDefault(); |
130 | 130 | $this->app |
131 | - ->shouldReceive( 'blob' ) |
|
132 | - ->andReturn( $this->blob ) |
|
131 | + ->shouldReceive('blob') |
|
132 | + ->andReturn($this->blob) |
|
133 | 133 | ->byDefault(); |
134 | 134 | $this->api |
135 | - ->shouldReceive( 'fetch' ) |
|
136 | - ->andReturn( $this->fetch ) |
|
135 | + ->shouldReceive('fetch') |
|
136 | + ->andReturn($this->fetch) |
|
137 | 137 | ->byDefault(); |
138 | 138 | $this->api |
139 | - ->shouldReceive( 'persist' ) |
|
140 | - ->andReturn( $this->persist ) |
|
139 | + ->shouldReceive('persist') |
|
140 | + ->andReturn($this->persist) |
|
141 | 141 | ->byDefault(); |
142 | 142 | } |
143 | 143 |