Пример моего кода на PHP... И не только.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

192 Zeilen
8.5KB

  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) exit;
  3. class ChiefRed_Admin {
  4. private static $initiated = false;
  5. public static function init()
  6. {
  7. if ( ! self::$initiated ) {
  8. self::init_hooks();
  9. }
  10. if (isset($_POST['chiefred_action'])) {
  11. if ( function_exists('current_user_can') && !current_user_can('manage_options') ) exit('CANT');
  12. if (
  13. ! isset( $_POST['chiefred_token'] )
  14. || ! wp_verify_nonce( $_POST['chiefred_token'], 'wp_rest' )
  15. ) {
  16. exit('CSRF');
  17. } else {
  18. switch ($_POST['chiefred_action']) {
  19. case 'synchronize':
  20. wp_clear_scheduled_hook('chiefred_synchronize');
  21. if (false !== wp_schedule_single_event( time(), 'chiefred_synchronize' )) {
  22. exit('ok');
  23. } else {
  24. exit('notOk');
  25. }
  26. case 'clear_synchronize':
  27. wp_clear_scheduled_hook('chiefred_synchronize');
  28. exit('ok');
  29. case 'save':
  30. update_option('chiefred_site_id', absint($_POST['site_id']));
  31. if (filter_var(trim($_POST['api_ip']), FILTER_VALIDATE_IP)) {
  32. update_option('chiefred_api_ip', trim($_POST['api_ip']));
  33. }
  34. update_option('chiefred_api_port', absint($_POST['api_port']));
  35. update_option('chiefred_api_secret', preg_replace('/[^a-zA-Z0-9]+/', '', $_POST['api_secret']));
  36. update_option('chiefred_article_parent_link', absint($_POST['article_parent_link']));
  37. update_option('chiefred_article_creators', absint($_POST['article_creators']));
  38. update_option('chiefred_article_childrens', absint($_POST['article_childrens']));
  39. update_option('chiefred_article_was_useful', absint($_POST['article_was_useful']));
  40. if (in_array($_POST['illustration_size'], ['thumbnail', 'medium', 'medium_large', 'full'])) {
  41. update_option('chiefred_illustration_size', $_POST['illustration_size']);
  42. }
  43. update_option('chiefred_illustration_class', sanitize_html_class($_POST['illustration_class']));
  44. update_option('chiefred_illustration_link', absint($_POST['illustration_link']));
  45. update_option('chiefred_illustration_link_class', sanitize_html_class($_POST['illustration_link_class']));
  46. update_option('chiefred_illustration_link_rel', sanitize_html_class($_POST['illustration_link_rel']));
  47. update_option('chiefred_illustration_link_target', sanitize_html_class($_POST['illustration_link_target']));
  48. exit('<script type="text/javascript">window.location = window.location.href;</script>');
  49. }
  50. }
  51. }
  52. }
  53. public static function init_hooks()
  54. {
  55. self::$initiated = true;
  56. add_filter( 'plugin_action_links', [__CLASS__, 'options_link'], 2, 2);
  57. add_action( 'admin_menu', [__CLASS__, 'admin_menu'], 5 );
  58. add_action( 'admin_enqueue_scripts', [__CLASS__, 'load_resources'] );
  59. add_action( 'wp_ajax_display_synchronize_log', [__CLASS__, 'display_synchronize_log'] );
  60. add_action( 'add_meta_boxes', [__CLASS__, 'switch_boxes'] );
  61. add_post_type_support( 'page', 'excerpt' );
  62. }
  63. public static function admin_menu()
  64. {
  65. add_options_page(
  66. 'Настройки плагина API ChiefRed.com',
  67. 'API ChiefRed.com',
  68. 'manage_options',
  69. 'chiefred',
  70. array( __CLASS__, 'display_options_page' )
  71. );
  72. }
  73. public static function options_link($actions, $file)
  74. {
  75. if (false !== strpos($file, 'chiefred')) {
  76. $add['options'] = '<a href="options-general.php?page=chiefred">Настройки</a>';
  77. $actions = $add + $actions;
  78. }
  79. return $actions;
  80. }
  81. public static function load_resources()
  82. {
  83. wp_register_style( 'chiefred_options.css', CHIEFRED_PLUGIN_URL . 'res/chiefred_options.css', array(), CHIEFRED_VERSION );
  84. wp_enqueue_style( 'chiefred_options.css' );
  85. wp_register_script( 'chiefred_options.js', CHIEFRED_PLUGIN_URL . 'res/chiefred_options.js', array('jquery'), CHIEFRED_VERSION );
  86. wp_enqueue_script( 'chiefred_options.js' );
  87. wp_register_script( 'jquery.mask.min.js', CHIEFRED_PLUGIN_URL . 'res/jquery.mask.min.js', array('jquery'), CHIEFRED_VERSION );
  88. wp_enqueue_script( 'jquery.mask.min.js' );
  89. }
  90. public static function display_options_page()
  91. {
  92. $options['site_id'] = get_option('chiefred_site_id');
  93. $options['api_ip'] = get_option('chiefred_api_ip');
  94. $options['api_port'] = get_option('chiefred_api_port');
  95. $options['api_secret'] = get_option('chiefred_api_secret');
  96. $options['article_parent_link'] = get_option('chiefred_article_parent_link');
  97. $options['article_creators'] = get_option('chiefred_article_creators');
  98. $options['article_childrens'] = get_option('chiefred_article_childrens');
  99. $options['article_was_useful'] = get_option('chiefred_article_was_useful');
  100. $options['illustration_size'] = get_option('chiefred_illustration_size');
  101. $options['illustration_class'] = get_option('chiefred_illustration_class');
  102. $options['illustration_link'] = get_option('chiefred_illustration_link');
  103. $options['illustration_link_class'] = get_option('chiefred_illustration_link_class');
  104. $options['illustration_link_rel'] = get_option('chiefred_illustration_link_rel');
  105. $options['illustration_link_target'] = get_option('chiefred_illustration_link_target');
  106. foreach (ChiefRed::get_image_sizes() as $size_name => $size) {
  107. switch ($size_name) {
  108. case 'thumbnail':
  109. $options['image_sizes']['thumbnail'] = ['nname'=>'Малый', 'width'=>$size['width']];
  110. break;
  111. case 'medium':
  112. $options['image_sizes']['medium'] = ['nname'=>'Средний', 'width'=>$size['width']];
  113. break;
  114. case 'medium_large':
  115. $options['image_sizes']['medium_large'] = ['nname'=>'Крупный', 'width'=>$size['width']];
  116. break;
  117. }
  118. }
  119. $options['image_sizes']['full'] = ['nname'=>'Полный', 'width'=>800];
  120. ChiefRed::view( 'options', $options );
  121. }
  122. public static function display_synchronize_log()
  123. {
  124. header('Content-Type: text/plain; charset=utf-8');
  125. echo htmlspecialchars(file_get_contents( CHIEFRED_PLUGIN_DIR . '/logs/chiefred.log' ));
  126. wp_die();
  127. }
  128. public static function switch_boxes()
  129. {
  130. if ( ! post_type_supports( $GLOBALS['post']->post_type, 'excerpt' ) )
  131. {
  132. return;
  133. }
  134. remove_meta_box(
  135. 'postexcerpt',
  136. '',
  137. 'normal'
  138. );
  139. add_meta_box(
  140. 'postexcerpt2',
  141. __('Excerpt'),
  142. array(__CLASS__, 'show'),
  143. null,
  144. 'normal',
  145. 'core'
  146. );
  147. }
  148. public static function show( $post )
  149. {
  150. ?>
  151. <label class="screen-reader-text" for="excerpt"><?php
  152. _e( 'Excerpt' )
  153. ?></label>
  154. <?php
  155. wp_editor(
  156. self::unescape($post->post_excerpt),
  157. 'excerpt',
  158. array(
  159. 'editor_height' => 500,
  160. 'media_buttons' => FALSE,
  161. 'teeny' => TRUE,
  162. 'tinymce' => TRUE
  163. )
  164. );
  165. }
  166. public static function unescape( $str )
  167. {
  168. return str_replace(
  169. array ( '&lt;', '&gt;', '&quot;', '&amp;', '&nbsp;', '&amp;nbsp;' ),
  170. array ( '<', '>', '"', '&', ' ', ' ' ),
  171. $str
  172. );
  173. }
  174. }