PluginProbe
Code Snippets / 3.4.1
Code Snippets v3.4.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.4.1, at php/export/class-export.php

189 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 $snippets_list;
19
20 /**
21 * Class constructor
22 *
23 * @param array<int>|int $ids List of snippet IDs to export.
24 * @param string $table_name Name of the database table to fetch snippets from.
25 */
26 public function __construct( $ids, string $table_name = '' ) {
27 $this->fetch_snippets( $ids, $table_name );
28 }
29
30 /**
31 * Fetch the selected snippets from the database
32 *
33 * @param array<int>|int $ids List of snippet IDs to export.
34 * @param string $table_name Name of database table to fetch snippets from.
35 */
36 private function fetch_snippets( $ids, string $table_name ) {
37 if ( '' === $table_name ) {
38 $table_name = code_snippets()->db->get_table_name();
39 }
40
41 if ( ! is_array( $ids ) ) {
42 $ids = array( $ids );
43 }
44
45 $this->snippets_list = get_snippets( $ids, $table_name );
46 }
47
48 /**
49 * Build the export filename.
50 *
51 * @param string $format File format. Used for file extension.
52 *
53 * @return string
54 */
55 public function build_filename( string $format ): string {
56 if ( 1 === count( $this->snippets_list ) ) {
57 // If there is only snippet to export, use its name instead of the site name.
58 $title = strtolower( $this->snippets_list[0]->name );
59 } else {
60 // Otherwise, use the site name as set in Settings > General.
61 $title = strtolower( get_bloginfo( 'name' ) );
62 }
63
64 $filename = "$title.code-snippets.$format";
65 return apply_filters( 'code_snippets/export/filename', $filename, $title, $this->snippets_list );
66 }
67
68 /**
69 * Bundle snippets together into JSON format.
70 *
71 * @return array<string, string|Snippet[]> Snippets as JSON object.
72 */
73 public function create_export_object(): array {
74 $snippets = array();
75
76 foreach ( $this->snippets_list as $snippet ) {
77 $snippets[] = array_map(
78 function ( $value ) {
79 return is_string( $value ) ?
80 str_replace( "\r\n", "\n", $value ) :
81 $value;
82 },
83 $snippet->get_modified_fields()
84 );
85 }
86
87 return array(
88 'generator' => 'Code Snippets v' . code_snippets()->version,
89 'date_created' => gmdate( 'Y-m-d H:i' ),
90 'snippets' => $snippets,
91 );
92 }
93
94 /**
95 * Bundle a snippets into a PHP file.
96 */
97 public function export_snippets_php(): string {
98 $result = "<?php\n";
99
100 foreach ( $this->snippets_list as $snippet ) {
101 $code = trim( $snippet->code );
102
103 if ( 'php' !== $snippet->type && 'html' !== $snippet->type || ! $code ) {
104 continue;
105 }
106
107 $result .= "\n/**\n * $snippet->display_name\n";
108
109 if ( ! empty( $snippet->desc ) ) {
110 // Convert description to PhpDoc.
111 $desc = wp_strip_all_tags( str_replace( "\n", "\n * ", $snippet->desc ) );
112 $result .= " *\n * $desc\n";
113 }
114
115 $result .= " */\n";
116
117 if ( 'content' === $snippet->scope ) {
118 $shortcode_tag = apply_filters( 'code_snippets_export_shortcode_tag', "code_snippets_export_$snippet->id", $snippet );
119
120 $code = sprintf(
121 "add_shortcode( '%s', function () {\n\tob_start();\n\t?>\n\n\t%s\n\n\t<?php\n\treturn ob_get_clean();\n} );",
122 $shortcode_tag,
123 str_replace( "\n", "\n\t", $code )
124 );
125 }
126
127 $result .= "$code\n";
128 }
129
130 return $result;
131 }
132
133 /**
134 * Export snippets in a generic JSON format that is not intended for importing.
135 *
136 * @return string
137 */
138 public function export_snippets_basic_json(): string {
139 $snippet_data = array();
140
141 foreach ( $this->snippets_list as $snippet ) {
142 $snippet_data[] = $snippet->get_modified_fields();
143 }
144
145 return wp_json_encode( 1 === count( $snippet_data ) ? $snippet_data[0] : $snippet_data );
146 }
147
148 /**
149 * Generate a downloadable CSS or JavaScript file from a list of snippets
150 *
151 * @phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
152 *
153 * @param string|null $type Snippet type. Supports 'css' or 'js'.
154 */
155 public function export_snippets_code( string $type = null ): string {
156 $result = '';
157
158 if ( ! $type ) {
159 $type = $this->snippets_list[0]->type;
160 }
161
162 if ( 'php' === $type || 'html' === $type ) {
163 return $this->export_snippets_php();
164 }
165
166 foreach ( $this->snippets_list as $snippet ) {
167 $snippet = new Snippet( $snippet );
168
169 if ( $snippet->type !== $type ) {
170 continue;
171 }
172
173 $result .= "\n/*\n";
174
175 if ( $snippet->name ) {
176 $result .= wp_strip_all_tags( $snippet->name ) . "\n\n";
177 }
178
179 if ( ! empty( $snippet->desc ) ) {
180 $result .= wp_strip_all_tags( $snippet->desc ) . "\n";
181 }
182
183 $result .= "*/\n\n$snippet->code\n\n";
184 }
185
186 return $result;
187 }
188 }
189