| 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 |
* Enables/Disable safe mode, in which the php code will not be executed. |
| 11 |
*/ |
| 12 |
function wbcr_inp_safe_mode() { |
| 13 |
if ( isset( $_GET['wbcr-php-snippets-safe-mode'] ) ) { |
| 14 |
WINP_Helper::enable_safe_mode(); |
| 15 |
} |
| 16 |
if ( isset( $_GET['wbcr-php-snippets-disable-safe-mode'] ) ) { |
| 17 |
WINP_Helper::disable_safe_mode(); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
add_action( 'plugins_loaded', 'wbcr_inp_safe_mode', - 1 ); |
| 22 |
|
| 23 |
function wbcr_inp_admin_init() { |
| 24 |
|
| 25 |
$plugin = WINP_Plugin::app(); |
| 26 |
|
| 27 |
// Register metaboxes |
| 28 |
require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/base-options.php' ); |
| 29 |
Wbcr_FactoryMetaboxes404::registerFor( new WINP_BaseOptionsMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin ); |
| 30 |
|
| 31 |
if ( current_user_can( 'install_plugins' ) && ! is_plugin_active( 'robin-image-optimizer/robin-image-optimizer.php' ) ) { |
| 32 |
require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/info.php' ); |
| 33 |
Wbcr_FactoryMetaboxes404::registerFor( new WINP_InfoMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin ); |
| 34 |
} |
| 35 |
|
| 36 |
$snippet_type = WINP_Helper::get_snippet_type(); |
| 37 |
|
| 38 |
if ( $snippet_type !== WINP_SNIPPET_TYPE_PHP ) { |
| 39 |
require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/view-options.php' ); |
| 40 |
Wbcr_FactoryMetaboxes404::registerFor( new WINP_ViewOptionsMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin ); |
| 41 |
} |
| 42 |
|
| 43 |
// Upgrade up to new version |
| 44 |
$first_activation = get_option( $plugin->getOptionName( 'first_activation' ) ); |
| 45 |
$is_upgraded = get_option( $plugin->getOptionName( 'upgrade_up_to_201' ) ); |
| 46 |
|
| 47 |
if ( ! $first_activation && ! $is_upgraded ) { |
| 48 |
$role = get_role( 'administrator' ); |
| 49 |
|
| 50 |
if ( ! $role ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
$role->add_cap( 'edit_' . WINP_SNIPPETS_POST_TYPE ); |
| 55 |
$role->add_cap( 'read_' . WINP_SNIPPETS_POST_TYPE ); |
| 56 |
$role->add_cap( 'delete_' . WINP_SNIPPETS_POST_TYPE ); |
| 57 |
$role->add_cap( 'edit_' . WINP_SNIPPETS_POST_TYPE . 's' ); |
| 58 |
$role->add_cap( 'edit_others_' . WINP_SNIPPETS_POST_TYPE . 's' ); |
| 59 |
$role->add_cap( 'publish_' . WINP_SNIPPETS_POST_TYPE . 's' ); |
| 60 |
$role->add_cap( 'read_private_' . WINP_SNIPPETS_POST_TYPE . 's' ); |
| 61 |
|
| 62 |
update_option( $plugin->getOptionName( 'upgrade_up_to_201' ), 1 ); |
| 63 |
update_option( $plugin->getOptionName( 'what_new_210' ), 1 ); |
| 64 |
|
| 65 |
// Create a demo snippets with examples of use |
| 66 |
if ( ! get_option( $plugin->getOptionName( 'demo_snippets_created' ) ) ) { |
| 67 |
WINP_Helper::create_demo_snippets(); |
| 68 |
} |
| 69 |
|
| 70 |
// Write information about the first activation of plugin |
| 71 |
if ( ! $first_activation ) { |
| 72 |
update_option( $plugin->getOptionName( 'first_activation' ), array( |
| 73 |
'version' => $plugin->getPluginVersion(), |
| 74 |
'timestamp' => time() |
| 75 |
) ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// If the user has updated the plugin or activated it for the first time, |
| 80 |
// you need to show the page "What's new?" |
| 81 |
|
| 82 |
if ( ! isset( $_GET['wbcr_inp_about_page_viewed'] ) ) { |
| 83 |
if ( WINP_Helper::is_need_show_about_page() ) { |
| 84 |
try { |
| 85 |
$redirect_url = WINP_Plugin::app()->getPluginPageUrl( 'about', array( 'wbcr_inp_about_page_viewed' => 1 ) ); |
| 86 |
wp_safe_redirect( $redirect_url ); |
| 87 |
die(); |
| 88 |
} catch( Exception $e ) { |
| 89 |
} |
| 90 |
} |
| 91 |
} else { |
| 92 |
if ( WINP_Helper::is_need_show_about_page() ) { |
| 93 |
delete_option( $plugin->getOptionName( 'what_new_210' ) ); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
add_action( 'admin_init', 'wbcr_inp_admin_init' ); |
| 99 |
|
| 100 |
// --- |
| 101 |
// Editor |
| 102 |
// |
| 103 |
|
| 104 |
/** |
| 105 |
* Enqueue scripts |
| 106 |
*/ |
| 107 |
function wbcr_inp_enqueue_scripts() { |
| 108 |
global $pagenow; |
| 109 |
|
| 110 |
$screen = get_current_screen(); |
| 111 |
|
| 112 |
if ( ( 'post-new.php' == $pagenow || 'post.php' == $pagenow ) && WINP_SNIPPETS_POST_TYPE == $screen->post_type ) { |
| 113 |
wp_enqueue_script( 'wbcr-inp-admin-scripts', WINP_PLUGIN_URL . '/admin/assets/js/scripts.js', array( |
| 114 |
'jquery', |
| 115 |
'jquery-ui-tooltip' |
| 116 |
), WINP_Plugin::app()->getPluginVersion() ); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Asset scripts for the tinymce editor |
| 122 |
* |
| 123 |
* @param string $hook |
| 124 |
*/ |
| 125 |
function wbcr_inp_enqueue_tinymce_assets( $hook ) { |
| 126 |
$pages = array( |
| 127 |
'post.php', |
| 128 |
'post-new.php', |
| 129 |
'widgets.php' |
| 130 |
); |
| 131 |
|
| 132 |
if ( ! in_array( $hook, $pages ) || ! current_user_can( 'manage_options' ) ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
wp_enqueue_script( 'wbcr-inp-tinymce-button-widget', WINP_PLUGIN_URL . '/admin/assets/js/tinymce4.4.js', array( 'jquery' ), WINP_Plugin::app()->getPluginVersion(), true ); |
| 137 |
} |
| 138 |
|
| 139 |
add_action( 'admin_enqueue_scripts', 'wbcr_inp_enqueue_tinymce_assets' ); |
| 140 |
add_action( 'admin_enqueue_scripts', 'wbcr_inp_enqueue_scripts' ); |
| 141 |
|
| 142 |
/** |
| 143 |
* Adds js variable required for shortcodes. |
| 144 |
* |
| 145 |
* @see before_wp_tiny_mce |
| 146 |
* @since 1.1.0 |
| 147 |
*/ |
| 148 |
function wbcr_inp_tinymce_data( $hook ) { |
| 149 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
// styles for the plugin shorcodes |
| 154 |
$shortcode_icon = WINP_PLUGIN_URL . '/admin/assets/img/shortcode-icon5.png'; |
| 155 |
$shortcode_title = __( 'Woody ad snippets', 'insert-php' ); |
| 156 |
|
| 157 |
$result = WINP_Helper::get_shortcode_data(); |
| 158 |
$shortcode_snippets_json = json_encode( $result ); |
| 159 |
|
| 160 |
?> |
| 161 |
<!-- <?= WINP_Plugin::app()->getPluginTitle() ?> for tinymce --> |
| 162 |
<style> |
| 163 |
i.wbcr-inp-shortcode-icon { |
| 164 |
background: url("<?php echo $shortcode_icon ?>") center no-repeat; |
| 165 |
} |
| 166 |
</style> |
| 167 |
<script> |
| 168 |
var wbcr_inp_tinymce_snippets_button_title = '<?php echo $shortcode_title ?>'; |
| 169 |
var wbcr_inp_post_tinymce_nonce = '<?php echo wp_create_nonce( 'wbcr_inp_tinymce_post_nonce' ) ?>'; |
| 170 |
var wbcr_inp_shortcode_snippets = <?= $shortcode_snippets_json ?>; |
| 171 |
</script> |
| 172 |
<!-- /end <?= WINP_Plugin::app()->getPluginTitle() ?> for tinymce --> |
| 173 |
<?php |
| 174 |
} |
| 175 |
|
| 176 |
add_action( 'admin_print_scripts-post.php', 'wbcr_inp_tinymce_data' ); |
| 177 |
add_action( 'admin_print_scripts-post-new.php', 'wbcr_inp_tinymce_data' ); |
| 178 |
add_action( 'admin_print_scripts-widgets.php', 'wbcr_inp_tinymce_data' ); |
| 179 |
|
| 180 |
/** |
| 181 |
* Deactivate snippet on trashed |
| 182 |
* |
| 183 |
* @param $post_id |
| 184 |
* |
| 185 |
* @since 2.0.6 |
| 186 |
*/ |
| 187 |
function wbcr_inp_trash_post( $post_id ) { |
| 188 |
$post_type = get_post_type( $post_id ); |
| 189 |
if ( $post_type == WINP_SNIPPETS_POST_TYPE ) { |
| 190 |
WINP_Helper::updateMetaOption( $post_id, 'snippet_activate', 0 ); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
add_action( 'wp_trash_post', 'wbcr_inp_trash_post' ); |
| 195 |
|
| 196 |
/** |
| 197 |
* Removes the default 'new item' from the admin menu to add own page 'new item' later. |
| 198 |
* |
| 199 |
* @see menu_order |
| 200 |
* |
| 201 |
* @param $menu |
| 202 |
* |
| 203 |
* @return mixed |
| 204 |
*/ |
| 205 |
function wbcr_inp_remove_new_item( $menu ) { |
| 206 |
global $submenu; |
| 207 |
|
| 208 |
if ( ! isset( $submenu[ 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ] ) ) { |
| 209 |
return $menu; |
| 210 |
} |
| 211 |
unset( $submenu[ 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ][10] ); |
| 212 |
|
| 213 |
return $menu; |
| 214 |
} |
| 215 |
|
| 216 |
add_filter( 'custom_menu_order', '__return_true' ); |
| 217 |
add_filter( 'menu_order', 'wbcr_inp_remove_new_item' ); |
| 218 |
|
| 219 |
/** |
| 220 |
* If the user tried to get access to the default 'new item', |
| 221 |
* redirects forcibly to our page 'new item'. |
| 222 |
* |
| 223 |
* @see current_screen |
| 224 |
*/ |
| 225 |
function wbcr_inp_redirect_to_new_item() { |
| 226 |
$screen = get_current_screen(); |
| 227 |
|
| 228 |
if ( empty( $screen ) ) { |
| 229 |
return; |
| 230 |
} |
| 231 |
if ( 'add' !== $screen->action || 'post' !== $screen->base || WINP_SNIPPETS_POST_TYPE !== $screen->post_type ) { |
| 232 |
return; |
| 233 |
} |
| 234 |
if ( isset( $_GET['winp_item'] ) ) { |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
$url = admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&page=new-item-' . WINP_Plugin::app()->getPluginName() ); |
| 239 |
|
| 240 |
wp_safe_redirect( $url ); |
| 241 |
|
| 242 |
exit; |
| 243 |
} |
| 244 |
|
| 245 |
add_action( 'current_screen', 'wbcr_inp_redirect_to_new_item' ); |