|
| 1 | +<?php |
| 2 | + |
| 3 | +class AffilinetWidgetsPlugin { |
| 4 | + |
| 5 | + public function __construct() { |
| 6 | + |
| 7 | + |
| 8 | + add_action( 'admin_init', array( $this, 'admin_init' ) ); |
| 9 | + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 10 | + add_action( 'widgets_init', array( $this, 'register_widget' ) ); |
| 11 | + |
| 12 | + add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 13 | + |
| 14 | + add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); |
| 15 | + |
| 16 | + add_shortcode( 'affilinet_widget', array( $this, 'affilinet_product_widgets_shortcode' ) ); |
| 17 | + |
| 18 | + add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
| 19 | + |
| 20 | + |
| 21 | + add_filter( 'plugin_action_links_' .plugin_basename(AFFILINET_PRODUCT_WIDGETS_PLUGIN_FILE ), array( $this, 'plugin_add_settings_link' ) ); |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + /** |
| 30 | + * Register Settings for admin area |
| 31 | + */ |
| 32 | + public function admin_init() { |
| 33 | + register_setting( 'affilinet-product-widgets-settings-group', 'affilinet_product_widgets_publisher_id' ); |
| 34 | + register_setting( 'affilinet-product-widgets-settings-group', 'affilinet_product_widgets_publisher_webservice_password' ); |
| 35 | + // this value will change with every update and can be used to hook the password change |
| 36 | + register_setting( 'affilinet-product-widgets-settings-group', 'affilinet_product_widgets_last_credential_change', array( |
| 37 | + 'sanitize_callback' => array( |
| 38 | + $this, |
| 39 | + 'validate_credentials' |
| 40 | + ) |
| 41 | + ) ); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + function validate_credentials( $timestamp ) { |
| 46 | + // clear the cache to get the new values |
| 47 | + wp_cache_delete( 'alloptions', 'options' ); |
| 48 | + |
| 49 | + // check credentials |
| 50 | + if ( AffilinetWidgetsApi::logon() === false ) { |
| 51 | + add_settings_error( 'affilinet_product_widgets_publisher_webservice_password', '1', __( 'Invalid Webservice Password or Publisher ID ', 'affilinet-product-widgets' ) ); |
| 52 | + update_option( 'affilinet_product_widgets_webservice_login_is_correct', 'false', true ); |
| 53 | + wp_cache_delete( 'alloptions', 'options' ); |
| 54 | + |
| 55 | + return $timestamp; |
| 56 | + } |
| 57 | + |
| 58 | + return $timestamp; |
| 59 | + } |
| 60 | + |
| 61 | + function admin_notice() { |
| 62 | + |
| 63 | + if ( get_option( 'affilinet_product_widgets_webservice_login_is_correct', 'false' ) === 'false' ) { |
| 64 | + ?> |
| 65 | + <div class="notice notice-warning is-dismissible"> |
| 66 | + <p><?php _e( '<strong>affilinet Widgets:</strong><br> Please make sure you have entered the correct Publisher ID and Publisher Webservice password.', 'affilinet-product-widgets' ); ?> |
| 67 | + <a class="button" |
| 68 | + href="<?php echo admin_url( 'options-general.php?page=affilinet-product-widgets-settings' ); ?>"><?php _e( 'Check your settings.', 'affilinet-product-widgets' ); ?></a> |
| 69 | + |
| 70 | + </p> |
| 71 | + </div> |
| 72 | + <?php |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * Create the admin Menu |
| 79 | + */ |
| 80 | + public function admin_menu() { |
| 81 | + // options menu |
| 82 | + add_options_page( 'affilinet Widgets Settings', 'affilinet Widgets', 'manage_options', 'affilinet-product-widgets-settings', 'AffilinetWidgetsView::settings' ); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Register the widget |
| 87 | + */ |
| 88 | + public function register_widget() { |
| 89 | + register_widget( 'AffilinetWidgetsWidget' ); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + /** |
| 94 | + * Load Admin scripts |
| 95 | + * |
| 96 | + * @param $hook string |
| 97 | + */ |
| 98 | + public function admin_enqueue_scripts( $hook ) { |
| 99 | + // on post page add the editor button for affilinet plugin |
| 100 | + if ( $hook === 'post.php' || $hook == 'post-new.php' ) { |
| 101 | + add_action( 'admin_head', array( $this, 'register_tiny_mce_buttons' ) ); |
| 102 | + add_action( "admin_head-$hook", array( $this, 'add_tiny_mce_variables' ) ); |
| 103 | + } |
| 104 | + |
| 105 | + // on settings page integrate font awesome |
| 106 | + |
| 107 | + if ( $hook == 'settings_page_affilinet-product-widgets-settings' ) { |
| 108 | + wp_enqueue_style( 'font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css' ); |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + public function plugin_add_settings_link( $links ) { |
| 114 | + |
| 115 | + $settings_link = '<a href="' . admin_url( 'options-general.php?page=affilinet-product-widgets-settings' ) . '">' . __( 'Settings' ) . '</a>'; |
| 116 | + array_push( $links, $settings_link ); |
| 117 | + return $links; |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + /** |
| 122 | + * Shortcode |
| 123 | + * |
| 124 | + * @param array $params |
| 125 | + * |
| 126 | + * @return string |
| 127 | + */ |
| 128 | + public function affilinet_product_widgets_shortcode( $params = array() ) { |
| 129 | + // default $widget_id parameter |
| 130 | + /** |
| 131 | + * @var String $size |
| 132 | + */ |
| 133 | + extract( shortcode_atts( array( |
| 134 | + 'id' => 'notset', |
| 135 | + ), $params ) ); |
| 136 | + |
| 137 | + return AffilinetWidgetsWidget::getAdCode( $params['id'] ); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * TRANSLATION |
| 142 | + */ |
| 143 | + public function load_textdomain() { |
| 144 | + load_plugin_textdomain( 'affilinet-product-widgets', false, dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages' ); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * TinyMCE Editor Button |
| 149 | + */ |
| 150 | + public function register_tiny_mce_buttons() { |
| 151 | + // check user permissions |
| 152 | + if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { |
| 153 | + return; |
| 154 | + } |
| 155 | + // check if WYSIWYG is enabled |
| 156 | + if ( get_user_option( 'rich_editing' ) == 'true' ) { |
| 157 | + add_filter( 'mce_external_plugins', array( $this, 'add_buttons' ) ); |
| 158 | + add_filter( 'mce_buttons', array( $this, 'register_buttons' ) ); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Load TinyMCE Variables |
| 164 | + */ |
| 165 | + public function add_tiny_mce_variables() { |
| 166 | + $img = plugin_dir_url( plugin_basename( dirname( __FILE__ ) ) ) . 'images/'; |
| 167 | + $widgets = AffilinetWidgetsApi::getMyWidgets(); |
| 168 | + $widgetsForTinyMce = []; |
| 169 | + foreach ( $widgets as $widget ) { |
| 170 | + $widgetsForTinyMce[] = [ |
| 171 | + 'value' => $widget['id'], |
| 172 | + 'id' => $widget['id'], |
| 173 | + 'text' => $widget['widgetName'], |
| 174 | + ]; |
| 175 | + } |
| 176 | + ?> |
| 177 | + <!-- TinyMCE Shortcode Plugin --> |
| 178 | + <script type='text/javascript'> |
| 179 | + var affilinet_product_widgets_mce_variables = { |
| 180 | + 'image_path': '<?php echo $img; ?>', |
| 181 | + 'choose_widget': 'Choose widget', |
| 182 | + 'widgets': <?php echo json_encode( $widgetsForTinyMce, JSON_PRETTY_PRINT ); ?>, |
| 183 | + }; |
| 184 | + </script> |
| 185 | + <!-- TinyMCE Shortcode Plugin --> |
| 186 | + <?php |
| 187 | + } |
| 188 | + |
| 189 | + public function add_buttons( $plugin_array ) { |
| 190 | + $plugin_array['affilinet_product_widgets_mce_button'] = plugin_dir_url( plugin_basename( dirname( __FILE__ ) ) ) . 'js/affilinet_product_widgets_mce_button.js'; |
| 191 | + |
| 192 | + return $plugin_array; |
| 193 | + } |
| 194 | + |
| 195 | + public function register_buttons( $buttons ) { |
| 196 | + array_push( $buttons, 'affilinet_product_widgets_mce_button' ); |
| 197 | + |
| 198 | + return $buttons; |
| 199 | + } |
| 200 | + |
| 201 | +} |
0 commit comments