class-base.php
1 year ago
class-dashboard-widget.php
1 year ago
class-rest-request.php
1 year ago
index.php
3 years ago
class-base.php
325 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WBCR\Factory_Adverts_159; |
| 4 | |
| 5 | // Exit if accessed directly |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | exit; |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * Base class for adverts module. |
| 12 | * |
| 13 | * Contains methods for retrieving banner data for a specific position. |
| 14 | * With this class user cat get advert content for a specific position. |
| 15 | * This class use functional design pattern. |
| 16 | * |
| 17 | * @author Alexander Vitkalov <nechin.va@gmail.com> |
| 18 | * @author Alexander Kovalev <alex.kovalevv@gmail.com>, Github: https://github.com/alexkovalevv |
| 19 | * |
| 20 | * @since 1.0.0 Added |
| 21 | * @package factory-adverts |
| 22 | * @copyright (c) 2019 Webcraftic Ltd |
| 23 | */ |
| 24 | class Base { |
| 25 | |
| 26 | /** |
| 27 | * Экзепляр плагина с которым взаимодействует этот модуль |
| 28 | * |
| 29 | * @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 30 | * @since 1.0.1 |
| 31 | * @var \Wbcr_Factory480_Plugin |
| 32 | */ |
| 33 | private $plugin; |
| 34 | |
| 35 | /* |
| 36 | * Contain array data with the plugin information and the module settings. |
| 37 | * Mainly used to get the name of the plugin and how to get the adverts blocks. |
| 38 | * |
| 39 | * @since 1.0.0 Added |
| 40 | * |
| 41 | * @var array Example: array( |
| 42 | * 'dashboard_widget' => true, |
| 43 | * 'right_sidebar' => true, |
| 44 | * 'notice' => true, |
| 45 | * ... |
| 46 | * ) |
| 47 | * |
| 48 | */ |
| 49 | private $settings = []; |
| 50 | |
| 51 | /** |
| 52 | * Экземпляр класса для работы API CreativeMotion |
| 53 | * |
| 54 | * @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 55 | * @since 1.0.1 |
| 56 | * @var \WBCR\Factory_Adverts_159\Creative_Motion_API |
| 57 | */ |
| 58 | private $api; |
| 59 | |
| 60 | /** |
| 61 | * Со� |
| 62 | раняем уже полученные данные, для объектного кеширования |
| 63 | * |
| 64 | * @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 65 | * @since 1.0.1 |
| 66 | * @var array |
| 67 | */ |
| 68 | private $placements = []; |
| 69 | |
| 70 | /** |
| 71 | * @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 72 | * @since 1.0.1 |
| 73 | * @var array |
| 74 | */ |
| 75 | private $errors = []; |
| 76 | |
| 77 | /** |
| 78 | * Wbcr_Factory_Adinserter constructor. |
| 79 | * |
| 80 | * - Store plugin information and settings. |
| 81 | * - Add filter and actions. |
| 82 | * - Include dashboard widget. |
| 83 | * |
| 84 | * @since 1.0.0 Added |
| 85 | * |
| 86 | * @param \Wbcr_Factory480_Plugin $plugin |
| 87 | */ |
| 88 | public function __construct( \Wbcr_Factory480_Plugin $plugin, $settings ) { |
| 89 | $this->plugin = $plugin; |
| 90 | |
| 91 | $this->settings = wp_parse_args( $settings, [ |
| 92 | 'dashboard_widget' => false, // show dashboard widget (default: false) |
| 93 | 'right_sidebar' => false, // show adverts sidebar (default: false) |
| 94 | 'notice' => false, // show notice message (default: false), |
| 95 | 'business_suggetion' => false, |
| 96 | 'support' => false |
| 97 | ] ); |
| 98 | |
| 99 | $this->api = new Creative_Motion_API( $this->plugin ); |
| 100 | |
| 101 | add_filter( 'wbcr/factory/pages/impressive/widgets', [ $this, 'register_plugin_widgets' ], 10, 3 ); |
| 102 | add_action( 'wbcr/factory/admin_notices', [ $this, 'register_plugin_notice' ], 10, 2 ); |
| 103 | add_action( 'current_screen', [ $this, 'register_dashboard_widget' ], 10, 2 ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Directly get advert content for selected position. |
| 108 | * |
| 109 | * @since 1.0.1 Rename method. Content should now be printed. |
| 110 | * @since 1.0.0 Added |
| 111 | * |
| 112 | * @param string $position Custom position name |
| 113 | * |
| 114 | * @return void |
| 115 | */ |
| 116 | public function render_placement( $position = 'right_sidebar' ) { |
| 117 | $content = ''; |
| 118 | |
| 119 | if ( $position ) { |
| 120 | $content = $this->get_content( $position ); |
| 121 | } |
| 122 | |
| 123 | echo $content; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Register widgets. |
| 128 | * |
| 129 | * Depending on the settings, register new widgets. |
| 130 | * |
| 131 | * @since 1.0.0 Added |
| 132 | * |
| 133 | * @param array $widgets Already existing registered widgets |
| 134 | * @param string $position Position for the widget |
| 135 | * @param string $plugin Plugin object for which the hook is run |
| 136 | * |
| 137 | * @return array array( |
| 138 | * 'adverts_widget' => '<p></p>', |
| 139 | * 'business_suggetion' => '<p></p>', |
| 140 | * 'support' => '<p></p>', |
| 141 | * ... |
| 142 | * ) |
| 143 | */ |
| 144 | public function register_plugin_widgets( $widgets, $position, $plugin ) { |
| 145 | if ( $plugin->getPluginName() == $this->plugin->getPluginName() && 'right' == $position ) { |
| 146 | |
| 147 | if ( $this->settings['right_sidebar'] ) { |
| 148 | $content = $this->get_content( 'right_sidebar' ); |
| 149 | $widgets['adverts_widget'] = $content; |
| 150 | |
| 151 | if ( empty( $widgets['adverts_widget'] ) ) { |
| 152 | if ( defined( 'FACTORY_ADVERTS_DEBUG' ) && FACTORY_ADVERTS_DEBUG ) { |
| 153 | $debug_message = '<div style="background: #fff4f1;padding: 10px;color: #a58074;">'; |
| 154 | $debug_message .= $this->get_debug_message( 'right_sidebar' ); |
| 155 | $debug_message .= '</div>'; |
| 156 | |
| 157 | $widgets['adverts_widget'] = $debug_message; |
| 158 | } else { |
| 159 | unset( $widgets['adverts_widget'] ); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if ( $this->settings['business_suggetion'] ) { |
| 165 | $content = $this->get_content( 'business_suggetion' ); |
| 166 | |
| 167 | if ( ! empty( $content ) ) { |
| 168 | $widgets['business_suggetion'] = $content; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if ( $this->settings['support'] ) { |
| 173 | $content = $this->get_content( 'support' ); |
| 174 | |
| 175 | if ( ! empty( $content ) ) { |
| 176 | $widgets['support'] = $content; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return $widgets; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Регистрирует уведомление для текущего плагина |
| 186 | * |
| 187 | * Мы добавляем уведомления в массив все� |
| 188 | уведомлений плагина с ключем 'adverts_notice', |
| 189 | * то есть если другие плагины, тоже добавят свои рекламные уведомления, они просто |
| 190 | * будут перезаписывать друг друга, в итоге будет отображено только одно рекламное |
| 191 | * уведомеление. Это нужно для того, чтобы ограничить пользователя от спама. |
| 192 | * |
| 193 | * @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 194 | * |
| 195 | * @since 1.0.1 Переделан полностью под интферфейс фреймворка. Используем встроенную систему уведомлений. |
| 196 | * @since 1.0.0 Added |
| 197 | * |
| 198 | * @param array $notices Массив со списком все� |
| 199 | уведомлений, которые будут напечатыны в админ панели |
| 200 | * @param string $plugin_name Имя плагина, передано для того, чтобы выводить уведомления условно, только для конкретного плагина |
| 201 | */ |
| 202 | public function register_plugin_notice( $notices, $plugin_name ) { |
| 203 | if ( $plugin_name !== $this->plugin->getPluginName() ) { |
| 204 | return $notices; |
| 205 | } |
| 206 | |
| 207 | if ( $this->settings['notice'] ) { |
| 208 | $notice_content = $this->get_content( 'notice' ); |
| 209 | |
| 210 | if ( empty( $notice_content ) ) { |
| 211 | # Информация для отладки |
| 212 | if ( defined( 'FACTORY_ADVERTS_DEBUG' ) && FACTORY_ADVERTS_DEBUG ) { |
| 213 | $debug_message = $this->get_debug_message( 'notice' ); |
| 214 | |
| 215 | $notices['adverts_notice'] = [ |
| 216 | 'id' => 'adverts_debug', |
| 217 | 'type' => 'error', |
| 218 | 'dismissible' => false, |
| 219 | 'dismiss_expires' => 0, |
| 220 | 'text' => '<p><b>' . $this->plugin->getPluginTitle() . '</b>:<br>' . $debug_message . '</p>' |
| 221 | ]; |
| 222 | } |
| 223 | |
| 224 | return $notices; |
| 225 | } |
| 226 | |
| 227 | $hash = md5( $notice_content ); |
| 228 | |
| 229 | $notices['adverts_notice'] = [ |
| 230 | 'id' => 'adverts_' . $hash, |
| 231 | 'type' => 'success', |
| 232 | 'dismissible' => true, |
| 233 | 'dismiss_expires' => 0, |
| 234 | 'text' => '<p><b>' . $this->plugin->getPluginTitle() . '</b>:<br>' . $notice_content . '</p>' |
| 235 | ]; |
| 236 | } |
| 237 | |
| 238 | return $notices; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Include dashboard widget |
| 243 | * |
| 244 | * Include functionality the output of the widget on the dashboard. |
| 245 | * Only one dashboard widget must be shown for some plugins with this setting (dashboard_widget). |
| 246 | * |
| 247 | * @since 1.0.0 Added |
| 248 | */ |
| 249 | public function register_dashboard_widget() { |
| 250 | if ( $this->settings['dashboard_widget'] && current_user_can( 'manage_options' ) ) { |
| 251 | $current_screen = get_current_screen(); |
| 252 | |
| 253 | if ( ! in_array( $current_screen->id, [ 'dashboard', 'dashboard-network' ] ) ) { |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | $content = $this->get_content( 'dashboard_widget' ); |
| 258 | |
| 259 | if ( empty( $content ) && defined( 'FACTORY_ADVERTS_DEBUG' ) && FACTORY_ADVERTS_DEBUG ) { |
| 260 | $content = $this->get_debug_message( 'dashboard_widget' ); |
| 261 | } |
| 262 | |
| 263 | require_once FACTORY_ADVERTS_159_DIR . '/includes/class-dashboard-widget.php'; |
| 264 | new Dashboard_Widget( $this->plugin, $content ); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Позволяет получить сообщение об ошибка� |
| 270 | |
| 271 | * |
| 272 | * Метод проверяет последние ошибки, которые могли произойти в результате api запроса. |
| 273 | * Если ошибки есть, он выводит предупреждение и список последни� |
| 274 | ошибок. Если ошибок нет, |
| 275 | * метод вернет просто предупреждение, что реклама не настроена. |
| 276 | * |
| 277 | * @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 278 | * @since 1.0.1 |
| 279 | * |
| 280 | * @param string $position Position for the widget |
| 281 | * |
| 282 | * @return string Возвращает сообщение с последниеми ошибками для отладки |
| 283 | */ |
| 284 | private function get_debug_message( $position ) { |
| 285 | $debug_massage = 'Plugin ads not configured or server unavailable. See full error report below.<br>'; |
| 286 | |
| 287 | if ( isset( $this->errors[ $position ] ) && ! empty( $this->errors ) ) { |
| 288 | $debug_massage .= '<b>Last errors:</b><br>'; |
| 289 | foreach ( $this->errors[ $position ] as $error_code => $error_message ) { |
| 290 | $debug_massage .= 'Code: ' . $error_code . ' Error: ' . $error_message . '<br>'; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return $debug_massage; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Get advert content for selected position. |
| 299 | * |
| 300 | * @since 1.0.1 Полностью переписан |
| 301 | * @since 1.0.0 Added |
| 302 | * |
| 303 | * @param string $position The position for advert |
| 304 | * |
| 305 | * @return string |
| 306 | */ |
| 307 | private function get_content( $position ) { |
| 308 | if ( isset( $this->placements[ $position ] ) ) { |
| 309 | return $this->placements[ $position ]; |
| 310 | } |
| 311 | |
| 312 | $content = $this->api->get_content( $position ); |
| 313 | |
| 314 | if ( is_wp_error( $content ) ) { |
| 315 | $this->errors[ $position ][ $content->get_error_code() ] = $content->get_error_message(); |
| 316 | |
| 317 | return null; |
| 318 | } |
| 319 | |
| 320 | $this->placements[ $position ] = $content; |
| 321 | |
| 322 | return $content; |
| 323 | } |
| 324 | } |
| 325 |