| 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 |
|