|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
-
- if ( ! defined( 'ABSPATH' ) ) exit;
-
- class ChiefRed_Admin {
-
- private static $initiated = false;
-
- public static function init()
- {
- if ( ! self::$initiated ) {
- self::init_hooks();
- }
-
- if (isset($_POST['chiefred_action'])) {
- if ( function_exists('current_user_can') && !current_user_can('manage_options') ) exit('CANT');
- if (
- ! isset( $_POST['chiefred_token'] )
- || ! wp_verify_nonce( $_POST['chiefred_token'], 'wp_rest' )
- ) {
- exit('CSRF');
- } else {
-
- switch ($_POST['chiefred_action']) {
- case 'synchronize':
- wp_clear_scheduled_hook('chiefred_synchronize');
- if (false !== wp_schedule_single_event( time(), 'chiefred_synchronize' )) {
- exit('ok');
- } else {
- exit('notOk');
- }
- case 'clear_synchronize':
- wp_clear_scheduled_hook('chiefred_synchronize');
- exit('ok');
- case 'save':
- update_option('chiefred_site_id', absint($_POST['site_id']));
- if (filter_var(trim($_POST['api_ip']), FILTER_VALIDATE_IP)) {
- update_option('chiefred_api_ip', trim($_POST['api_ip']));
- }
- update_option('chiefred_api_port', absint($_POST['api_port']));
- update_option('chiefred_api_secret', preg_replace('/[^a-zA-Z0-9]+/', '', $_POST['api_secret']));
- update_option('chiefred_article_parent_link', absint($_POST['article_parent_link']));
- update_option('chiefred_article_creators', absint($_POST['article_creators']));
- update_option('chiefred_article_childrens', absint($_POST['article_childrens']));
- update_option('chiefred_article_was_useful', absint($_POST['article_was_useful']));
- if (in_array($_POST['illustration_size'], ['thumbnail', 'medium', 'medium_large', 'full'])) {
- update_option('chiefred_illustration_size', $_POST['illustration_size']);
- }
- update_option('chiefred_illustration_class', sanitize_html_class($_POST['illustration_class']));
- update_option('chiefred_illustration_link', absint($_POST['illustration_link']));
- update_option('chiefred_illustration_link_class', sanitize_html_class($_POST['illustration_link_class']));
- update_option('chiefred_illustration_link_rel', sanitize_html_class($_POST['illustration_link_rel']));
- update_option('chiefred_illustration_link_target', sanitize_html_class($_POST['illustration_link_target']));
- exit('<script type="text/javascript">window.location = window.location.href;</script>');
- }
- }
- }
- }
-
- public static function init_hooks()
- {
- self::$initiated = true;
- add_filter( 'plugin_action_links', [__CLASS__, 'options_link'], 2, 2);
- add_action( 'admin_menu', [__CLASS__, 'admin_menu'], 5 );
- add_action( 'admin_enqueue_scripts', [__CLASS__, 'load_resources'] );
- add_action( 'wp_ajax_display_synchronize_log', [__CLASS__, 'display_synchronize_log'] );
- add_action( 'add_meta_boxes', [__CLASS__, 'switch_boxes'] );
- add_post_type_support( 'page', 'excerpt' );
- }
-
- public static function admin_menu()
- {
- add_options_page(
- 'Настройки плагина API ChiefRed.com',
- 'API ChiefRed.com',
- 'manage_options',
- 'chiefred',
- array( __CLASS__, 'display_options_page' )
- );
- }
-
- public static function options_link($actions, $file)
- {
- if (false !== strpos($file, 'chiefred')) {
- $add['options'] = '<a href="options-general.php?page=chiefred">Настройки</a>';
- $actions = $add + $actions;
- }
- return $actions;
- }
-
- public static function load_resources()
- {
- wp_register_style( 'chiefred_options.css', CHIEFRED_PLUGIN_URL . 'res/chiefred_options.css', array(), CHIEFRED_VERSION );
- wp_enqueue_style( 'chiefred_options.css' );
- wp_register_script( 'chiefred_options.js', CHIEFRED_PLUGIN_URL . 'res/chiefred_options.js', array('jquery'), CHIEFRED_VERSION );
- wp_enqueue_script( 'chiefred_options.js' );
- wp_register_script( 'jquery.mask.min.js', CHIEFRED_PLUGIN_URL . 'res/jquery.mask.min.js', array('jquery'), CHIEFRED_VERSION );
- wp_enqueue_script( 'jquery.mask.min.js' );
- }
-
- public static function display_options_page()
- {
-
- $options['site_id'] = get_option('chiefred_site_id');
- $options['api_ip'] = get_option('chiefred_api_ip');
- $options['api_port'] = get_option('chiefred_api_port');
- $options['api_secret'] = get_option('chiefred_api_secret');
- $options['article_parent_link'] = get_option('chiefred_article_parent_link');
- $options['article_creators'] = get_option('chiefred_article_creators');
- $options['article_childrens'] = get_option('chiefred_article_childrens');
- $options['article_was_useful'] = get_option('chiefred_article_was_useful');
- $options['illustration_size'] = get_option('chiefred_illustration_size');
- $options['illustration_class'] = get_option('chiefred_illustration_class');
- $options['illustration_link'] = get_option('chiefred_illustration_link');
- $options['illustration_link_class'] = get_option('chiefred_illustration_link_class');
- $options['illustration_link_rel'] = get_option('chiefred_illustration_link_rel');
- $options['illustration_link_target'] = get_option('chiefred_illustration_link_target');
-
- foreach (ChiefRed::get_image_sizes() as $size_name => $size) {
- switch ($size_name) {
- case 'thumbnail':
- $options['image_sizes']['thumbnail'] = ['nname'=>'Малый', 'width'=>$size['width']];
- break;
- case 'medium':
- $options['image_sizes']['medium'] = ['nname'=>'Средний', 'width'=>$size['width']];
- break;
- case 'medium_large':
- $options['image_sizes']['medium_large'] = ['nname'=>'Крупный', 'width'=>$size['width']];
- break;
- }
- }
- $options['image_sizes']['full'] = ['nname'=>'Полный', 'width'=>800];
-
- ChiefRed::view( 'options', $options );
- }
-
- public static function display_synchronize_log()
- {
- header('Content-Type: text/plain; charset=utf-8');
- echo htmlspecialchars(file_get_contents( CHIEFRED_PLUGIN_DIR . '/logs/chiefred.log' ));
- wp_die();
- }
-
- public static function switch_boxes()
- {
- if ( ! post_type_supports( $GLOBALS['post']->post_type, 'excerpt' ) )
- {
- return;
- }
- remove_meta_box(
- 'postexcerpt',
- '',
- 'normal'
- );
- add_meta_box(
- 'postexcerpt2',
- __('Excerpt'),
- array(__CLASS__, 'show'),
- null,
- 'normal',
- 'core'
- );
- }
-
- public static function show( $post )
- {
- ?>
- <label class="screen-reader-text" for="excerpt"><?php
- _e( 'Excerpt' )
- ?></label>
- <?php
- wp_editor(
- self::unescape($post->post_excerpt),
- 'excerpt',
- array(
- 'editor_height' => 500,
- 'media_buttons' => FALSE,
- 'teeny' => TRUE,
- 'tinymce' => TRUE
- )
- );
- }
-
- public static function unescape( $str )
- {
- return str_replace(
- array ( '<', '>', '"', '&', ' ', '&nbsp;' ),
- array ( '<', '>', '"', '&', ' ', ' ' ),
- $str
- );
- }
- }
|