| 1 |
<?php |
| 2 |
/** |
| 3 |
* Warning notices |
| 4 |
* |
| 5 |
* @author Webcraftic <wordpress.webraftic@gmail.com> |
| 6 |
* @copyright (c) 11.12.2018, Webcraftic |
| 7 |
* @version 1.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class WINP_WarningNotices { |
| 16 |
|
| 17 |
/** |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
private $prefix = 'inp_'; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
private $notices = []; |
| 26 |
|
| 27 |
public function __construct() { |
| 28 |
/** |
| 29 |
* @since 2.2.8 Filter Name Changed |
| 30 |
*/ |
| 31 |
add_filter( 'wbcr/factory/admin_notices', [ $this, 'register_notices' ], 10, 2 ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 36 |
* |
| 37 |
* @param array $notices |
| 38 |
* @param string $plugin_name |
| 39 |
* |
| 40 |
* @return array |
| 41 |
* @throws \Exception |
| 42 |
*/ |
| 43 |
public function register_notices( $notices, $plugin_name ) { |
| 44 |
if ( $plugin_name !== WINP_Plugin::app()->getPluginName() ) { |
| 45 |
return $notices; |
| 46 |
} |
| 47 |
|
| 48 |
if ( ! WINP_Plugin::app()->currentUserCan() ) { |
| 49 |
return $notices; |
| 50 |
} |
| 51 |
|
| 52 |
if ( WINP_Plugin::app()->getOption( 'need_show_attention_notice' ) ) { |
| 53 |
$this->add_notice( 'upgrade_plugin2', $this->get_upgrade_notice(), 'warning' ); |
| 54 |
} |
| 55 |
|
| 56 |
$this->add_notice( 'safe_mode', $this->get_safe_mode_notice(), 'success', false ); |
| 57 |
$this->add_notice( 'result_error', $this->get_throw_php_error_notice(), 'error', false, 0, [ |
| 58 |
'post', |
| 59 |
'post-new', |
| 60 |
'edit' |
| 61 |
] ); |
| 62 |
/*$this->add_notice( 'leave_feedback', $this->get_leave_feedback_notice(), 'success', true, time() + 3600 * 60, [ |
| 63 |
'post', |
| 64 |
'post-new', |
| 65 |
'edit' |
| 66 |
] );*/ |
| 67 |
|
| 68 |
return array_merge( $notices, $this->notices ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* A small line that asks for feedback from user |
| 73 |
* |
| 74 |
* @return string |
| 75 |
*/ |
| 76 |
public function get_leave_feedback_notice() { |
| 77 |
$post_type = WINP_Plugin::app()->request->get( 'post_type' ); |
| 78 |
if ( ! empty( $post_type ) && WINP_SNIPPETS_POST_TYPE == $post_type ) { |
| 79 |
return '<strong>' . __( 'Have feedback on Woody ad Snippets?', 'insert-php' ) . '</strong> ' . __( 'Please take the time to answer a short survey on how you use this plugin and what you\'d like to see changed or added in the future.', 'insert-php' ) . ' |
| 80 |
<a href="http://bit.ly/2MpVokA" class="button secondary" target="_blank" style="margin: auto .5em;">' . __( 'Take the survey now', 'insert-php' ) . '</a>'; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Show error notification after saving snippet. We can also show this message when the snippet is activated. |
| 86 |
* We must warn the user that we can not perform the spippet due to an error. |
| 87 |
* |
| 88 |
* @return string|null |
| 89 |
*/ |
| 90 |
public function get_throw_php_error_notice() { |
| 91 |
$save_snippet_result = WINP_Plugin::app()->request->get( 'wbcr_inp_save_snippet_result' ); |
| 92 |
$post_id = WINP_Plugin::app()->request->get( 'post' ); |
| 93 |
|
| 94 |
if ( ! empty( $save_snippet_result ) && 'code-error' == $save_snippet_result ) { |
| 95 |
$post_id = ! empty( $post_id ) ? intval( $post_id ) : null; |
| 96 |
|
| 97 |
if ( $post_id ) { |
| 98 |
$error = WINP_Plugin::app()->getExecuteObject()->getSnippetError( $post_id ); |
| 99 |
|
| 100 |
if ( false !== $error ) { |
| 101 |
return sprintf( '<p>%s</p><p><strong>%s</strong></p>', sprintf( __( 'The snippet has been deactivated due to an error on line %d:', 'insert-php' ), $error['line'] ), $error['message'] ); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return null; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* This warning is for users of the old version 1.3.0. The plugin has changed the way it works |
| 111 |
* and requires user actions to continue working with plugin. |
| 112 |
* |
| 113 |
* @return string |
| 114 |
*/ |
| 115 |
public function get_upgrade_notice() { |
| 116 |
$create_notice_url = admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ); |
| 117 |
|
| 118 |
$notice = '<b>' . WINP_Plugin::app()->getPluginTitle() . '</b>: ' . sprintf( __( 'Attention! If you have previously used version 1.3.0 of plugin Insert php. This new %s 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 until March 2019, after that we will stop supporting shortcodes [insert_php].', 'insert-php' ), WINP_Plugin::app()->getPluginVersion() ); |
| 119 |
|
| 120 |
$notice .= '<br><br>' . __( 'We strongly recommend you to porting your php code to snippets and call them in your posts/pages and widgets using [wbcr_php_snippet id = "000"] shortcodes.', 'insert-php' ); |
| 121 |
$notice .= ' ' . sprintf( __( 'For more information on porting code and using snippets, see our plugin <a href="%s" target="_blank">documentation</a>', 'insert-php' ), WINP_Plugin::app()->get_support()->get_docs_url( true, 'admin-notice' ) ); |
| 122 |
$notice .= '<br><br><a href="' . $create_notice_url . '" class="button button-default">' . __( 'Create new php snippet', 'insert-php' ) . '</a> '; |
| 123 |
$notice .= '<a href="https://downloads.wordpress.org/plugin/insert-php.1.3.zip" class="button button-default">' . __( 'Download old version', 'insert-php' ) . '</a>'; |
| 124 |
$notice .= '<br><br>' . sprintf( __( 'If you still want to use the old shortcodes [insert_php] and you don’t have time to upgrade to the new version, you can enable support for old shortcodes in the plugin <a href="%s">settings</a>.', 'insert-php' ), WINP_Plugin::app()->getPluginPageUrl( 'settings' ) ); |
| 125 |
$notice .= '<br>' . 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' ), 'http://forum.webcraftic.com' ); |
| 126 |
|
| 127 |
return $notice; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* When the safe mode of the plugin is enabled, this notification will remind |
| 132 |
* the user to exit safe mode so that the user's snippets are available publicly. |
| 133 |
* |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
public function get_safe_mode_notice() { |
| 137 |
if ( ! WINP_Helper::is_safe_mode() ) { |
| 138 |
return null; |
| 139 |
} |
| 140 |
|
| 141 |
$disable_safe_mode_url = add_query_arg( [ 'wbcr-php-snippets-disable-safe-mode' => 1 ] ); |
| 142 |
|
| 143 |
$notice = WINP_Plugin::app()->getPluginTitle() . ': ' . __( 'Running in safe mode. This mode your snippets will not be started.', 'insert-php' ); |
| 144 |
$notice .= ' <a href="' . $disable_safe_mode_url . '" class="button button-default">' . __( 'Disable Safe Mode', 'insert-php' ) . '</a>'; |
| 145 |
|
| 146 |
return $notice; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 151 |
* |
| 152 |
* @param string $id |
| 153 |
* @param string $message |
| 154 |
* @param string $type |
| 155 |
* @param bool $dismissible |
| 156 |
* @param int $dismiss_expires |
| 157 |
* @param array $where |
| 158 |
*/ |
| 159 |
protected function add_notice( $id, $message, $type = 'warning', $dismissible = true, $dismiss_expires = 0, $where = [] ) { |
| 160 |
if ( is_null( $message ) ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
if ( ! empty( $this->notices ) ) { |
| 165 |
foreach ( $this->notices as $notice ) { |
| 166 |
if ( $notice['id'] == $this->prefix . $id ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
$this->notices[] = [ |
| 173 |
'id' => $this->prefix . $id, |
| 174 |
'type' => $type, |
| 175 |
'dismissible' => (bool) $dismissible, |
| 176 |
'dismiss_expires' => (int) $dismiss_expires, |
| 177 |
'where' => $where, |
| 178 |
'text' => '<p>' . $message . '</p>' |
| 179 |
]; |
| 180 |
} |
| 181 |
} |