PluginProbe
Code Snippets / 3.8.1
Code Snippets v3.8.1
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / export / class-export.php

class-export.php in Code Snippets 3.8.1, at php/export/class-export.php

195 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets;
4
5 /**
6 * Handles exporting snippets from the site in various downloadable formats
7 *
8 * @package Code_Snippets
9 * @since 3.0.0
10 */
11 class Export {
12
13 /**
14 * Array of snippet data fetched from the database
15 *
16 * @var Snippet[]
17 */
18 protected array $snippets_list;
19
20 /**
21 * Class constructor
22 *
23 * @param array<int> $ids List of snippet IDs to export.
24 * @param boolean|null $network Whether to fetch snippets from local or network table.
25 */
26 public function __construct( array $ids, ?bool $network = null ) {
27 $this->snippets_list = get_snippets( $ids, $network );
28 }
29
30 /**
31 * Build the export filename.
32 *
33 * @param string $format File format. Used for file extension.
34 *
35 * @return string
36 */
37 public function build_filename( string $format ): string {
38 if ( 1 === count( $this->snippets_list ) ) {
39 // If there is only snippet to export, use its name instead of the site name.
40 $title = strtolower( $this->snippets_list[0]->name );
41 } else {
42 // Otherwise, use the site name as set in Settings > General.
43 $title = strtolower( get_bloginfo( 'name' ) );
44 }
45
46 $filename = "$title.code-snippets.$format";
47 return apply_filters( 'code_snippets/export/filename', $filename, $title, $this->snippets_list );
48 }
49
50 /**
51 * Bundle snippets together into JSON format.
52 *
53 * @return array<string, string|Snippet[]> Snippets as JSON object.
54 */
55 public function create_export_object(): array {
56 $snippets = array();
57
58 foreach ( $this->snippets_list as $snippet ) {
59 $snippets[] = array_map(
60 function ( $value ) {
61 return is_string( $value ) ?
62 str_replace( "\r\n", "\n", $value ) :
63 $value;
64 },
65 $snippet->get_modified_fields()
66 );
67 }
68
69 return array(
70 'generator' => 'Code Snippets v' . code_snippets()->version,
71 'date_created' => gmdate( 'Y-m-d H:i' ),
72 'snippets' => $snippets,
73 );
74 }
75
76 /**
77 * Bundle a snippets into a PHP file.
78 */
79 public function export_snippets_php(): string {
80 $result = "<?php\n";
81
82 foreach ( $this->snippets_list as $snippet ) {
83 $code = trim( $snippet->code );
84
85 if ( ( 'php' !== $snippet->type && 'html' !== $snippet->type ) || ! $code ) {
86 continue;
87 }
88
89 $result .= "\n/**\n * $snippet->display_name\n";
90
91 if ( ! empty( $snippet->desc ) ) {
92 // Convert description to PhpDoc.
93 $desc = wp_strip_all_tags( str_replace( "\n", "\n * ", $snippet->desc ) );
94 $result .= " *\n * $desc\n";
95 }
96
97 $result .= " */\n";
98
99 if ( 'content' === $snippet->scope ) {
100 $shortcode_tag = apply_filters( 'code_snippets_export_shortcode_tag', "code_snippets_export_$snippet->id", $snippet );
101
102 $code = sprintf(
103 "add_shortcode( '%s', function () {\n\tob_start();\n\t?>\n\n\t%s\n\n\t<?php\n\treturn ob_get_clean();\n} );",
104 $shortcode_tag,
105 str_replace( "\n", "\n\t", $code )
106 );
107 }
108
109 $result .= "$code\n";
110 }
111
112 return $result;
113 }
114
115 /**
116 * Export conditions in JSON format.
117 *
118 * @return string
119 */
120 public function export_conditions_json(): string {
121 $conditions_data = [];
122 $fields_to_copy = [ 'name', 'desc', 'tags' ];
123
124 foreach ( $this->snippets_list as $snippet ) {
125 $condition_data = [];
126
127 if ( ! $snippet->code || 'cond' !== $snippet->type ) {
128 continue;
129 }
130
131 $rules = json_decode( $snippet->code, false );
132
133 if ( json_last_error() !== JSON_ERROR_NONE ) {
134 continue;
135 }
136
137 foreach ( $fields_to_copy as $field ) {
138 if ( ! empty( $snippet->$field ) ) {
139 $condition_data[ $field ] = $snippet->$field;
140 }
141 }
142
143 $condition_data['rules'] = $rules;
144 $conditions_data[] = $condition_data;
145 }
146
147 return wp_json_encode( 1 === count( $conditions_data ) ? $conditions_data[0] : $conditions_data, JSON_PRETTY_PRINT );
148 }
149
150 /**
151 * Generate a downloadable CSS or JavaScript file from a list of snippets
152 *
153 * @phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
154 *
155 * @param string|null $type Snippet type. Supports 'css' or 'js'.
156 */
157 public function export_snippets_code( ?string $type = null ): string {
158 $result = '';
159
160 if ( ! $type ) {
161 $type = $this->snippets_list[0]->type;
162 }
163
164 if ( 'php' === $type || 'html' === $type ) {
165 return $this->export_snippets_php();
166 }
167
168 if ( 'cond' === $type ) {
169 return $this->export_conditions_json();
170 }
171
172 foreach ( $this->snippets_list as $snippet ) {
173 $snippet = new Snippet( $snippet );
174
175 if ( $snippet->type !== $type ) {
176 continue;
177 }
178
179 $result .= "\n/*\n";
180
181 if ( $snippet->name ) {
182 $result .= wp_strip_all_tags( $snippet->name ) . "\n\n";
183 }
184
185 if ( ! empty( $snippet->desc ) ) {
186 $result .= wp_strip_all_tags( $snippet->desc ) . "\n";
187 }
188
189 $result .= "*/\n\n$snippet->code\n\n";
190 }
191
192 return $result;
193 }
194 }
195