admin-screens.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. <?php
  2. /**
  3. * Custom CSS and JS
  4. *
  5. */
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. exit; // Exit if accessed directly
  8. }
  9. /**
  10. * CustomCSSandJS_Admin
  11. */
  12. class CustomCSSandJS_Admin {
  13. /**
  14. * Default options for a new page
  15. */
  16. private $default_options = array(
  17. 'type' => 'header',
  18. 'linking' => 'internal',
  19. 'side' => 'frontend',
  20. 'priority' => 5,
  21. 'language' => 'css',
  22. );
  23. /**
  24. * Array with the options for a specific custom-css-js post
  25. */
  26. private $options = array();
  27. /**
  28. * Constructor
  29. */
  30. public function __construct() {
  31. $this->add_functions();
  32. }
  33. /**
  34. * Add actions and filters
  35. */
  36. function add_functions() {
  37. // Add filters
  38. $filters = array(
  39. 'manage_custom-css-js_posts_columns' => 'manage_custom_posts_columns',
  40. );
  41. foreach( $filters as $_key => $_value ) {
  42. add_filter( $_key, array( $this, $_value ) );
  43. }
  44. // Add actions
  45. $actions = array(
  46. 'admin_menu' => 'admin_menu',
  47. 'admin_enqueue_scripts' => 'admin_enqueue_scripts',
  48. 'current_screen' => 'current_screen',
  49. 'admin_notices' => 'create_uploads_directory',
  50. 'edit_form_after_title' => 'codemirror_editor',
  51. 'add_meta_boxes' => 'add_meta_boxes',
  52. 'save_post' => 'options_save_meta_box_data',
  53. 'trashed_post' => 'trash_post',
  54. 'untrashed_post' => 'trash_post',
  55. 'wp_ajax_ccj_active_code' => 'wp_ajax_ccj_active_code',
  56. 'wp_ajax_ccj_permalink' => 'wp_ajax_ccj_permalink',
  57. 'post_submitbox_start' => 'post_submitbox_start',
  58. 'restrict_manage_posts' => 'restrict_manage_posts',
  59. 'edit_form_before_permalink' => 'edit_form_before_permalink',
  60. 'before_delete_post' => 'before_delete_post',
  61. );
  62. foreach( $actions as $_key => $_value ) {
  63. add_action( $_key, array( $this, $_value ) );
  64. }
  65. // Add some custom actions/filters
  66. add_action( 'manage_custom-css-js_posts_custom_column', array( $this, 'manage_posts_columns' ), 10, 2 );
  67. add_filter( 'manage_edit-custom-css-js_sortable_columns', array( $this, 'manage_edit_posts_sortable_columns' ) );
  68. add_filter( 'post_row_actions', array( $this, 'post_row_actions' ), 10, 2 );
  69. add_filter( 'parse_query', array($this, 'parse_query') , 10);
  70. add_action('current_screen', array($this, 'current_screen_2'), 100);
  71. }
  72. /**
  73. * Add submenu pages
  74. */
  75. function admin_menu() {
  76. $menu_slug = 'edit.php?post_type=custom-css-js';
  77. $submenu_slug = 'post-new.php?post_type=custom-css-js';
  78. remove_submenu_page( $menu_slug, $submenu_slug);
  79. $title = __('Add Custom CSS', 'custom-css-js');
  80. add_submenu_page( $menu_slug, $title, $title, 'publish_custom_csss', $submenu_slug .'&language=css');
  81. $title = __('Add Custom JS', 'custom-css-js');
  82. add_submenu_page( $menu_slug, $title, $title, 'publish_custom_csss', $submenu_slug . '&language=js');
  83. $title = __('Add Custom HTML', 'custom-css-js');
  84. add_submenu_page( $menu_slug, $title, $title, 'publish_custom_csss', $submenu_slug . '&language=html');
  85. }
  86. /**
  87. * Enqueue the scripts and styles
  88. */
  89. public function admin_enqueue_scripts( $hook ) {
  90. $screen = get_current_screen();
  91. // Only for custom-css-js post type
  92. if ( $screen->post_type != 'custom-css-js' )
  93. return false;
  94. // Some handy variables
  95. $a = plugins_url( '/', CCJ_PLUGIN_FILE). 'assets';
  96. $cm = $a . '/codemirror';
  97. $v = CCJ_VERSION;
  98. wp_register_script( 'ccj-admin', $a . '/ccj_admin.js', array('jquery', 'jquery-ui-resizable'), $v, false );
  99. wp_localize_script( 'ccj-admin', 'CCJ', $this->cm_localize() );
  100. wp_enqueue_script( 'ccj-admin' );
  101. wp_enqueue_style( 'ccj-admin', $a . '/ccj_admin.css', array(), $v );
  102. // Only for the new/edit Code's page
  103. if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
  104. wp_deregister_script('wp-codemirror');
  105. wp_enqueue_style( 'jquery-ui', 'https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css', array(), $v );
  106. wp_enqueue_script( 'ccj-codemirror', $cm . '/lib/codemirror.js', array( 'jquery' ), $v, false);
  107. wp_enqueue_style( 'ccj-codemirror', $cm . '/lib/codemirror.css', array(), $v );
  108. wp_enqueue_script( 'ccj-admin_url_rules', $a . '/ccj_admin-url_rules.js', array('jquery'), $v, false );
  109. // Add the language modes
  110. $cmm = $cm . '/mode/';
  111. wp_enqueue_script('cm-xml', $cmm . 'xml/xml.js', array('ccj-codemirror'), $v, false);
  112. wp_enqueue_script('cm-js', $cmm . 'javascript/javascript.js', array('ccj-codemirror'), $v, false);
  113. wp_enqueue_script('cm-css', $cmm . 'css/css.js', array('ccj-codemirror'), $v, false);
  114. wp_enqueue_script('cm-htmlmixed', $cmm . 'htmlmixed/htmlmixed.js', array('ccj-codemirror'), $v, false);
  115. $cma = $cm . '/addon/';
  116. wp_enqueue_script('cm-dialog', $cma . 'dialog/dialog.js', array('ccj-codemirror'), $v, false);
  117. wp_enqueue_script('cm-search', $cma . 'search/search.js', array('ccj-codemirror'), $v, false);
  118. wp_enqueue_script('cm-searchcursor', $cma . 'search/searchcursor.js',array('ccj-codemirror'), $v, false);
  119. wp_enqueue_script('cm-jump-to-line', $cma . 'search/jump-to-line.js', array('ccj-codemirror'), $v, false);
  120. wp_enqueue_style('cm-dialog', $cma . 'dialog/dialog.css', array(), $v );
  121. // remove the assets from other plugins so it doesn't interfere with CodeMirror
  122. global $wp_scripts;
  123. if (is_array($wp_scripts->registered) && count($wp_scripts->registered) != 0) {
  124. foreach($wp_scripts->registered as $_key => $_value) {
  125. if (!isset($_value->src)) continue;
  126. if (strstr($_value->src, 'wp-content/plugins') !== false
  127. && strstr($_value->src, 'plugins/custom-css-js/assets') === false
  128. && strstr($_value->src, 'plugins/advanced-custom-fields/') === false
  129. && strstr($_value->src, 'plugins/advanced-custom-fields-pro/') === false) {
  130. unset($wp_scripts->registered[$_key]);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * Send variables to the ccj_admin.js script
  138. */
  139. public function cm_localize() {
  140. $vars = array(
  141. 'active' => __('Active', 'custom-css-js'),
  142. 'inactive' => __('Inactive', 'custom-css-js'),
  143. 'activate' => __('Activate', 'custom-css-js'),
  144. 'deactivate' => __('Deactivate', 'custom-css-js'),
  145. 'active_title' => __('The code is active. Click to deactivate it', 'custom-css-js'),
  146. 'deactive_title' => __('The code is inactive. Click to activate it', 'custom-css-js'),
  147. );
  148. return $vars;
  149. }
  150. public function add_meta_boxes() {
  151. add_meta_box('custom-code-options', __('Options', 'custom-css-js'), array( $this, 'custom_code_options_meta_box_callback'), 'custom-css-js', 'side', 'low');
  152. remove_meta_box( 'slugdiv', 'custom-css-js', 'normal' );
  153. }
  154. /**
  155. * Get options for a specific custom-css-js post
  156. */
  157. private function get_options( $post_id ) {
  158. if ( isset( $this->options[ $post_id ] ) ) {
  159. return $this->options[ $post_id ];
  160. }
  161. $options = get_post_meta( $post_id );
  162. if ( empty( $options ) || ! isset ( $options['options'][0] ) ) {
  163. $this->options[ $post_id ] = $this->default_options;
  164. return $this->default_options;
  165. }
  166. $options = unserialize( $options['options'][0] );
  167. $this->options[ $post_id ] = $options;
  168. return $options;
  169. }
  170. /**
  171. * Reformat the `edit` or the `post` screens
  172. */
  173. function current_screen( $current_screen ) {
  174. if ( $current_screen->post_type != 'custom-css-js' ) {
  175. return false;
  176. }
  177. if ( $current_screen->base == 'post' ) {
  178. add_action( 'admin_head', array( $this, 'current_screen_post' ) );
  179. }
  180. if ( $current_screen->base == 'edit' ) {
  181. add_action( 'admin_head', array( $this, 'current_screen_edit' ) );
  182. }
  183. wp_deregister_script( 'autosave' );
  184. }
  185. /**
  186. * Add the buttons in the `edit` screen
  187. */
  188. function add_new_buttons() {
  189. $current_screen = get_current_screen();
  190. if ( (isset($current_screen->action ) && $current_screen->action == 'add') || $current_screen->post_type != 'custom-css-js' ) {
  191. return false;
  192. }
  193. ?>
  194. <div class="updated buttons">
  195. <a href="post-new.php?post_type=custom-css-js&language=css" class="custom-btn custom-css-btn"><?php _e('Add CSS code', 'custom-css-js'); ?></a>
  196. <a href="post-new.php?post_type=custom-css-js&language=js" class="custom-btn custom-js-btn"><?php _e('Add JS code', 'custom-css-js'); ?></a>
  197. <a href="post-new.php?post_type=custom-css-js&language=html" class="custom-btn custom-js-btn"><?php _e('Add HTML code', 'custom-css-js'); ?></a>
  198. <!-- a href="post-new.php?post_type=custom-css-js&language=php" class="custom-btn custom-php-btn">Add PHP code</a -->
  199. </div>
  200. <?php
  201. }
  202. /**
  203. * Add new columns in the `edit` screen
  204. */
  205. function manage_custom_posts_columns( $columns ) {
  206. return array(
  207. 'cb' => '<input type="checkbox" />',
  208. 'active' => '<span class="ccj-dashicons dashicons dashicons-star-empty" title="'.__('Active', 'custom-css-js') .'"></span>',
  209. 'type' => __('Type', 'custom-css-js'),
  210. 'title' => __('Title', 'custom-css-js'),
  211. 'author' => __('Author', 'custom-css-js'),
  212. 'published' => __('Published', 'custom-css-js'),
  213. 'modified' => __('Modified', 'custom-css-js'),
  214. );
  215. }
  216. /**
  217. * Fill the data for the new added columns in the `edit` screen
  218. */
  219. function manage_posts_columns( $column, $post_id ) {
  220. if ( $column == 'type' ) {
  221. $options = $this->get_options( $post_id );
  222. echo '<span class="language language-'.$options['language'].'">' . $options['language'] . '</span>';
  223. }
  224. if ( $column == 'modified' || $column == 'published' ) {
  225. if ( $column == 'modified' ) {
  226. $f_time = get_post_modified_time( __('Y/m/d g:i:s a'), true, $post_id );
  227. $g_time = get_post_modified_time( 'G', true, $post_id );
  228. } else {
  229. $f_time = get_post_time( __('Y/m/d g:i:s a' ), true );
  230. $g_time = get_post_time( 'G', true );
  231. }
  232. $time_diff = time() - $g_time;
  233. if ( $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) {
  234. $h_time = sprintf( __( '%s ago', 'custom-css-js' ), human_time_diff( $g_time) );
  235. } else {
  236. $h_time = mysql2date( get_option('date_format'), $f_time );
  237. }
  238. echo $h_time;
  239. }
  240. if ( $column == 'active' ) {
  241. $url = wp_nonce_url( admin_url( 'admin-ajax.php?action=ccj_active_code&code_id=' . $post_id ), 'ccj-active-code-' .$post_id );
  242. if ( $this->is_active( $post_id ) ) {
  243. $active_title = __('The code is active. Click to deactivate it', 'custom-css-js');
  244. $active_icon = 'dashicons-star-filled';
  245. } else {
  246. $active_title = __('The code is inactive. Click to activate it', 'custom-css-js');
  247. $active_icon = 'dashicons-star-empty ccj_row';
  248. }
  249. echo '<a href="' . esc_url( $url ) . '" class="ccj_activate_deactivate" data-code-id="'.$post_id.'" title="'.$active_title . '">' .
  250. '<span class="dashicons '.$active_icon.'"></span>'.
  251. '</a>';
  252. }
  253. }
  254. /**
  255. * Make the 'Modified' column sortable
  256. */
  257. function manage_edit_posts_sortable_columns( $columns ) {
  258. $columns['modified'] = 'modified';
  259. $columns['published'] = 'published';
  260. return $columns;
  261. }
  262. /**
  263. * List table: Change the query in order to filter by code type
  264. */
  265. function parse_query( $query ){
  266. if ( !is_admin() || !$query->is_main_query() ){
  267. return $query;
  268. }
  269. if ( !isset($query->query['post_type'])) {
  270. return $query;
  271. }
  272. if ( 'custom-css-js' !== $query->query['post_type'] ) {
  273. return $query;
  274. }
  275. $filter = filter_input( INPUT_GET, 'language_filter' );
  276. if ( !is_string($filter) || strlen($filter) == 0 ) {
  277. return $query;
  278. }
  279. global $wpdb;
  280. $post_id_query = "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value LIKE %s";
  281. $post_ids = $wpdb->get_col( $wpdb->prepare($post_id_query, 'options', '%'.$filter.'%') );
  282. if ( !is_array($post_ids) || count($post_ids) == 0 ) {
  283. $post_ids = array(-1);
  284. }
  285. $query->query_vars['post__in'] = $post_ids;
  286. return $query;
  287. }
  288. /**
  289. * List table: add a filter by code type
  290. */
  291. function restrict_manage_posts( $post_type ) {
  292. if('custom-css-js' !== $post_type){
  293. return;
  294. }
  295. $languages = array(
  296. 'css' => __('CSS Codes', 'custom-cs-js'),
  297. 'js' => __('JS Codes', 'custom-css-js'),
  298. 'html' => __('HTML Codes', 'custom-css-js'),
  299. );
  300. echo '<label class="screen-reader-text" for="custom-css-js-filter">' . esc_html__( 'Filter Code Type', 'custom-css-js' ) . '</label>';
  301. echo '<select name="language_filter" id="custom-css-js-filter">';
  302. echo '<option value="">' . __('All Custom Codes', 'custom-css-js'). '</option>';
  303. foreach ( $languages as $_lang => $_label ) {
  304. $selected = selected( filter_input( INPUT_GET, 'language_filter' ), $_lang, false ); ;
  305. echo '<option ' . $selected . ' value="' . $_lang. '">' . $_label . '</option>';
  306. }
  307. echo '</select>';
  308. }
  309. /**
  310. * Activate/deactivate a code
  311. *
  312. * @return void
  313. */
  314. function wp_ajax_ccj_active_code() {
  315. if ( ! isset( $_GET['code_id'] ) ) die();
  316. $code_id = absint( $_GET['code_id'] );
  317. $response = 'error';
  318. if ( check_admin_referer( 'ccj-active-code-' . $code_id) ) {
  319. if ( 'custom-css-js' === get_post_type( $code_id ) ) {
  320. $active = get_post_meta($code_id, '_active', true );
  321. if ( $active === false || $active === '' ) {
  322. $active = 'yes';
  323. }
  324. $response = $active;
  325. update_post_meta( $code_id, '_active', $active === 'yes' ? 'no' : 'yes' );
  326. $this->build_search_tree();
  327. }
  328. }
  329. echo $response;
  330. die();
  331. }
  332. /**
  333. * Check if a code is active
  334. *
  335. * @return bool
  336. */
  337. function is_active( $post_id ) {
  338. return get_post_meta( $post_id, '_active', true ) !== 'no';
  339. }
  340. /**
  341. * Reformat the `edit` screen
  342. */
  343. function current_screen_edit() {
  344. ?>
  345. <script type="text/javascript">
  346. /* <![CDATA[ */
  347. jQuery(window).ready(function($){
  348. var h1 = '<?php _e('Custom Code', 'custom-css-js'); ?> ';
  349. h1 += '<a href="post-new.php?post_type=custom-css-js&language=css" class="page-title-action"><?php _e('Add CSS Code', 'custom-css-js'); ?></a>';
  350. h1 += '<a href="post-new.php?post_type=custom-css-js&language=js" class="page-title-action"><?php _e('Add JS Code', 'custom-css-js'); ?></a>';
  351. h1 += '<a href="post-new.php?post_type=custom-css-js&language=html" class="page-title-action"><?php _e('Add HTML Code', 'custom-css-js'); ?></a>';
  352. $("#wpbody-content h1").html(h1);
  353. });
  354. </script>
  355. <?php
  356. }
  357. /**
  358. * Reformat the `post` screen
  359. */
  360. function current_screen_post() {
  361. $this->remove_unallowed_metaboxes();
  362. $strings = array(
  363. 'Add CSS Code' => __('Add CSS Code', 'custom-css-js'),
  364. 'Add JS Code' => __('Add JS Code', 'custom-css-js'),
  365. 'Add HTML Code' => __('Add HTML Code', 'custom-css-js'),
  366. 'Edit CSS Code' => __('Edit CSS Code', 'custom-css-js'),
  367. 'Edit JS Code' => __('Edit JS Code', 'custom-css-js'),
  368. 'Edit HTML Code' => __('Edit HTML Code', 'custom-css-js'),
  369. );
  370. if ( isset( $_GET['post'] ) ) {
  371. $action = 'Edit';
  372. $post_id = esc_attr($_GET['post']);
  373. } else {
  374. $action = 'Add';
  375. $post_id = false;
  376. }
  377. $language = $this->get_language($post_id);
  378. $title = $action . ' ' . strtoupper( $language ) . ' Code';
  379. $title = (isset($strings[$title])) ? $strings[$title] : $strings['Add CSS Code'];
  380. if ( $action == 'Edit' ) {
  381. $title .= ' <a href="post-new.php?post_type=custom-css-js&language=css" class="page-title-action">'.__('Add CSS Code', 'custom-css-js') .'</a> ';
  382. $title .= '<a href="post-new.php?post_type=custom-css-js&language=js" class="page-title-action">'.__('Add JS Code', 'custom-css-js') .'</a>';
  383. $title .= '<a href="post-new.php?post_type=custom-css-js&language=html" class="page-title-action">'.__('Add HTML Code', 'custom-css-js') .'</a>';
  384. }
  385. ?>
  386. <style type="text/css">
  387. #post-body-content, .edit-form-section { position: static !important; }
  388. #ed_toolbar { display: none; }
  389. #postdivrich { display: none; }
  390. </style>
  391. <script type="text/javascript">
  392. /* <![CDATA[ */
  393. jQuery(window).ready(function($){
  394. $("#wpbody-content h1").html('<?php echo $title; ?>');
  395. $("#message.updated.notice").html('<p><?php _e('Code updated', 'custom-css-js'); ?></p>');
  396. var from_top = -$("#normal-sortables").height();
  397. if ( from_top != 0 ) {
  398. $(".ccj_only_premium-first").css('margin-top', from_top.toString() + 'px' );
  399. } else {
  400. $(".ccj_only_premium-first").hide();
  401. }
  402. });
  403. /* ]]> */
  404. </script>
  405. <?php
  406. }
  407. /**
  408. * Remove unallowed metaboxes from custom-css-js edit page
  409. *
  410. * Use the custom-css-js-meta-boxes filter to add/remove allowed metaboxdes on the page
  411. */
  412. function remove_unallowed_metaboxes() {
  413. global $wp_meta_boxes;
  414. // Side boxes
  415. $allowed = array( 'submitdiv', 'custom-code-options' );
  416. $allowed = apply_filters( 'custom-css-js-meta-boxes', $allowed );
  417. foreach( $wp_meta_boxes['custom-css-js']['side'] as $_priority => $_boxes ) {
  418. foreach( $_boxes as $_key => $_value ) {
  419. if ( ! in_array( $_key, $allowed ) ) {
  420. unset( $wp_meta_boxes['custom-css-js']['side'][$_priority][$_key] );
  421. }
  422. }
  423. }
  424. // Normal boxes
  425. $allowed = array( 'slugdiv', 'previewdiv', 'url-rules', 'revisionsdiv' );
  426. $allowed = apply_filters( 'custom-css-js-meta-boxes-normal', $allowed );
  427. foreach( $wp_meta_boxes['custom-css-js']['normal'] as $_priority => $_boxes ) {
  428. foreach( $_boxes as $_key => $_value ) {
  429. if ( ! in_array( $_key, $allowed ) ) {
  430. unset( $wp_meta_boxes['custom-css-js']['normal'][$_priority][$_key] );
  431. }
  432. }
  433. }
  434. unset($wp_meta_boxes['custom-css-js']['advanced']);
  435. }
  436. /**
  437. * Add the codemirror editor in the `post` screen
  438. */
  439. public function codemirror_editor( $post ) {
  440. $current_screen = get_current_screen();
  441. if ( $current_screen->post_type != 'custom-css-js' ) {
  442. return false;
  443. }
  444. if ( empty( $post->title ) && empty( $post->post_content ) ) {
  445. $new_post = true;
  446. $post_id = false;
  447. } else {
  448. $new_post = false;
  449. if ( ! isset( $_GET['post'] ) ) $_GET['post'] = $post->id;
  450. $post_id = esc_attr($_GET['post']);
  451. }
  452. $language = $this->get_language($post_id);
  453. $settings = get_option( 'ccj_settings' );
  454. // Replace the htmlentities (https://wordpress.org/support/topic/annoying-bug-in-text-editor/), but only selectively
  455. if ( isset($settings['ccj_htmlentities']) && $settings['ccj_htmlentities'] == 1 && strstr($post->post_content, '&') ) {
  456. // First the ampresands
  457. $post->post_content = str_replace('&amp', htmlentities('&amp'), $post->post_content );
  458. // Then the rest of the entities
  459. $html_flags = defined('ENT_HTML5') ? ENT_QUOTES | ENT_HTML5 : ENT_QUOTES;
  460. $entities = get_html_translation_table(HTML_ENTITIES, $html_flags);
  461. unset( $entities[ array_search('&amp;', $entities) ]);
  462. $regular_expression = str_replace(';', '', '/('.implode('|', $entities).')/i');
  463. preg_match_all($regular_expression, $post->post_content, $matches);
  464. if ( isset($matches[0]) && count($matches[0]) > 0 ) {
  465. foreach($matches[0] as $_entity) {
  466. $post->post_content = str_replace($_entity, htmlentities($_entity), $post->post_content);
  467. }
  468. }
  469. }
  470. if ( isset($settings['ccj_htmlentities2']) && $settings['ccj_htmlentities2'] == 1 ) {
  471. $post->post_content = htmlentities( $post->post_content );
  472. }
  473. switch ( $language ) {
  474. case 'js' :
  475. if ( $new_post ) {
  476. $post->post_content = __('/* Add your JavaScript code here.
  477. If you are using the jQuery library, then don\'t forget to wrap your code inside jQuery.ready() as follows:
  478. jQuery(document).ready(function( $ ){
  479. // Your code in here
  480. });
  481. --
  482. If you want to link a JavaScript file that resides on another server (similar to
  483. <script src="https://example.com/your-js-file.js"></script>), then please use
  484. the "Add HTML Code" page, as this is a HTML code that links a JavaScript file.
  485. End of comment */ ', 'custom-css-js') . PHP_EOL . PHP_EOL;
  486. }
  487. $code_mirror_mode = 'text/javascript';
  488. $code_mirror_before = '<script type="text/javascript">';
  489. $code_mirror_after = '</script>';
  490. break;
  491. case 'html' :
  492. if ( $new_post ) {
  493. $post->post_content = __('<!-- Add HTML code to the header or the footer.
  494. For example, you can use the following code for loading the jQuery library from Google CDN:
  495. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  496. or the following one for loading the Bootstrap library from MaxCDN:
  497. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  498. -- End of the comment --> ', 'custom-css-js') . PHP_EOL . PHP_EOL;
  499. }
  500. $code_mirror_mode = 'html';
  501. $code_mirror_before = '';
  502. $code_mirror_after = '';
  503. break;
  504. case 'php' :
  505. if ( $new_post ) {
  506. $post->post_content = '/* The following will be executed as if it were written in functions.php. */' . PHP_EOL . PHP_EOL;
  507. }
  508. $code_mirror_mode = 'php';
  509. $code_mirror_before = '<?php';
  510. $code_mirror_after = '?>';
  511. break;
  512. default :
  513. if ( $new_post ) {
  514. $post->post_content = __('/* Add your CSS code here.
  515. For example:
  516. .example {
  517. color: red;
  518. }
  519. For brushing up on your CSS knowledge, check out http://www.w3schools.com/css/css_syntax.asp
  520. End of comment */ ', 'custom-css-js') . PHP_EOL . PHP_EOL;
  521. }
  522. $code_mirror_mode = 'text/css';
  523. $code_mirror_before = '<style type="text/css">';
  524. $code_mirror_after = '</style>';
  525. }
  526. ?>
  527. <form style="position: relative; margin-top: .5em;">
  528. <div class="code-mirror-before"><div><?php echo htmlentities( $code_mirror_before );?></div></div>
  529. <textarea class="wp-editor-area" id="ccj_content" mode="<?php echo htmlentities($code_mirror_mode); ?>" name="content"><?php echo $post->post_content; ?></textarea>
  530. <div class="code-mirror-after"><div><?php echo htmlentities( $code_mirror_after );?></div></div>
  531. <table id="post-status-info"><tbody><tr>
  532. <td class="autosave-info">
  533. <span class="autosave-message">&nbsp;</span>
  534. <?php
  535. if ( 'auto-draft' != $post->post_status ) {
  536. echo '<span id="last-edit">';
  537. if ( $last_user = get_userdata( get_post_meta( $post->ID, '_edit_last', true ) ) ) {
  538. printf(__('Last edited by %1$s on %2$s at %3$s', 'custom-css-js-pro'), esc_html( $last_user->display_name ), mysql2date(get_option('date_format'), $post->post_modified), mysql2date(get_option('time_format'), $post->post_modified));
  539. } else {
  540. printf(__('Last edited on %1$s at %2$s', 'custom-css-js-pro'), mysql2date(get_option('date_format'), $post->post_modified), mysql2date(get_option('time_format'), $post->post_modified));
  541. }
  542. echo '</span>';
  543. } ?>
  544. </td>
  545. </tr></tbody></table>
  546. <input type="hidden" id="update-post_<?php echo $post->ID ?>" value="<?php echo wp_create_nonce('update-post_'. $post->ID ); ?>" />
  547. </form>
  548. <?php
  549. }
  550. /**
  551. * Show the options form in the `post` screen
  552. */
  553. function custom_code_options_meta_box_callback( $post ) {
  554. $options = $this->get_options( $post->ID );
  555. if ( ! isset($options['preprocessor'] ) )
  556. $options['preprocessor'] = 'none';
  557. if ( isset( $_GET['language'] ) ) {
  558. $options['language'] = $this->get_language();
  559. }
  560. $meta = $this->get_options_meta();
  561. if ( $options['language'] == 'html' ) {
  562. $meta = $this->get_options_meta_html();
  563. }
  564. wp_nonce_field( 'options_save_meta_box_data', 'custom-css-js_meta_box_nonce' );
  565. ?>
  566. <div class="options_meta_box">
  567. <?php
  568. $output = '';
  569. foreach( $meta as $_key => $a ) {
  570. $close_div = false;
  571. if ( ($_key == 'preprocessor' && $options['language'] == 'css') ||
  572. ($_key == 'linking' && $options['language'] == 'html') ||
  573. $_key == 'priority' ||
  574. $_key == 'minify' ) {
  575. $close_div = true;
  576. $output .= '<div class="ccj_opaque">';
  577. }
  578. // Don't show Pre-processors for JavaScript Codes
  579. if ( $options['language'] == 'js' && $_key == 'preprocessor' ) {
  580. continue;
  581. }
  582. $output .= '<h3>' . $a['title'] . '</h3>' . PHP_EOL;
  583. $output .= $this->render_input( $_key, $a, $options );
  584. if ( $close_div ) {
  585. $output .= '</div>';
  586. }
  587. }
  588. echo $output;
  589. ?>
  590. <input type="hidden" name="custom_code_language" value="<?php echo $options['language']; ?>" />
  591. <div style="clear: both;"></div>
  592. </div>
  593. <div class="ccj_only_premium ccj_only_premium-right">
  594. <div>
  595. <a href="https://www.silkypress.com/simple-custom-css-js-pro/?utm_source=wordpress&utm_campaign=ccj_free&utm_medium=banner" target="_blank"><?php _e('Available only in <br />Simple Custom CSS and JS Pro', 'custom-css-js'); ?></a>
  596. </div>
  597. </div>
  598. <?php
  599. }
  600. /**
  601. * Get an array with all the information for building the code's options
  602. */
  603. function get_options_meta() {
  604. $options = array(
  605. 'linking' => array(
  606. 'title' => __('Linking type', 'custom-css-js'),
  607. 'type' => 'radio',
  608. 'default' => 'internal',
  609. 'values' => array(
  610. 'external' => array(
  611. 'title' => __('External File', 'custom-css-js'),
  612. 'dashicon' => 'media-code',
  613. ),
  614. 'internal' => array(
  615. 'title' => __('Internal', 'custom-css-js'),
  616. 'dashicon' => 'editor-alignleft',
  617. ),
  618. ),
  619. ),
  620. 'type' => array(
  621. 'title' => __('Where on page', 'custom-css-js'),
  622. 'type' => 'radio',
  623. 'default' => 'header',
  624. 'values' => array(
  625. 'header' => array(
  626. 'title' => __('Header', 'custom-css-js'),
  627. 'dashicon' => 'arrow-up-alt2',
  628. ),
  629. 'footer' => array(
  630. 'title' => __('Footer', 'custom-css-js'),
  631. 'dashicon' => 'arrow-down-alt2',
  632. ),
  633. ),
  634. ),
  635. 'side' => array(
  636. 'title' => __('Where in site', 'custom-css-js'),
  637. 'type' => 'radio',
  638. 'default' => 'frontend',
  639. 'values' => array(
  640. 'frontend' => array(
  641. 'title' => __('In Frontend', 'custom-css-js'),
  642. 'dashicon' => 'tagcloud',
  643. ),
  644. 'admin' => array(
  645. 'title' => __('In Admin', 'custom-css-js'),
  646. 'dashicon' => 'id',
  647. ),
  648. 'login' => array(
  649. 'title' => __('On Login Page', 'custom-css-js'),
  650. 'dashicon' => 'admin-network',
  651. ),
  652. ),
  653. ),
  654. 'preprocessor' => array(
  655. 'title' => __('CSS Preprocessor', 'custom-css-js'),
  656. 'type' => 'radio',
  657. 'default' => 'none',
  658. 'values' => array(
  659. 'none' => array(
  660. 'title' => __('None', 'custom-css-js'),
  661. ),
  662. 'less' => array(
  663. 'title' => __('Less', 'custom-css-js'),
  664. ),
  665. 'sass' => array(
  666. 'title' => __('SASS (only SCSS syntax)', 'custom-css-js'),
  667. ),
  668. ),
  669. 'disabled' => true,
  670. ),
  671. 'minify' => array(
  672. 'title' => __('Minify the code', 'custom-css-js'),
  673. 'type' => 'checkbox',
  674. 'default' => false,
  675. 'dashicon' => 'editor-contract',
  676. 'disabled' => true,
  677. ),
  678. 'priority' => array(
  679. 'title' => __('Priority', 'custom-css-js'),
  680. 'type' => 'select',
  681. 'default' => 5,
  682. 'dashicon' => 'sort',
  683. 'values' => array(
  684. 1 => _x('1 (highest)', '1 is the highest priority', 'custom-css-js'),
  685. 2 => '2',
  686. 3 => '3',
  687. 4 => '4',
  688. 5 => '5',
  689. 6 => '6',
  690. 7 => '7',
  691. 8 => '8',
  692. 9 => '9',
  693. 10 => _x('10 (lowest)', '10 is the lowest priority', 'custom-css-js'),
  694. ),
  695. 'disabled' => true,
  696. ),
  697. );
  698. return $options;
  699. }
  700. /**
  701. * Get an array with all the information for building the code's options
  702. */
  703. function get_options_meta_html() {
  704. $options = array(
  705. 'type' => array(
  706. 'title' => __('Where on page', 'custom-css-js'),
  707. 'type' => 'radio',
  708. 'default' => 'header',
  709. 'values' => array(
  710. 'header' => array(
  711. 'title' => __('Header', 'custom-css-js'),
  712. 'dashicon' => 'arrow-up-alt2',
  713. ),
  714. 'footer' => array(
  715. 'title' => __('Footer', 'custom-css-js'),
  716. 'dashicon' => 'arrow-down-alt2',
  717. ),
  718. ),
  719. ),
  720. 'side' => array(
  721. 'title' => __('Where in site', 'custom-css-js'),
  722. 'type' => 'radio',
  723. 'default' => 'frontend',
  724. 'values' => array(
  725. 'frontend' => array(
  726. 'title' => __('In Frontend', 'custom-css-js'),
  727. 'dashicon' => 'tagcloud',
  728. ),
  729. 'admin' => array(
  730. 'title' => __('In Admin', 'custom-css-js'),
  731. 'dashicon' => 'id',
  732. ),
  733. ),
  734. ),
  735. 'linking' => array(
  736. 'title' => __('On which device', 'custom-css-js'),
  737. 'type' => 'radio',
  738. 'default' => 'both',
  739. 'dashicon' => '',
  740. 'values' => array(
  741. 'desktop' => array(
  742. 'title' => __('Desktop', 'custom-css-js'),
  743. 'dashicon' => 'desktop',
  744. ),
  745. 'mobile' => array(
  746. 'title' => __('Mobile', 'custom-css-js'),
  747. 'dashicon' => 'smartphone',
  748. ),
  749. 'both' => array(
  750. 'title' => __('Both', 'custom-css-js'),
  751. 'dashicon' => 'tablet',
  752. ),
  753. ),
  754. 'disabled' => true,
  755. ),
  756. 'priority' => array(
  757. 'title' => __('Priority', 'custom-css-js'),
  758. 'type' => 'select',
  759. 'default' => 5,
  760. 'dashicon' => 'sort',
  761. 'values' => array(
  762. 1 => _x('1 (highest)', '1 is the highest priority', 'custom-css-js'),
  763. 2 => '2',
  764. 3 => '3',
  765. 4 => '4',
  766. 5 => '5',
  767. 6 => '6',
  768. 7 => '7',
  769. 8 => '8',
  770. 9 => '9',
  771. 10 => _x('10 (lowest)', '10 is the lowest priority', 'custom-css-js'),
  772. ),
  773. 'disabled' => true,
  774. ),
  775. );
  776. return $options;
  777. }
  778. /**
  779. * Save the post and the metadata
  780. */
  781. function options_save_meta_box_data( $post_id ) {
  782. // The usual checks
  783. if ( ! isset( $_POST['custom-css-js_meta_box_nonce'] ) ) {
  784. return;
  785. }
  786. if ( ! wp_verify_nonce( $_POST['custom-css-js_meta_box_nonce'], 'options_save_meta_box_data' ) ) {
  787. return;
  788. }
  789. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  790. return;
  791. }
  792. if ( isset( $_POST['post_type'] ) && 'custom-css-js' != $_POST['post_type'] ) {
  793. return;
  794. }
  795. // Update the post's meta
  796. $defaults = array(
  797. 'type' => 'header',
  798. 'linking' => 'internal',
  799. 'priority' => 5,
  800. 'side' => 'frontend',
  801. 'language' => 'css',
  802. );
  803. if ( $_POST['custom_code_language'] == 'html' ) {
  804. $defaults = array(
  805. 'type' => 'header',
  806. 'linking' => 'both',
  807. 'side' => 'frontend',
  808. 'language' => 'html',
  809. 'priority' => 5,
  810. );
  811. }
  812. foreach( $defaults as $_field => $_default ) {
  813. $options[ $_field ] = isset( $_POST['custom_code_'.$_field] ) ? esc_attr($_POST['custom_code_'.$_field]) : $_default;
  814. }
  815. update_post_meta( $post_id, 'options', $options );
  816. if ( $options['language'] == 'html' ) {
  817. $this->build_search_tree();
  818. return;
  819. }
  820. if ( $options['language'] == 'js' ) {
  821. // Replace the default comment
  822. if ( preg_match('@/\* Add your JavaScript code here[\s\S]*?End of comment \*/@im', $_POST['content'] ) ) {
  823. $_POST['content'] = preg_replace('@/\* Add your JavaScript code here[\s\S]*?End of comment \*/@im', '/* Default comment here */', $_POST['content']);
  824. }
  825. // For other locales remove all the comments
  826. if ( substr( get_locale(), 0, 3) !== 'en_' ) {
  827. $_POST['content'] = preg_replace('@/\*[\s\S]*?\*/@', '', $_POST['content']);
  828. }
  829. }
  830. // Save the Custom Code in a file in `wp-content/uploads/custom-css-js`
  831. if ( $options['linking'] == 'internal' ) {
  832. $before = '<!-- start Simple Custom CSS and JS -->' . PHP_EOL;
  833. $after = '<!-- end Simple Custom CSS and JS -->' . PHP_EOL;
  834. if ( $options['language'] == 'css' ) {
  835. $before .= '<style type="text/css">' . PHP_EOL;
  836. $after = '</style>' . PHP_EOL . $after;
  837. }
  838. if ( $options['language'] == 'js' ) {
  839. if ( ! preg_match( '/<script\b[^>]*>([\s\S]*?)<\/script>/im', $_POST['content'] ) ) {
  840. $before .= '<script type="text/javascript">' . PHP_EOL;
  841. $after = '</script>' . PHP_EOL . $after;
  842. } else {
  843. // the content has a <script> tag, then remove the comments so they don't show up on the frontend
  844. $_POST['content'] = preg_replace('@/\*[\s\S]*?\*/@', '', $_POST['content']);
  845. }
  846. }
  847. }
  848. if ( $options['linking'] == 'external' ) {
  849. $before = '/******* Do not edit this file *******' . PHP_EOL .
  850. 'Simple Custom CSS and JS - by Silkypress.com' . PHP_EOL .
  851. 'Saved: '.date('M d Y | H:i:s').' */' . PHP_EOL;
  852. }
  853. if ( wp_is_writable( CCJ_UPLOAD_DIR ) ) {
  854. $file_name = $post_id . '.' . $options['language'];
  855. $file_content = $before . stripslashes($_POST['content']) . $after;
  856. @file_put_contents( CCJ_UPLOAD_DIR . '/' . $file_name , $file_content );
  857. // save the file as the Permalink slug
  858. $slug = get_post_meta( $post_id, '_slug', true );
  859. if ( $slug ) {
  860. @file_put_contents( CCJ_UPLOAD_DIR . '/' . $slug . '.' . $options['language'], $file_content );
  861. }
  862. }
  863. $this->build_search_tree();
  864. }
  865. /**
  866. * Create the custom-css-js dir in uploads directory
  867. *
  868. * Show a message if the directory is not writable
  869. *
  870. * Create an empty index.php file inside
  871. */
  872. function create_uploads_directory() {
  873. $current_screen = get_current_screen();
  874. // Check if we are editing a custom-css-js post
  875. if ( $current_screen->base != 'post' || $current_screen->post_type != 'custom-css-js' ) {
  876. return false;
  877. }
  878. $dir = CCJ_UPLOAD_DIR;
  879. // Create the dir if it doesn't exist
  880. if ( ! file_exists( $dir ) ) {
  881. wp_mkdir_p( $dir );
  882. }
  883. // Show a message if it couldn't create the dir
  884. if ( ! file_exists( $dir ) ) : ?>
  885. <div class="notice notice-error is-dismissible">
  886. <p><?php printf(__('The %s directory could not be created', 'custom-css-js'), '<b>custom-css-js</b>'); ?></p>
  887. <p><?php _e('Please run the following commands in order to make the directory', 'custom-css-js'); ?>: <br /><strong>mkdir <?php echo $dir; ?>; </strong><br /><strong>chmod 777 <?php echo $dir; ?>;</strong></p>
  888. </div>
  889. <?php return; endif;
  890. // Show a message if the dir is not writable
  891. if ( ! wp_is_writable( $dir ) ) : ?>
  892. <div class="notice notice-error is-dismissible">
  893. <p><?php printf(__('The %s directory is not writable, therefore the CSS and JS files cannot be saved.', 'custom-css-js'), '<b>'.$dir.'</b>'); ?></p>
  894. <p><?php _e('Please run the following command to make the directory writable', 'custom-css-js'); ?>:<br /><strong>chmod 777 <?php echo $dir; ?> </strong></p>
  895. </div>
  896. <?php return; endif;
  897. // Write a blank index.php
  898. if ( ! file_exists( $dir. '/index.php' ) ) {
  899. $content = '<?php' . PHP_EOL . '// Silence is golden.';
  900. @file_put_contents( $dir. '/index.php', $content );
  901. }
  902. }
  903. /**
  904. * Build a tree where you can quickly find the needed custom-css-js posts
  905. *
  906. * @return void
  907. */
  908. private function build_search_tree() {
  909. // Retrieve all the custom-css-js codes
  910. $posts = query_posts( 'post_type=custom-css-js&post_status=publish&nopaging=true' );
  911. $tree = array();
  912. foreach ( $posts as $_post ) {
  913. if ( ! $this->is_active( $_post->ID ) ) {
  914. continue;
  915. }
  916. $options = $this->get_options( $_post->ID );
  917. // Get the branch name, example: frontend-css-header-external
  918. $tree_branch = $options['side'] . '-' .$options['language'] . '-' . $options['type'] . '-' . $options['linking'];
  919. $filename = $_post->ID . '.' . $options['language'];
  920. if ( $options['linking'] == 'external' ) {
  921. $filename .= '?v=' . rand(1, 10000);
  922. }
  923. // Add the code file to the tree branch
  924. $tree[ $tree_branch ][] = $filename;
  925. }
  926. // Save the tree in the database
  927. update_option( 'custom-css-js-tree', $tree );
  928. }
  929. /**
  930. * Rebuilt the tree when you trash or restore a custom code
  931. */
  932. function trash_post( $post_id ) {
  933. $this->build_search_tree( );
  934. }
  935. /**
  936. * Render the checkboxes, radios, selects and inputs
  937. */
  938. function render_input( $_key, $a, $options ) {
  939. $name = 'custom_code_' . $_key;
  940. $output = '';
  941. // Show radio type options
  942. if ( $a['type'] === 'radio' ) {
  943. $output .= '<div class="radio-group">' . PHP_EOL;
  944. foreach( $a['values'] as $__key => $__value ) {
  945. $selected = '';
  946. $id = $name . '-' . $__key;
  947. $dashicons = isset($__value['dashicon'] ) ? 'dashicons-before dashicons-' . $__value['dashicon'] : '';
  948. if ( isset( $a['disabled'] ) && $a['disabled'] ) {
  949. $selected = ' disabled="disabled"';
  950. }
  951. $selected .= ( $__key == $options[$_key] ) ? ' checked="checked" ' : '';
  952. $output .= '<input type="radio" '. $selected.'value="'.$__key.'" name="'.$name.'" id="'.$id.'">' . PHP_EOL;
  953. $output .= '<label class="'.$dashicons.'" for="'.$id.'"> '.$__value['title'].'</label><br />' . PHP_EOL;
  954. }
  955. $output .= '</div>' . PHP_EOL;
  956. }
  957. // Show checkbox type options
  958. if ( $a['type'] == 'checkbox' ) {
  959. $dashicons = isset($a['dashicon'] ) ? 'dashicons-before dashicons-' . $a['dashicon'] : '';
  960. $selected = ( isset($options[$_key]) && $options[$_key] == '1') ? ' checked="checked" ' : '';
  961. if ( isset( $a['disabled'] ) && $a['disabled'] ) {
  962. $selected .= ' disabled="disabled"';
  963. }
  964. $output .= '<div class="radio-group">' . PHP_EOL;
  965. $output .= '<input type="checkbox" '.$selected.' value="1" name="'.$name.'" id="'.$name.'">' . PHP_EOL;
  966. $output .= '<label class="'.$dashicons.'" for="'.$name.'"> '.$a['title'].'</label>';
  967. $output .= '</div>' . PHP_EOL;
  968. }
  969. // Show select type options
  970. if ( $a['type'] == 'select' ) {
  971. $output .= '<div class="radio-group">' . PHP_EOL;
  972. $output .= '<select name="'.$name.'" id="'.$name.'">' . PHP_EOL;
  973. foreach( $a['values'] as $__key => $__value ) {
  974. $selected = ( isset($options[$_key]) && $options[$_key] == $__key) ? ' selected="selected"' : '';
  975. $output .= '<option value="'.$__key.'"'.$selected.'>' . $__value . '</option>' . PHP_EOL;
  976. }
  977. $output .= '</select>' . PHP_EOL;
  978. $output .= '</div>' . PHP_EOL;
  979. }
  980. return $output;
  981. }
  982. /**
  983. * Get the language for the current post
  984. */
  985. function get_language( $post_id = false ) {
  986. if( $post_id !== false ) {
  987. $options = $this->get_options( $post_id );
  988. $language = $options['language'];
  989. } else {
  990. $language = isset( $_GET['language'] ) ? esc_attr(strtolower($_GET['language'])) : 'css';
  991. }
  992. if ( !in_array($language, array('css', 'js', 'html'))) $language = 'css';
  993. return $language;
  994. }
  995. /**
  996. * Show the activate/deactivate link in the row's action area
  997. */
  998. function post_row_actions($actions, $post) {
  999. if ( 'custom-css-js' !== $post->post_type ) {
  1000. return $actions;
  1001. }
  1002. $url = wp_nonce_url( admin_url( 'admin-ajax.php?action=ccj_active_code&code_id=' . $post->ID), 'ccj-active-code-'. $post->ID );
  1003. if ( $this->is_active( $post->ID) ) {
  1004. $active_title = __('The code is active. Click to deactivate it', 'custom-css-js');
  1005. $active_text = __('Deactivate', 'custom-css-js');
  1006. } else {
  1007. $active_title = __('The code is inactive. Click to activate it', 'custom-css-js');
  1008. $active_text = __('Activate', 'custom-css-js');
  1009. }
  1010. $actions['activate'] = '<a href="' . esc_url( $url ) . '" title="'. $active_title . '" class="ccj_activate_deactivate" data-code-id="'.$post->ID.'">' . $active_text . '</a>';
  1011. return $actions;
  1012. }
  1013. /**
  1014. * Show the activate/deactivate link in admin.
  1015. */
  1016. public function post_submitbox_start() {
  1017. global $post;
  1018. if ( ! is_object( $post ) ) return;
  1019. if ( 'custom-css-js' !== $post->post_type ) return;
  1020. if ( !isset( $_GET['post'] ) ) return;
  1021. $url = wp_nonce_url( admin_url( 'admin-ajax.php?action=ccj_active_code&code_id=' . $post->ID), 'ccj-active-code-'. $post->ID );
  1022. if ( $this->is_active( $post->ID) ) {
  1023. $text = __('Active', 'custom-css-js');
  1024. $action = __('Deactivate', 'custom-css-js');
  1025. } else {
  1026. $text = __('Inactive', 'custom-css-js');
  1027. $action = __('Activate', 'custom-css-js');
  1028. }
  1029. ?>
  1030. <div id="activate-action"><span style="font-weight: bold;"><?php echo $text; ?></span>
  1031. (<a class="ccj_activate_deactivate" data-code-id="<?php echo $post->ID; ?>" href="<?php echo esc_url( $url ); ?>"><?php echo $action ?></a>)
  1032. </div>
  1033. <?php
  1034. }
  1035. /**
  1036. * Show the Permalink edit form
  1037. */
  1038. function edit_form_before_permalink($filename = '', $permalink = '', $filetype = 'css') {
  1039. if ( isset($_GET['language'] ) ) $filetype = $_GET['language'];
  1040. if ( !is_string($filename) ) {
  1041. global $post;
  1042. if ( ! is_object( $post ) ) return;
  1043. if ( 'custom-css-js' !== $post->post_type ) return;
  1044. $post = $filename;
  1045. $slug = get_post_meta( $post->ID, '_slug', true );
  1046. $options = get_post_meta( $post->ID, 'options', true );
  1047. if ( isset($options['language'] ) ) $filetype = $options['language'];
  1048. if ( ! @file_exists( CCJ_UPLOAD_DIR . '/' . $slug . '.' . $options['language'] ) ) {
  1049. $slug = false;
  1050. }
  1051. $filename = ( $slug ) ? $slug : $post->ID;
  1052. }
  1053. if ( ! in_array( $filetype, array( 'css', 'js' ) ) ) return;
  1054. if ( empty( $permalink ) ) {
  1055. $permalink = CCJ_UPLOAD_URL. '/' . $filename . '.' . $filetype;
  1056. }
  1057. ?>
  1058. <div class="inside">
  1059. <div id="edit-slug-box" class="hide-if-no-js">
  1060. <strong>Permalink:</strong>
  1061. <span id="sample-permalink"><a href="<?php echo esc_url($permalink); ?>"><?php echo esc_html(CCJ_UPLOAD_URL ) . '/'; ?><span id="editable-post-name"><?php echo esc_html( $filename ); ?></span>.<?php echo esc_html($filetype); ?></a></span>
  1062. &lrm;<span id="ccj-edit-slug-buttons"><button type="button" class="ccj-edit-slug button button-small hide-if-no-js" aria-label="Edit permalink">Edit</button></span>
  1063. <span id="editable-post-name-full"><?php echo esc_html($filename); ?></span>
  1064. </div>
  1065. <?php wp_nonce_field( 'ccj-permalink', 'ccj-permalink-nonce' ); ?>
  1066. </div>
  1067. <?php
  1068. }
  1069. /**
  1070. * AJAX save the Permalink slug
  1071. */
  1072. function wp_ajax_ccj_permalink() {
  1073. if ( ! isset( $_POST['ccj_permalink_nonce'] ) ) return;
  1074. if ( ! wp_verify_nonce( $_POST['ccj_permalink_nonce'], 'ccj-permalink') ) return;
  1075. $code_id = isset($_POST['code_id'])? intval($_POST['code_id']) : 0;
  1076. $permalink = isset($_POST['permalink'])? $_POST['permalink'] : null;
  1077. $slug = isset($_POST['new_slug'])? trim(sanitize_file_name($_POST['new_slug'])) : null;
  1078. $filetype = isset($_POST['filetype'])? $_POST['filetype'] : 'css';
  1079. if ( empty($slug ) ) {
  1080. $slug = (string)$code_id;
  1081. } else {
  1082. update_post_meta( $code_id, '_slug', $slug);
  1083. }
  1084. $this->edit_form_before_permalink( $slug, $permalink, $filetype );
  1085. wp_die();
  1086. }
  1087. /**
  1088. * Remove the JS/CSS file from the disk when deleting the post
  1089. */
  1090. function before_delete_post( $postid ) {
  1091. global $post;
  1092. if ( ! is_object( $post ) ) return;
  1093. if ( 'custom-css-js' !== $post->post_type ) return;
  1094. if ( ! wp_is_writable( CCJ_UPLOAD_DIR ) ) return;
  1095. $options = get_post_meta( $postid, 'options', true );
  1096. $slug = get_post_meta( $postid, '_slug', true );
  1097. $file_name = $postid . '.' . $options['language'];
  1098. @unlink( CCJ_UPLOAD_DIR . '/' . $file_name );
  1099. if ( !empty( $slug ) ) {
  1100. @unlink( CCJ_UPLOAD_DIR . '/' . $slug . '.' . $options['language'] );
  1101. }
  1102. }
  1103. /**
  1104. * Fix for bug: white page Edit Custom Code for WordPress 5.0 with Classic Editor
  1105. */
  1106. function current_screen_2() {
  1107. $screen = get_current_screen();
  1108. if ( $screen->post_type != 'custom-css-js' )
  1109. return false;
  1110. remove_filter( 'use_block_editor_for_post', array( 'Classic_Editor', 'choose_editor' ), 100, 2 );
  1111. add_filter( 'use_block_editor_for_post', '__return_false', 100 );
  1112. add_filter( 'use_block_editor_for_post_type', '__return_false', 100 );
  1113. }
  1114. }
  1115. return new CustomCSSandJS_Admin();