| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin boot |
| 4 |
* @author Alex Kovalev <alex.kovalevv@gmail.com> |
| 5 |
* @copyright Alex Kovalev 05.06.2018 |
| 6 |
* @version 1.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Admin warning notices |
| 11 |
*/ |
| 12 |
function wbcr_inp_admin_conflict_notices_error($notices, $plugin_name) |
| 13 |
{ |
| 14 |
if( $plugin_name != WINP_Plugin::app()->getPluginName() ) { |
| 15 |
return $notices; |
| 16 |
} |
| 17 |
|
| 18 |
if( isset($_COOKIE['wbcr-php-snippets-safe-mode']) ) { |
| 19 |
$disable_safe_mode_url = add_query_arg(array('wbcr-php-snippets-disable-safe-mode' => 1)); |
| 20 |
|
| 21 |
$safe_mode_notice = WINP_Plugin::app() |
| 22 |
->getPluginTitle() . ': ' . __('Running in safe mode. This mode your snippets will not be started.', 'insert-php'); |
| 23 |
$safe_mode_notice .= ' <a href="' . $disable_safe_mode_url . '" class="button button-default">Disable Safe Mode</a>'; |
| 24 |
|
| 25 |
$notices[] = array( |
| 26 |
'id' => 'inp_safe_mode', |
| 27 |
'type' => 'success', |
| 28 |
'dismissible' => false, |
| 29 |
'dismiss_expires' => 0, |
| 30 |
'text' => '<p>' . $safe_mode_notice . '</p>' |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
$create_notice_url = admin_url('edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE); |
| 35 |
|
| 36 |
$upgrade_plugin_notice = '<b>' . WINP_Plugin::app() |
| 37 |
->getPluginTitle() . '</b>: ' . __('Attention! This new 2.0 plugin version, we added the ability to insert php code using snippets. This is a more convenient and secure way than using shortcodes [insert_php] code execute [/ insert_php]. However, for compatibility reasons, we left support for [insert_php] shortcodes, but we will depreciate them in the next versions of the plugin.', 'insert-php'); |
| 38 |
|
| 39 |
$upgrade_plugin_notice .= '<br><br>' . __('We strongly recommend you to transfer your php code to snippets and call them in your posts/pages and widgets using [wbcr_php_snippet id = "000"] shortcodes.', 'insert-php'); |
| 40 |
|
| 41 |
//$upgrade_plugin_notice .= '<br><br><span style="color:red">' . __('If you have updated from version 1.3 of the plugin (Insert php). Please deactivate and activate the plugin to update the settings.', 'insert-php') . '</span>'; |
| 42 |
|
| 43 |
$upgrade_plugin_notice .= '<br><br><a href="' . $create_notice_url . '" class="button button-default">' . __('Create new php snippet', 'insert-php') . '</a> '; |
| 44 |
|
| 45 |
$upgrade_plugin_notice .= '<a href="https://downloads.wordpress.org/plugin/insert-php.1.3.zip" class="button button-default">' . __('Download old version', 'insert-php') . '</a><br><br>'; |
| 46 |
$upgrade_plugin_notice .= sprintf(__('If you have issues with the plugin new version or any suggestions, please contact us on <a href="%s" target="_blank">our forum</a>.', 'insert-php'), 'https://wordpress.org/support/plugin/insert-php'); |
| 47 |
$upgrade_plugin_notice .= '<br>' . sprintf(__('We tried to make the plugin better and more convenient. If you like the new version, feel free to <a href="%s" target="_blank">leave your feedback</a>. It will give us motivation for further improvements.', 'insert-php'), 'https://wordpress.org/support/plugin/insert-php/reviews/#new-post'); |
| 48 |
|
| 49 |
$notices[] = array( |
| 50 |
'id' => 'inp_upgrade_plugin', |
| 51 |
'type' => 'warning', |
| 52 |
'dismissible' => true, |
| 53 |
'dismiss_expires' => 0, |
| 54 |
'text' => '<p>' . $upgrade_plugin_notice . '</p>' |
| 55 |
); |
| 56 |
|
| 57 |
return $notices; |
| 58 |
} |
| 59 |
|
| 60 |
add_filter('wbcr_factory_admin_notices', 'wbcr_inp_admin_conflict_notices_error', 10, 2); |
| 61 |
|
| 62 |
function wbcr_inp_admin_init() |
| 63 |
{ |
| 64 |
$plugin = WINP_Plugin::app(); |
| 65 |
|
| 66 |
// Disable safe mode |
| 67 |
|
| 68 |
if( isset($_COOKIE['wbcr-php-snippets-safe-mode']) && isset($_REQUEST['wbcr-php-snippets-disable-safe-mode']) && $plugin->currentUserCan() ) { |
| 69 |
unset($_COOKIE['wbcr-php-snippets-safe-mode']); |
| 70 |
setcookie('wbcr-php-snippets-safe-mode', null, -1); |
| 71 |
wp_safe_redirect(remove_query_arg(array('wbcr-php-snippets-disable-safe-mode'))); |
| 72 |
exit; |
| 73 |
} |
| 74 |
|
| 75 |
// Register metaboxes |
| 76 |
|
| 77 |
require_once(WINP_PLUGIN_DIR . '/admin/metaboxes/base-options.php'); |
| 78 |
Wbcr_FactoryMetaboxes400::registerFor(new WINP_BaseOptionsMetaBox($plugin), WINP_SNIPPETS_POST_TYPE, $plugin); |
| 79 |
|
| 80 |
// Upgrade up to new version |
| 81 |
if( !$plugin->getOption('upgrade_up_to_201', false) ) { |
| 82 |
$role = get_role('administrator'); |
| 83 |
if( !$role ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$role->add_cap('edit_' . WINP_SNIPPETS_POST_TYPE); |
| 88 |
$role->add_cap('read_' . WINP_SNIPPETS_POST_TYPE); |
| 89 |
$role->add_cap('delete_' . WINP_SNIPPETS_POST_TYPE); |
| 90 |
$role->add_cap('edit_' . WINP_SNIPPETS_POST_TYPE . 's'); |
| 91 |
$role->add_cap('edit_others_' . WINP_SNIPPETS_POST_TYPE . 's'); |
| 92 |
$role->add_cap('publish_' . WINP_SNIPPETS_POST_TYPE . 's'); |
| 93 |
$role->add_cap('read_private_' . WINP_SNIPPETS_POST_TYPE . 's'); |
| 94 |
|
| 95 |
$plugin->updateOption('upgrade_up_to_201', 1); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
add_action('admin_init', 'wbcr_inp_admin_init'); |
| 100 |
|
| 101 |
// --- |
| 102 |
// Editor |
| 103 |
// |
| 104 |
|
| 105 |
/** |
| 106 |
* Asset scripts for the tinymce editor |
| 107 |
* |
| 108 |
* @param string $hook |
| 109 |
*/ |
| 110 |
function wbcr_inp_enqueue_tinymce_assets($hook) |
| 111 |
{ |
| 112 |
$pages = array( |
| 113 |
'post.php', |
| 114 |
'post-new.php', |
| 115 |
'widgets.php' |
| 116 |
); |
| 117 |
|
| 118 |
if( !in_array($hook, $pages) || !current_user_can('manage_options') ) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
wp_enqueue_script('wbcr-inp-tinymce-button-widget', WINP_PLUGIN_URL . '/admin/assets/js/tinymce4.4.js', array('jquery'), WINP_Plugin::app() |
| 123 |
->getPluginVersion(), true); |
| 124 |
} |
| 125 |
|
| 126 |
add_action('admin_enqueue_scripts', 'wbcr_inp_enqueue_tinymce_assets'); |
| 127 |
|
| 128 |
/** |
| 129 |
* Adds js variable required for shortcodes. |
| 130 |
* |
| 131 |
* @see before_wp_tiny_mce |
| 132 |
* @since 1.1.0 |
| 133 |
*/ |
| 134 |
function wbcr_inp_tinymce_data($hook) |
| 135 |
{ |
| 136 |
if( !current_user_can('manage_options') ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
// styles for the plugin shorcodes |
| 141 |
$shortcode_icon = WINP_PLUGIN_URL . '/admin/assets/img/shortcode-icon.png'; |
| 142 |
$shortcode_title = __('PHP snippets', 'insert-php'); |
| 143 |
|
| 144 |
$snippets = get_posts(array( |
| 145 |
'post_type' => WINP_SNIPPETS_POST_TYPE, |
| 146 |
'meta_key' => WINP_Plugin::app()->getPrefix() . 'snippet_scope', |
| 147 |
'meta_value' => 'shortcode', |
| 148 |
'post_status' => 'publish', |
| 149 |
'numberposts' => -1 |
| 150 |
)); |
| 151 |
|
| 152 |
$result = array(); |
| 153 |
foreach((array)$snippets as $snippet) { |
| 154 |
$result[] = array( |
| 155 |
'id' => $snippet->ID, |
| 156 |
'title' => empty($snippet->post_title) |
| 157 |
? '(no titled, ID=' . $snippet->ID . ')' |
| 158 |
: $snippet->post_title |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
$shortcode_snippets_json = json_encode($result); |
| 163 |
|
| 164 |
?> |
| 165 |
<!-- <?= WINP_Plugin::app()->getPluginTitle() ?> for tinymce --> |
| 166 |
<style> |
| 167 |
i.wbcr-inp-shortcode-icon { |
| 168 |
background: url("<?php echo $shortcode_icon ?>") center no-repeat; |
| 169 |
} |
| 170 |
</style> |
| 171 |
<script> |
| 172 |
var wbcr_inp_tinymce_snippets_button_title = '<?php echo $shortcode_title ?>'; |
| 173 |
var wbcr_inp_post_tinymce_nonce = '<?php echo wp_create_nonce('wbcr_inp_tinymce_post_nonce') ?>'; |
| 174 |
var wbcr_inp_shortcode_snippets = <?= $shortcode_snippets_json ?>; |
| 175 |
</script> |
| 176 |
<!-- /end <?= WINP_Plugin::app()->getPluginTitle() ?> for tinymce --> |
| 177 |
<?php |
| 178 |
} |
| 179 |
|
| 180 |
add_action('admin_print_scripts-post.php', 'wbcr_inp_tinymce_data'); |
| 181 |
add_action('admin_print_scripts-post-new.php', 'wbcr_inp_tinymce_data'); |
| 182 |
add_action('admin_print_scripts-widgets.php', 'wbcr_inp_tinymce_data'); |
| 183 |
|