custom-css-js.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * Plugin Name: Simple Custom CSS and JS
  4. * Plugin URI: https://wordpress.org/plugins/custom-css-js/
  5. * Description: Easily add Custom CSS or JS to your website with an awesome editor.
  6. * Version: 3.28
  7. * Author: SilkyPress.com
  8. * Author URI: https://www.silkypress.com
  9. * License: GPL2
  10. *
  11. * Text Domain: custom-css-js
  12. * Domain Path: /languages/
  13. *
  14. * WC requires at least: 2.3.0
  15. * WC tested up to: 3.8
  16. */
  17. if ( ! defined( 'ABSPATH' ) ) {
  18. exit; // Exit if accessed directly
  19. }
  20. if ( ! class_exists( 'CustomCSSandJS' ) ) :
  21. /**
  22. * Main CustomCSSandJS Class
  23. *
  24. * @class CustomCSSandJS
  25. */
  26. final class CustomCSSandJS {
  27. public $search_tree = false;
  28. protected static $_instance = null;
  29. private $settings = array();
  30. /**
  31. * Main CustomCSSandJS Instance
  32. *
  33. * Ensures only one instance of CustomCSSandJS is loaded or can be loaded
  34. *
  35. * @static
  36. * @return CustomCSSandJS - Main instance
  37. */
  38. public static function instance() {
  39. if ( is_null( self::$_instance ) ) {
  40. self::$_instance = new self();
  41. }
  42. return self::$_instance;
  43. }
  44. /**
  45. * Cloning is forbidden.
  46. */
  47. public function __clone() {
  48. _doing_it_wrong( __FUNCTION__, __( 'An error has occurred. Please reload the page and try again.' ), '1.0' );
  49. }
  50. /**
  51. * Unserializing instances of this class is forbidden.
  52. */
  53. public function __wakeup() {
  54. _doing_it_wrong( __FUNCTION__, __( 'An error has occurred. Please reload the page and try again.' ), '1.0' );
  55. }
  56. /**
  57. * CustomCSSandJS Constructor
  58. * @access public
  59. */
  60. public function __construct() {
  61. include_once( 'includes/admin-install.php' );
  62. register_activation_hook(__FILE__, array('CustomCSSandJS_Install', 'install'));
  63. add_action( 'init', array( 'CustomCSSandJS_Install', 'register_post_type' ) );
  64. $this->set_constants();
  65. if ( is_admin() ) {
  66. $this->load_plugin_textdomain();
  67. include_once( 'includes/admin-screens.php' );
  68. include_once( 'includes/admin-config.php' );
  69. include_once( 'includes/admin-addons.php' );
  70. include_once( 'includes/admin-warnings.php' );
  71. include_once( 'includes/admin-notices.php' );
  72. }
  73. $this->search_tree = get_option( 'custom-css-js-tree' );
  74. $this->settings = get_option('ccj_settings');
  75. if ( ! $this->search_tree || count( $this->search_tree ) == 0 ) {
  76. return false;
  77. }
  78. if ( is_null( self::$_instance ) ) {
  79. $this->print_code_actions();
  80. }
  81. }
  82. /**
  83. * Add the appropriate wp actions
  84. */
  85. function print_code_actions() {
  86. foreach( $this->search_tree as $_key => $_value ) {
  87. $action = 'wp_';
  88. if ( strpos( $_key, 'admin' ) !== false ) {
  89. $action = 'admin_';
  90. }
  91. if ( strpos( $_key, 'login' ) !== false ) {
  92. $action = 'login_';
  93. }
  94. if ( strpos( $_key, 'header' ) !== false ) {
  95. $action .= 'head';
  96. } else {
  97. $action .= 'footer';
  98. }
  99. $priority = ( $action == 'wp_footer' ) ? 40 : 10;
  100. add_action( $action, array( $this, 'print_' . $_key ), $priority );
  101. }
  102. }
  103. /**
  104. * Print the custom code.
  105. */
  106. public function __call( $function, $args ) {
  107. if ( strpos( $function, 'print_' ) === false ) {
  108. return false;
  109. }
  110. $function = str_replace( 'print_', '', $function );
  111. if ( ! isset( $this->search_tree[ $function ] ) ) {
  112. return false;
  113. }
  114. $args = $this->search_tree[ $function ];
  115. if ( ! is_array( $args ) || count( $args ) == 0 ) {
  116. return false;
  117. }
  118. // print the `internal` code
  119. if ( strpos( $function, 'internal' ) !== false ) {
  120. if ( isset($this->settings['remove_comments']) && $this->settings['remove_comments'] ) {
  121. $before = '';
  122. $after = '';
  123. } else {
  124. $before = '<!-- start Simple Custom CSS and JS -->' . PHP_EOL;
  125. $after = '<!-- end Simple Custom CSS and JS -->' . PHP_EOL;
  126. }
  127. if ( strpos( $function, 'css' ) !== false ) {
  128. $before .= '<style type="text/css">' . PHP_EOL;
  129. $after = '</style>' . PHP_EOL . $after;
  130. }
  131. if ( strpos( $function, 'js' ) !== false ) {
  132. $before .= '<script type="text/javascript">' . PHP_EOL;
  133. $after = '</script>' . PHP_EOL . $after;
  134. }
  135. foreach( $args as $_post_id ) {
  136. if ( strstr( $_post_id, 'css' ) || strstr( $_post_id, 'js' ) ) {
  137. if ( isset($this->settings['remove_comments']) && $this->settings['remove_comments'] ) {
  138. ob_start();
  139. @include_once( CCJ_UPLOAD_DIR . '/' . $_post_id );
  140. $custom_code = ob_get_clean();
  141. $custom_code = str_replace(array('<!-- start Simple Custom CSS and JS -->' . PHP_EOL, '<!-- end Simple Custom CSS and JS -->' . PHP_EOL), '', $custom_code);
  142. echo $custom_code;
  143. } else {
  144. @include_once( CCJ_UPLOAD_DIR . '/' . $_post_id );
  145. }
  146. } else {
  147. $post = get_post( $_post_id );
  148. echo $before . $post->post_content . $after;
  149. }
  150. }
  151. }
  152. // link the `external` code
  153. if ( strpos( $function, 'external' ) !== false) {
  154. $in_footer = false;
  155. if ( strpos( $function, 'footer' ) !== false ) {
  156. $in_footer = true;
  157. }
  158. $upload_url = str_replace(array('https://', 'http://'), '//', CCJ_UPLOAD_URL) . '/';
  159. if ( strpos( $function, 'js' ) !== false ) {
  160. foreach( $args as $_filename ) {
  161. echo PHP_EOL . "<script type='text/javascript' src='".$upload_url . $_filename."'></script>" . PHP_EOL;
  162. }
  163. }
  164. if ( strpos( $function, 'css' ) !== false ) {
  165. foreach( $args as $_filename ) {
  166. $shortfilename = preg_replace( '@\.css\?v=.*$@', '', $_filename );
  167. echo PHP_EOL . "<link rel='stylesheet' id='".$shortfilename ."-css' href='".$upload_url . $_filename."' type='text/css' media='all' />" . PHP_EOL;
  168. }
  169. }
  170. }
  171. // link the HTML code
  172. if ( strpos( $function, 'html' ) !== false ) {
  173. foreach( $args as $_post_id ) {
  174. $_post_id = str_replace('.html', '', $_post_id);
  175. $post = get_post( $_post_id );
  176. echo $post->post_content . PHP_EOL;
  177. }
  178. }
  179. }
  180. /**
  181. * Set constants for later use
  182. */
  183. function set_constants() {
  184. $dir = wp_upload_dir();
  185. $constants = array(
  186. 'CCJ_VERSION' => '3.28',
  187. 'CCJ_UPLOAD_DIR' => $dir['basedir'] . '/custom-css-js',
  188. 'CCJ_UPLOAD_URL' => $dir['baseurl'] . '/custom-css-js',
  189. 'CCJ_PLUGIN_FILE' => __FILE__,
  190. );
  191. foreach( $constants as $_key => $_value ) {
  192. if (!defined($_key)) {
  193. define( $_key, $_value );
  194. }
  195. }
  196. }
  197. public function load_plugin_textdomain() {
  198. load_plugin_textdomain( 'custom-css-js', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
  199. }
  200. }
  201. endif;
  202. /**
  203. * Returns the main instance of CustomCSSandJS
  204. *
  205. * @return CustomCSSandJS
  206. */
  207. if ( ! function_exists('CustomCSSandJS' ) ) {
  208. function CustomCSSandJS() {
  209. return CustomCSSandJS::instance();
  210. }
  211. CustomCSSandJS();
  212. }
  213. /**
  214. * Plugin action link to Settings page
  215. */
  216. if ( ! function_exists('custom_css_js_plugin_action_links') ) {
  217. function custom_css_js_plugin_action_links( $links ) {
  218. $settings_link = '<a href="edit.php?post_type=custom-css-js">' .
  219. esc_html( __('Settings', 'custom-css-js' ) ) . '</a>';
  220. return array_merge( array( $settings_link), $links );
  221. }
  222. add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'custom_css_js_plugin_action_links' );
  223. }
  224. /**
  225. * Compatibility with the WP Quads Pro plugin,
  226. * otherwise on a Custom Code save there is a
  227. * "The link you followed has expired." page shown.
  228. */
  229. if ( ! function_exists('custom_css_js_quads_pro_compat') ) {
  230. function custom_css_js_quads_pro_compat( $post_types ) {
  231. $match = array_search('custom-css-js', $post_types);
  232. if ( $match ) {
  233. unset($post_types[$match]);
  234. }
  235. return $post_types;
  236. }
  237. add_filter('quads_meta_box_post_types', 'custom_css_js_quads_pro_compat', 20);
  238. }