PluginProbe
Sharing Image / 3.4
Sharing Image v3.4
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 3.4, at classes/class-tools.php

195 lines 5.3 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_save_config', array( __CLASS__, 'save_config_tab' ) );
40 add_action( 'admin_post_sharing_image_export_templates', array( __CLASS__, 'export_templates' ) );
41 add_action( 'admin_post_sharing_image_import_templates', array( __CLASS__, 'import_templates' ) );
42 add_action( 'admin_post_sharing_image_clone_template', array( __CLASS__, 'clone_template' ) );
43 add_action( 'admin_post_sharing_image_clear_templates', array( __CLASS__, 'clear_templates' ) );
44 }
45
46 /**
47 * Action to export templates.
48 */
49 public static function export_templates() {
50 check_admin_referer( Settings::SETTINGS_NONCE, 'nonce' );
51
52 if ( ! current_user_can( 'manage_options' ) ) {
53 wp_die( esc_html__( 'Sorry, you do not have permission to manage options for this site.', 'sharing-image' ) );
54 }
55
56 $templates = Templates::get_templates();
57
58 // Remove site-dependent posters.
59 foreach ( $templates as &$template ) {
60 unset( $template['preview'] );
61 }
62
63 $filename = 'sharing-image-export-' . time() . '.json';
64
65 status_header( 200 );
66
67 header( 'Content-Description: File Transfer' );
68 header( "Content-Disposition: attachment; filename={$filename}" );
69 header( 'Content-Type: application/json; charset=utf-8' );
70
71 echo wp_json_encode( $templates, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );
72 exit;
73 }
74
75 /**
76 * Action to import templates.
77 */
78 public static function import_templates() {
79 check_admin_referer( Settings::SETTINGS_NONCE, 'sharing_image_nonce' );
80
81 if ( ! current_user_can( 'manage_options' ) ) {
82 wp_die( esc_html__( 'Sorry, you do not have permission to manage options for this site.', 'sharing-image' ) );
83 }
84
85 if ( ! current_user_can( 'upload_files' ) ) {
86 wp_die( esc_html__( 'Sorry, you do not have permission to upload files.', 'sharing-image' ) );
87 }
88
89 $redirect = Settings::get_tab_link( 'tools' );
90
91 if ( empty( $_FILES['sharing_image_file']['tmp_name'] ) ) {
92 Settings::redirect_with_message( $redirect, 6 );
93 }
94
95 if ( ! empty( $_FILES['sharing_image_file']['error'] ) ) {
96 Settings::redirect_with_message( $redirect, 6 );
97 }
98
99 $file = sanitize_text_field( $_FILES['sharing_image_file']['tmp_name'] );
100
101 // phpcs:ignore WordPress.WP.AlternativeFunctions
102 $templates = file_get_contents( $file );
103 $templates = json_decode( $templates, true );
104
105 // Check if empty.
106 if ( empty( $templates ) || ! is_array( $templates ) ) {
107 Settings::redirect_with_message( $redirect, 7 );
108 }
109
110 foreach ( $templates as $index => $template ) {
111 Templates::update_templates( $index, Templates::sanitize_editor( $template ) );
112 }
113
114 Settings::redirect_with_message( $redirect, 9 );
115 }
116
117 /**
118 * Action to clone template.
119 */
120 public static function clone_template() {
121 check_admin_referer( Settings::SETTINGS_NONCE, 'sharing_image_nonce' );
122
123 if ( ! current_user_can( 'manage_options' ) ) {
124 wp_die( esc_html__( 'Sorry, you are not allowed to manage options for this site.', 'sharing-image' ) );
125 }
126
127 $redirect = Settings::get_tab_link( 'tools' );
128
129 if ( ! isset( $_REQUEST['sharing_image_source'] ) ) {
130 Settings::redirect_with_message( $redirect, 10 );
131 }
132
133 $index = sanitize_key( $_REQUEST['sharing_image_source'] );
134
135 // Get all templates to find by index.
136 $templates = Templates::get_templates();
137
138 if ( ! isset( $templates[ $index ] ) ) {
139 Settings::redirect_with_message( $redirect, 10 );
140 }
141
142 $template = $templates[ $index ];
143
144 // Remove poster if exists.
145 unset( $template['preview'] );
146
147 if ( empty( $template['title'] ) ) {
148 $template['title'] = esc_html__( 'Untitled', 'sharing-image' );
149 }
150
151 $prefix = esc_html__( 'Copy: ' );
152
153 if ( strpos( $template['title'], $prefix ) !== 0 ) {
154 $template['title'] = $prefix . $template['title'];
155 }
156
157 $layers = array();
158
159 foreach ( $template['layers'] as $layer ) {
160 $key = Templates::generate_layer_key();
161
162 $layers[ $key ] = $layer;
163 }
164
165 $template['layers'] = $layers;
166
167 if ( ! Templates::update_templates( Templates::create_unique_index(), $template ) ) {
168 Settings::redirect_with_message( $redirect, 10 );
169 }
170
171 Settings::redirect_with_message( $redirect, 11 );
172 }
173
174 /**
175 * Action to clear templates data.
176 */
177 public static function clear_templates() {
178 check_admin_referer( Settings::SETTINGS_NONCE, 'nonce' );
179
180 if ( ! current_user_can( 'manage_options' ) ) {
181 wp_die( esc_html__( 'Sorry, you do not have permission to manage options for this site.', 'sharing-image' ) );
182 }
183
184 $redirect = Settings::get_tab_link( 'tools' );
185
186 delete_metadata( 'post', null, Widget::META_SOURCE, '', true );
187 delete_metadata( 'post', null, Widget::META_FIELDSET, '', true );
188
189 delete_metadata( 'term', null, Widget::META_SOURCE, '', true );
190 delete_metadata( 'term', null, Widget::META_FIELDSET, '', true );
191
192 Settings::redirect_with_message( $redirect, 12 );
193 }
194 }
195