PluginProbe
Sharing Image / trunk
Sharing Image vtrunk
3.10 trunk 2.0 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.17 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0 3.1 3.2 3.3 All 29 releases
sharing-image / classes / class-tools.php

class-tools.php in Sharing Image trunk, at classes/class-tools.php

200 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle tools tab on settings page and provide static methods.
4 *
5 * @package sharing-image
6 * @author Anton Lukin
7 */
8
9 namespace Sharing_Image;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 die;
13 }
14
15 /**
16 * Tools class.
17 *
18 * @class Tools
19 */
20 class Tools {
21 /**
22 * Sharing Image config options name.
23 *
24 * @var string
25 */
26 const OPTION_CONFIG = 'sharing_image_config';
27
28 /**
29 * Init class actions and filters.
30 */
31 public static function init() {
32 add_action( 'admin_init', array( __CLASS__, 'handle_requests' ) );
33 }
34
35 /**
36 * Handle tools requests from admin-side.
37 */
38 public static function handle_requests() {
39 add_action( 'admin_post_sharing_image_export_templates', array( __CLASS__, 'export_templates' ) );
40 add_action( 'admin_post_sharing_image_import_templates', array( __CLASS__, 'import_templates' ) );
41 add_action( 'admin_post_sharing_image_clone_template', array( __CLASS__, 'clone_template' ) );
42 add_action( 'admin_post_sharing_image_clear_templates', array( __CLASS__, 'clear_templates' ) );
43 }
44
45 /**
46 * Action to export templates.
47 */
48 public static function export_templates() {
49 check_admin_referer( Settings::SETTINGS_NONCE, 'nonce' );
50
51 if ( ! current_user_can( 'manage_options' ) ) {
52 wp_die( esc_html__( 'Sorry, you do not have permission to manage options for this site.', 'sharing-image' ) );
53 }
54
55 $templates = Templates::get_templates();
56
57 // Remove site-dependent posters.
58 foreach ( $templates as &$template ) {
59 unset( $template['preview'] );
60 }
61
62 $filename = 'sharing-image-export-' . time() . '.json';
63
64 status_header( 200 );
65
66 header( 'Content-Description: File Transfer' );
67 header( "Content-Disposition: attachment; filename={$filename}" );
68 header( 'Content-Type: application/json; charset=utf-8' );
69
70 echo wp_json_encode( $templates, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );
71 exit;
72 }
73
74 /**
75 * Action to import templates.
76 */
77 public static function import_templates() {
78 check_admin_referer( Settings::SETTINGS_NONCE, 'sharing_image_nonce' );
79
80 if ( ! current_user_can( 'manage_options' ) ) {
81 wp_die( esc_html__( 'Sorry, you do not have permission to manage options for this site.', 'sharing-image' ) );
82 }
83
84 if ( ! current_user_can( 'upload_files' ) ) {
85 wp_die( esc_html__( 'Sorry, you do not have permission to upload files.', 'sharing-image' ) );
86 }
87
88 $redirect = Settings::get_tab_link( 'tools' );
89
90 if ( empty( $_FILES['sharing_image_file']['tmp_name'] ) ) {
91 Settings::redirect_with_message( $redirect, 6 );
92 }
93
94 if ( ! empty( $_FILES['sharing_image_file']['error'] ) ) {
95 Settings::redirect_with_message( $redirect, 6 );
96 }
97
98 $file = sanitize_text_field( $_FILES['sharing_image_file']['tmp_name'] );
99
100 // phpcs:ignore WordPress.WP.AlternativeFunctions
101 $templates = file_get_contents( $file );
102 $templates = json_decode( $templates, true );
103
104 // Check if empty.
105 if ( empty( $templates ) || ! is_array( $templates ) ) {
106 Settings::redirect_with_message( $redirect, 7 );
107 }
108
109 foreach ( $templates as $index => $template ) {
110 $index = sanitize_key( $index );
111
112 if ( ! Templates::is_valid_unique_index( $index ) ) {
113 $index = Templates::create_unique_index();
114 }
115
116 Templates::update_templates( $index, Templates::sanitize_editor( $template ) );
117 }
118
119 Settings::redirect_with_message( $redirect, 9 );
120 }
121
122 /**
123 * Action to clone template.
124 */
125 public static function clone_template() {
126 check_admin_referer( Settings::SETTINGS_NONCE, 'sharing_image_nonce' );
127
128 if ( ! current_user_can( 'manage_options' ) ) {
129 wp_die( esc_html__( 'Sorry, you are not allowed to manage options for this site.', 'sharing-image' ) );
130 }
131
132 $redirect = Settings::get_tab_link( 'tools' );
133
134 if ( ! isset( $_REQUEST['sharing_image_source'] ) ) {
135 Settings::redirect_with_message( $redirect, 10 );
136 }
137
138 $index = sanitize_key( $_REQUEST['sharing_image_source'] );
139
140 // Get all templates to find by index.
141 $templates = Templates::get_templates();
142
143 if ( ! isset( $templates[ $index ] ) ) {
144 Settings::redirect_with_message( $redirect, 10 );
145 }
146
147 $template = $templates[ $index ];
148
149 // Remove poster if exists.
150 unset( $template['preview'] );
151
152 if ( empty( $template['title'] ) ) {
153 $template['title'] = esc_html__( 'Untitled', 'sharing-image' );
154 }
155
156 $prefix = esc_html__( 'Copy: ' );
157
158 if ( strpos( $template['title'], $prefix ) !== 0 ) {
159 $template['title'] = $prefix . $template['title'];
160 }
161
162 $layers = array();
163
164 foreach ( $template['layers'] as $layer ) {
165 $key = Templates::generate_layer_key();
166
167 $layers[ $key ] = $layer;
168 }
169
170 $template['layers'] = $layers;
171
172 if ( ! Templates::update_templates( Templates::create_unique_index(), $template ) ) {
173 Settings::redirect_with_message( $redirect, 10 );
174 }
175
176 Settings::redirect_with_message( $redirect, 11 );
177 }
178
179 /**
180 * Action to clear templates data.
181 */
182 public static function clear_templates() {
183 check_admin_referer( Settings::SETTINGS_NONCE, 'nonce' );
184
185 if ( ! current_user_can( 'manage_options' ) ) {
186 wp_die( esc_html__( 'Sorry, you do not have permission to manage options for this site.', 'sharing-image' ) );
187 }
188
189 $redirect = Settings::get_tab_link( 'tools' );
190
191 delete_metadata( 'post', null, Widget::META_SOURCE, '', true );
192 delete_metadata( 'post', null, Widget::META_FIELDSET, '', true );
193
194 delete_metadata( 'term', null, Widget::META_SOURCE, '', true );
195 delete_metadata( 'term', null, Widget::META_FIELDSET, '', true );
196
197 Settings::redirect_with_message( $redirect, 12 );
198 }
199 }
200