PluginProbe
Code Snippets / 3.2.1
Code Snippets v3.2.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.2.1, at php/export/class-export.php

182 lines 4.4 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 array
17 */
18 protected $snippets_list;
19
20 /**
21 * Class constructor
22 *
23 * @param array|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, $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 $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, $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( $format ) {
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 string Snippets as JSON object.
72 */
73 public function export_snippets_json() {
74 $snippets = array();
75
76 foreach ( $this->snippets_list as $snippet ) {
77 $fields = array( 'name', 'desc', 'tags', 'scope', 'code', 'priority' );
78 $final_snippet = array();
79
80 foreach ( $fields as $field ) {
81 if ( ! empty( $snippet->$field ) ) {
82 $final_snippet[ $field ] = str_replace( "\r\n", "\n", $snippet->$field );
83 }
84 }
85
86 if ( $final_snippet ) {
87 $snippets[] = $final_snippet;
88 }
89 }
90
91 $data = array(
92 'generator' => 'Code Snippets v' . code_snippets()->version,
93 'date_created' => gmdate( 'Y-m-d H:i' ),
94 'snippets' => $snippets,
95 );
96
97 return wp_json_encode( $data, apply_filters( 'code_snippets/export/json_encode_options', 0 ) );
98 }
99
100 /**
101 * Bundle a snippets into a PHP file.
102 */
103 public function export_snippets_php() {
104 $result = "<?php\n";
105
106 /* @var Snippet $snippet */
107 foreach ( $this->snippets_list as $snippet ) {
108 $code = trim( $snippet->code );
109
110 if ( 'php' !== $snippet->type && 'html' !== $snippet->type || ! $code ) {
111 continue;
112 }
113
114 $result .= "\n/**\n * $snippet->display_name\n";
115
116 if ( ! empty( $snippet->desc ) ) {
117 /* Convert description to PhpDoc */
118 $desc = wp_strip_all_tags( str_replace( "\n", "\n * ", $snippet->desc ) );
119 $result .= " *\n * $desc\n";
120 }
121
122 $result .= " */\n";
123
124 if ( 'content' === $snippet->scope ) {
125 $shortcode_tag = apply_filters( 'code_snippets_export_shortcode_tag', "code_snippets_export_$snippet->id", $snippet );
126
127 $code = sprintf(
128 "add_shortcode( '%s', function () {\n\tob_start();\n\t?>\n\n\t%s\n\n\t<?php\n\treturn ob_get_clean();\n} );",
129 $shortcode_tag,
130 str_replace( "\n", "\n\t", $code )
131 );
132 }
133
134 $result .= "$code\n";
135 }
136
137 return $result;
138 }
139
140 /**
141 * Generate a downloadable CSS or JavaScript file from a list of snippets
142 *
143 * @phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
144 *
145 * @param string $type Snippet type. Supports 'css' or 'js'.
146 */
147 public function export_snippets_code( $type = null ) {
148 $result = '';
149
150 if ( ! $type ) {
151 $type = $this->snippets_list[0]->type;
152 }
153
154 if ( 'php' === $type || 'html' === $type ) {
155 return $this->export_snippets_php();
156 }
157
158 /* Loop through the snippets */
159 foreach ( $this->snippets_list as $snippet ) {
160 $snippet = new Snippet( $snippet );
161
162 if ( $snippet->type !== $type ) {
163 continue;
164 }
165
166 $result .= "\n/*\n";
167
168 if ( $snippet->name ) {
169 $result .= wp_strip_all_tags( $snippet->name ) . "\n\n";
170 }
171
172 if ( ! empty( $snippet->desc ) ) {
173 $result .= wp_strip_all_tags( $snippet->desc ) . "\n";
174 }
175
176 $result .= "*/\n\n{$snippet->code}\n\n";
177 }
178
179 return $result;
180 }
181 }
182