You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
10KB

  1. <?php
  2. /*
  3. Plugin Name: Prismatic
  4. Plugin URI: https://perishablepress.com/prismatic/
  5. Description: Display beautiful syntax-highlighted code snippets with Prism.js or Highlight.js
  6. Tags: code, snippets, syntax, highlight, language, snippet, pre, prettify, prism, css, fence
  7. Author: Jeff Starr
  8. Contributors: specialk
  9. Author URI: https://plugin-planet.com/
  10. Donate link: https://monzillamedia.com/donate.html
  11. Requires at least: 4.1
  12. Tested up to: 5.7
  13. Stable tag: 2.7
  14. Version: 2.7
  15. Requires PHP: 5.6.20
  16. Text Domain: prismatic
  17. Domain Path: /languages
  18. License: GPL v2 or later
  19. */
  20. /*
  21. This program is free software; you can redistribute it and/or
  22. modify it under the terms of the GNU General Public License
  23. as published by the Free Software Foundation; either version
  24. 2 of the License, or (at your option) any later version.
  25. This program is distributed in the hope that it will be useful,
  26. but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. GNU General Public License for more details.
  29. You should have received a copy of the GNU General Public License
  30. with this program. If not, visit: https://www.gnu.org/licenses/
  31. Copyright 2021 Monzilla Media. All rights reserved.
  32. */
  33. if (!defined('ABSPATH')) die();
  34. if (!class_exists('Prismatic')) {
  35. final class Prismatic {
  36. private static $instance;
  37. public static function instance() {
  38. if (!isset(self::$instance) && !(self::$instance instanceof Prismatic)) {
  39. self::$instance = new Prismatic;
  40. self::$instance->constants();
  41. self::$instance->includes();
  42. add_action('admin_init', array(self::$instance, 'check_plugin'));
  43. add_action('admin_init', array(self::$instance, 'check_version'));
  44. add_action('plugins_loaded', array(self::$instance, 'load_i18n'));
  45. add_filter('plugin_action_links', array(self::$instance, 'action_links'), 10, 2);
  46. add_filter('plugin_row_meta', array(self::$instance, 'plugin_links'), 10, 2);
  47. add_action('wp_enqueue_scripts', 'prismatic_block_styles');
  48. add_action('wp_enqueue_scripts', 'prismatic_enqueue');
  49. add_action('admin_enqueue_scripts', 'prismatic_enqueue');
  50. add_action('admin_enqueue_scripts', 'prismatic_enqueue_settings');
  51. add_action('admin_enqueue_scripts', 'prismatic_enqueue_buttons');
  52. add_action('admin_print_footer_scripts', 'prismatic_add_quicktags');
  53. add_action('admin_notices', 'prismatic_admin_notice');
  54. add_action('admin_menu', 'prismatic_menu_pages');
  55. add_action('admin_init', 'prismatic_register_settings');
  56. add_action('admin_init', 'prismatic_reset_options');
  57. add_action('admin_init', 'prismatic_buttons');
  58. add_action('init', 'prismatic_register_block_assets');
  59. add_action('init', 'prismatic_add_filters');
  60. add_shortcode('prismatic_code', 'prismatic_code_shortcode');
  61. }
  62. return self::$instance;
  63. }
  64. public static function options_general() {
  65. $options = array(
  66. 'library' => 'none',
  67. 'disable_block_styles' => false,
  68. );
  69. return apply_filters('prismatic_options_general', $options);
  70. }
  71. public static function options_prism() {
  72. $options = array(
  73. 'prism_theme' => 'default',
  74. 'filter_content' => 'none',
  75. 'filter_excerpts' => 'none',
  76. 'filter_comments' => 'none',
  77. 'line_highlight' => false,
  78. 'line_numbers' => false,
  79. 'show_language' => false,
  80. 'copy_clipboard' => false,
  81. 'command_line' => false,
  82. 'singular_only' => true,
  83. );
  84. return apply_filters('prismatic_options_prism', $options);
  85. }
  86. public static function options_highlight() {
  87. $options = array(
  88. 'highlight_theme' => 'default',
  89. 'filter_content' => 'none',
  90. 'filter_excerpts' => 'none',
  91. 'filter_comments' => 'none',
  92. 'init_javascript' => 'hljs.initHighlightingOnLoad();',
  93. 'singular_only' => true,
  94. );
  95. return apply_filters('prismatic_options_highlight', $options);
  96. }
  97. public static function options_plain() {
  98. $options = array(
  99. 'filter_content' => 'none',
  100. 'filter_excerpts' => 'none',
  101. 'filter_comments' => 'none',
  102. );
  103. return apply_filters('prismatic_options_plain', $options);
  104. }
  105. private function constants() {
  106. if (!defined('PRISMATIC_VERSION')) define('PRISMATIC_VERSION', '2.7');
  107. if (!defined('PRISMATIC_REQUIRE')) define('PRISMATIC_REQUIRE', '4.1');
  108. if (!defined('PRISMATIC_NAME')) define('PRISMATIC_NAME', 'Prismatic');
  109. if (!defined('PRISMATIC_AUTHOR')) define('PRISMATIC_AUTHOR', 'Jeff Starr');
  110. if (!defined('PRISMATIC_HOME')) define('PRISMATIC_HOME', 'https://perishablepress.com/prismatic/');
  111. if (!defined('PRISMATIC_URL')) define('PRISMATIC_URL', plugin_dir_url(__FILE__));
  112. if (!defined('PRISMATIC_DIR')) define('PRISMATIC_DIR', plugin_dir_path(__FILE__));
  113. if (!defined('PRISMATIC_FILE')) define('PRISMATIC_FILE', plugin_basename(__FILE__));
  114. if (!defined('PRISMATIC_SLUG')) define('PRISMATIC_SLUG', basename(dirname(__FILE__)));
  115. }
  116. private function includes() {
  117. require_once PRISMATIC_DIR .'inc/prismatic-blocks.php';
  118. require_once PRISMATIC_DIR .'inc/prismatic-buttons.php';
  119. require_once PRISMATIC_DIR .'inc/prismatic-core.php';
  120. require_once PRISMATIC_DIR .'inc/resources-enqueue.php';
  121. require_once PRISMATIC_DIR .'inc/settings-callbacks.php';
  122. require_once PRISMATIC_DIR .'inc/settings-display.php';
  123. require_once PRISMATIC_DIR .'inc/settings-register.php';
  124. require_once PRISMATIC_DIR .'inc/settings-reset.php';
  125. require_once PRISMATIC_DIR .'inc/settings-validate.php';
  126. }
  127. public function action_links($links, $file) {
  128. if ($file == PRISMATIC_FILE && (current_user_can('manage_options'))) {
  129. $prismatic_links = '<a href="'. admin_url('options-general.php?page=prismatic') .'">'. esc_html__('Settings', 'prismatic') .'</a>';
  130. array_unshift($links, $prismatic_links);
  131. }
  132. return $links;
  133. }
  134. public function plugin_links($links, $file) {
  135. if ($file == plugin_basename(__FILE__)) {
  136. $home_href = 'https://perishablepress.com/prismatic/';
  137. $home_title = esc_attr__('Plugin Homepage', 'prismatic');
  138. $home_text = esc_html__('Homepage', 'prismatic');
  139. $links[] = '<a target="_blank" rel="noopener noreferrer" href="'. $home_href .'" title="'. $home_title .'">'. $home_text .'</a>';
  140. $rate_href = 'https://wordpress.org/support/plugin/'. PRISMATIC_SLUG .'/reviews/?rate=5#new-post';
  141. $rate_title = esc_attr__('Click here to rate and review this plugin on WordPress.org', 'prismatic');
  142. $rate_text = esc_html__('Rate this plugin', 'prismatic') .'&nbsp;&raquo;';
  143. $links[] = '<a target="_blank" rel="noopener noreferrer" href="'. $rate_href .'" title="'. $rate_title .'">'. $rate_text .'</a>';
  144. $pro_href = 'https://plugin-planet.com/prismatic-pro/?plugin';
  145. $pro_title = esc_attr__('Get Prismatic Pro!', 'prismatic');
  146. $pro_text = esc_html__('Go&nbsp;Pro', 'prismatic');
  147. $pro_style = 'padding:1px 5px;color:#eee;background:#333;border-radius:1px;';
  148. // $links[] = '<a target="_blank" rel="noopener noreferrer" href="'. $pro_href .'" title="'. $pro_title .'" style="'. $pro_style .'">'. $pro_text .'</a>';
  149. }
  150. return $links;
  151. }
  152. public function check_plugin() {
  153. if (class_exists('Prismatic_Pro')) {
  154. if (is_plugin_active(PRISMATIC_FILE)) {
  155. deactivate_plugins(PRISMATIC_FILE);
  156. $msg = '<strong>'. esc_html__('Warning:', 'prismatic') .'</strong> ';
  157. $msg .= esc_html__('Pro version of Prismatic currently active. Free and Pro versions cannot be activated at the same time. ', 'prismatic');
  158. $msg .= esc_html__('Please return to the', 'prismatic') .' <a href="'. admin_url() .'">'. esc_html__('WP Admin Area', 'prismatic') .'</a> ';
  159. $msg .= esc_html__('and try again.', 'prismatic');
  160. wp_die($msg);
  161. }
  162. }
  163. }
  164. public function check_version() {
  165. $wp_version = get_bloginfo('version');
  166. if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
  167. if (version_compare($wp_version, PRISMATIC_REQUIRE, '<')) {
  168. if (is_plugin_active(PRISMATIC_FILE)) {
  169. deactivate_plugins(PRISMATIC_FILE);
  170. $msg = '<strong>'. PRISMATIC_NAME .'</strong> '. esc_html__('requires WordPress ', 'prismatic') . PRISMATIC_REQUIRE;
  171. $msg .= esc_html__(' or higher, and has been deactivated! ', 'prismatic');
  172. $msg .= esc_html__('Please return to the', 'prismatic') .' <a href="'. admin_url() .'">';
  173. $msg .= esc_html__('WP Admin Area', 'prismatic') .'</a> '. esc_html__('to upgrade WordPress and try again.', 'prismatic');
  174. wp_die($msg);
  175. }
  176. }
  177. }
  178. }
  179. public function load_i18n() {
  180. load_plugin_textdomain('prismatic', false, PRISMATIC_DIR .'languages/');
  181. }
  182. public function __clone() {
  183. _doing_it_wrong(__FUNCTION__, esc_html__('Cheatin&rsquo; huh?', 'prismatic'), PRISMATIC_VERSION);
  184. }
  185. public function __wakeup() {
  186. _doing_it_wrong(__FUNCTION__, esc_html__('Cheatin&rsquo; huh?', 'prismatic'), PRISMATIC_VERSION);
  187. }
  188. }
  189. }
  190. if (class_exists('Prismatic')) {
  191. $prismatic_options_general = get_option('prismatic_options_general', Prismatic::options_general());
  192. $prismatic_options_general = apply_filters('prismatic_get_options_general', $prismatic_options_general);
  193. $prismatic_options_prism = get_option('prismatic_options_prism', Prismatic::options_prism());
  194. $prismatic_options_prism = apply_filters('prismatic_get_options_prism', $prismatic_options_prism);
  195. $prismatic_options_highlight = get_option('prismatic_options_highlight', Prismatic::options_highlight());
  196. $prismatic_options_highlight = apply_filters('prismatic_get_options_highlight', $prismatic_options_highlight);
  197. $prismatic_options_plain = get_option('prismatic_options_plain', Prismatic::options_plain());
  198. $prismatic_options_plain = apply_filters('prismatic_get_options_plain', $prismatic_options_plain);
  199. if (!function_exists('prismatic')) {
  200. function prismatic() {
  201. do_action('prismatic');
  202. return Prismatic::instance();
  203. }
  204. }
  205. prismatic();
  206. }