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-demo.php

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

78 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Create demo template on plugin init.
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 * Demo class.
17 *
18 * @class Demo
19 */
20 class Demo {
21 /**
22 * Init class actions and filters.
23 */
24 public static function init() {
25 add_action( 'load-settings_page_' . Settings::SETTINGS_SLUG, array( __CLASS__, 'create_demo_template' ) );
26 }
27
28 /**
29 * Create demo template after plugin activation.
30 */
31 public static function create_demo_template() {
32 $config = Config::get_config();
33
34 // phpcs:ignore WordPress.Security.NonceVerification
35 if ( ! empty( $config['demo'] ) && ! isset( $_REQUEST['demo-template'] ) ) {
36 return;
37 }
38
39 if ( ! file_exists( SHARING_IMAGE_DIR . 'demo/templates.json' ) ) {
40 return;
41 }
42
43 // phpcs:ignore WordPress.WP.AlternativeFunctions
44 $templates = file_get_contents( SHARING_IMAGE_DIR . 'demo/templates.json' );
45 $templates = json_decode( $templates, true );
46
47 if ( empty( $templates ) ) {
48 return;
49 }
50
51 foreach ( $templates as $template ) {
52 $index = Templates::create_unique_index();
53 $editor = Templates::sanitize_editor( $template );
54
55 // Prepare template editor.
56 $editor = Generator::prepare_template( $editor, null, $index );
57
58 if ( ! Generator::check_required( $editor ) ) {
59 continue;
60 }
61
62 list( $path, $url ) = Generator::get_upload_file();
63
64 $poster = Generator::create_poster( $editor, $path );
65
66 if ( ! is_wp_error( $poster ) ) {
67 $editor['preview'] = $url;
68 }
69
70 Templates::update_templates( $index, $editor );
71 }
72
73 $config['demo'] = 1;
74
75 Config::update_config( $config );
76 }
77 }
78