PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.3.1
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.3.1
2.7.7 2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 2.6.1 2.7.0 All 28 releases
insert-php / admin / includes / class.import.snippet.php

class.import.snippet.php in Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts 2.3.1, at admin/includes/class.import.snippet.php

218 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Import snippet
5 *
6 * @author Webcraftic <wordpress.webraftic@gmail.com>
7 * @copyright (c) 16.11.2018, Webcraftic
8 * @version 1.0
9 */
10
11 // Exit if accessed directly
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 class WINP_Import_Snippet {
17
18 /**
19 * WINP_Export_Snippet constructor.
20 */
21 public function __construct() {
22 $this->registerHooks();
23 }
24
25 /**
26 * Register hooks
27 */
28 public function registerHooks() {
29 add_action( 'admin_init', [ $this, 'import_snippets_proccess' ] );
30 }
31
32 /**
33 * Process the uploaded import files
34 *
35 * @uses import_snippets() to process the import file
36 * @uses wp_redirect() to pass the import results to the page
37 * @uses add_query_arg() to append the results to the current URI
38 */
39 public function import_snippets_proccess() {
40 if ( isset( $_POST['wbcr_inp_import_form_action'] ) ) {
41
42 if ( ! WINP_Plugin::app()->currentUserCan() ) {
43 wp_die( __( 'Sorry, you are not allowed to import snippets as this user.' ), __( 'You need a higher level of permission.' ), 403 );
44 }
45
46 check_admin_referer( 'wbcr_inp_import_form', 'wbcr_inp_import_form_nonce_field' );
47
48 if ( ! isset( $_FILES['wbcr_inp_import_files'] ) || ! count( $_FILES['wbcr_inp_import_files'] ) || ! isset( $_FILES['wbcr_inp_import_files']['tmp_name'][0] ) || empty( $_FILES['wbcr_inp_import_files']['tmp_name'][0] ) ) {
49 return;
50 }
51
52 $url = remove_query_arg( [ 'wbcr_inp_error', 'wbcr_inp_imported' ] );
53
54 // Only ine files for free version
55 if ( ! WINP_Plugin::app()->get_api_object()->is_key() && count( $_FILES['wbcr_inp_import_files']['tmp_name'] ) > 1 ) {
56 $url = add_query_arg( [ 'wbcr_import_error' => true ], $url );
57 wp_redirect( esc_url_raw( $url ) );
58 exit;
59 }
60
61 $count = 0;
62 $uploads = $_FILES['wbcr_inp_import_files'];
63 $dup_action = WINP_Plugin::app()->request->post( 'duplicate_action', 'ignore', true );
64 $error = false;
65
66 foreach ( $uploads['tmp_name'] as $i => $import_file ) {
67 $ext = pathinfo( $uploads['name'][ $i ] );
68 $ext = $ext['extension'];
69 $mime_type = $uploads['type'][ $i ];
70
71 if ( 'json' === $ext || 'application/json' === $mime_type ) {
72 $result = $this->importSnippet( $import_file, $dup_action );
73 } else {
74 $result = apply_filters( 'wbcr/inp/import/snippet', false, $ext, $mime_type, $import_file, $dup_action );
75 }
76
77 if ( false === $result || - 1 === $result ) {
78 $error = true;
79 } else {
80 $count += count( $result );
81 }
82 }
83
84 $url = add_query_arg( $error ? [ 'wbcr_inp_error' => true ] : [ 'wbcr_inp_imported' => $count ], $url );
85 wp_redirect( esc_url_raw( $url ) );
86 exit;
87 }
88 }
89
90 /**
91 * Import snippets
92 *
93 * @param $file
94 * @param $dup_action
95 *
96 * @return int|bool|array
97 */
98 public function importSnippet( $file, $dup_action ) {
99 if ( ! file_exists( $file ) || ! is_file( $file ) ) {
100 return false;
101 }
102
103 $raw_data = file_get_contents( $file );
104 $data = json_decode( $raw_data, true );
105 $snippets = isset( $data['snippets'] ) ? $data['snippets'] : [];
106
107 $imported = $this->saveImportedSnippets( $snippets, $dup_action );
108
109 return $imported;
110 }
111
112 /**
113 * Update taxonomy tags
114 *
115 * @param $snippet_id
116 * @param $tags
117 */
118 private function updateTaxonomyTags( $snippet_id, $tags ) {
119 if ( ! empty( $tags ) ) {
120 foreach ( $tags as $tag_slug ) {
121 $term = get_term_by( 'slug', $tag_slug, WINP_SNIPPETS_TAXONOMY );
122 if ( $term ) {
123 wp_set_post_terms( $snippet_id, [ $term->term_id ], WINP_SNIPPETS_TAXONOMY, true );
124 }
125 }
126 }
127 }
128
129 /**
130 * Update post meta
131 *
132 * @param $post_id
133 * @param $meta_name
134 * @param $meta_value
135 */
136 private function updateMeta( $post_id, $meta_name, $meta_value ) {
137 update_post_meta( $post_id, WINP_Plugin::app()->getPrefix() . $meta_name, $meta_value );
138 }
139
140 /**
141 * Save snippet
142 *
143 * @param $snippet
144 *
145 * @return int
146 */
147 private function saveSnippet( $snippet ) {
148 $content = $snippet['content'];
149
150 if ( WINP_SNIPPET_TYPE_TEXT != $snippet['type'] ) {
151 $content = empty( $content ) && isset( $snippet['code'] ) && ! empty( $snippet['code'] ) ? $snippet['code'] : $content;
152 }
153
154 $data = [
155 'post_title' => $snippet['title'],
156 'post_content' => $content,
157 'post_status' => 'publish',
158 'post_type' => WINP_SNIPPETS_POST_TYPE
159 ];
160
161 if ( isset( $snippet['id'] ) && 0 != $snippet['id'] ) {
162 $data['ID'] = $snippet['id'];
163 }
164
165 $snippet['id'] = wp_insert_post( $data );
166
167 $this->updateMeta( $snippet['id'], 'snippet_location', $snippet['location'] );
168 $this->updateMeta( $snippet['id'], 'snippet_type', $snippet['type'] );
169 $this->updateMeta( $snippet['id'], 'snippet_filters', $snippet['filters'] );
170 $this->updateMeta( $snippet['id'], 'changed_filters', $snippet['changed_filters'] );
171 $this->updateMeta( $snippet['id'], 'snippet_scope', $snippet['scope'] );
172 $this->updateMeta( $snippet['id'], 'snippet_description', $snippet['description'] );
173 $this->updateMeta( $snippet['id'], 'snippet_tags', $snippet['attributes'] );
174 $this->updateMeta( $snippet['id'], 'snippet_activate', 0 );
175
176 $this->updateTaxonomyTags( $snippet['id'], $snippet['tags'] );
177
178 return $snippet['id'];
179 }
180
181 /**
182 * Save imported snippets
183 *
184 * @param $snippets
185 * @param $dup_action
186 *
187 * @return array
188 */
189 private function saveImportedSnippets( $snippets, $dup_action ) {
190 $existing_snippets = [];
191 if ( 'replace' === $dup_action || 'skip' === $dup_action ) {
192 $all_snippets = get_posts( [ 'post_type' => WINP_SNIPPETS_POST_TYPE ] );
193 foreach ( $all_snippets as $snippet ) {
194 $existing_snippets[ $snippet->post_name ] = $snippet->ID;
195 }
196 }
197
198 $imported = [];
199
200 foreach ( $snippets as $snippet ) {
201 if ( 'ignore' !== $dup_action && isset( $existing_snippets[ $snippet['name'] ] ) ) {
202 if ( 'replace' === $dup_action ) {
203 $snippet['id'] = $existing_snippets[ $snippet['name'] ];
204 } else if ( 'skip' === $dup_action ) {
205 continue;
206 }
207 }
208
209 if ( $snippet_id = $this->saveSnippet( $snippet ) ) {
210 $imported[] = $snippet_id;
211 }
212 }
213
214 return $imported;
215 }
216
217 }
218