PluginProbe
Code Snippets / 3.1.1
Code Snippets v3.1.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 / class-export.php

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

160 lines 4.3 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 private $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
38 if ( '' === $table_name ) {
39 $table_name = code_snippets()->db->get_table_name();
40 }
41
42 if ( ! is_array( $ids ) ) {
43 $ids = array( $ids );
44 }
45
46 $this->snippets_list = count( $ids ) ? get_snippets( $ids, $table_name ) : array();
47 }
48
49 /**
50 * Set up the current page to act like a downloadable file instead of being shown in the browser
51 *
52 * @param string $format File format. Used for file extension.
53 * @param string $mime_type File MIME type. Used for Content-Type header.
54 */
55 private function do_headers( $format, $mime_type = 'text/plain' ) {
56
57 /* Build the export filename */
58 if ( 1 === count( $this->snippets_list ) ) {
59 /* If there is only snippet to export, use its name instead of the site name */
60 $title = strtolower( $this->snippets_list[0]->name );
61 } else {
62 /* Otherwise, use the site name as set in Settings > General */
63 $title = strtolower( get_bloginfo( 'name' ) );
64 }
65
66 $filename = "$title.code-snippets.$format";
67 $filename = apply_filters( 'code_snippets/export/filename', $filename, $title, $this->snippets_list );
68
69 /* Set HTTP headers */
70 header( 'Content-Disposition: attachment; filename=' . sanitize_file_name( $filename ) );
71 header( sprintf( "Content-Type: %s; charset=%s", sanitize_mime_type( $mime_type ), get_bloginfo( 'charset' ) ) );
72 }
73
74 /**
75 * Export snippets in JSON format
76 */
77 public function export_snippets() {
78 $snippets = array();
79
80 foreach ( $this->snippets_list as $snippet ) {
81 $fields = array( 'name', 'desc', 'tags', 'scope', 'code', 'priority' );
82 $final_snippet = array();
83
84 foreach ( $fields as $field ) {
85 if ( ! empty( $snippet->$field ) ) {
86 $final_snippet[ $field ] = str_replace( "\r\n", "\n", $snippet->$field );
87 }
88 }
89
90 if ( $final_snippet ) {
91 $snippets[] = $final_snippet;
92 }
93 }
94
95 $this->do_headers( 'json', 'application/json' );
96
97 $data = array(
98 'generator' => 'Code Snippets v' . code_snippets()->version,
99 'date_created' => gmdate( 'Y-m-d H:i' ),
100 'snippets' => $snippets,
101 );
102
103 echo wp_json_encode( $data, apply_filters( 'code_snippets/export/json_encode_options', 0 ) );
104 exit;
105 }
106
107 /**
108 * Export snippets to a downloadable file.
109 *
110 * The Pro version of this function can download snippets to .css and .js files.
111 */
112 public function download_snippets() {
113 $this->download_php_snippets();
114 }
115
116 /**
117 * Generate a downloadable PHP file from a list of snippets
118 * @phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
119 */
120 public function download_php_snippets() {
121 $this->do_headers( 'php', 'text/php' );
122 echo "<?php\n";
123
124 /** Loop through the snippets
125 * @var Snippet $snippet
126 */
127 foreach ( $this->snippets_list as $snippet ) {
128 $code = trim( $snippet->code );
129
130 if ( 'php' !== $snippet->type && 'html' !== $snippet->type || ! $code ) {
131 continue;
132 }
133
134 echo "\n/**\n * $snippet->display_name\n";
135
136 if ( ! empty( $snippet->desc ) ) {
137 /* Convert description to PhpDoc */
138 $desc = str_replace( "\n", "\n * ", $snippet->desc );
139 echo " *\n * ", wp_strip_all_tags( $desc ), "\n";
140 }
141
142 echo " */\n";
143
144 if ( 'content' === $snippet->scope ) {
145 $shortcode_tag = apply_filters( 'code_snippets_export_shortcode_tag', "code_snippets_export_$snippet->id", $snippet );
146
147 $code = sprintf(
148 "add_shortcode( '%s', function () {\n\tob_start();\n\t?>\n\n\t%s\n\n\t<?php\n\treturn ob_get_clean();\n} );",
149 $shortcode_tag,
150 str_replace( "\n", "\n\t", $code )
151 );
152 }
153
154 echo $code, "\n";
155 }
156
157 exit;
158 }
159 }
160