Skip to content

Commit 8c8b24e

Browse files
author
sgotre
committed
v0.92
1 parent d09dedf commit 8c8b24e

24 files changed

Lines changed: 1340 additions & 0 deletions
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
Plugin Name: affilinet Product Widgets
5+
Plugin URI: https://www.affili.net/de/publisher/tools/influencer-web-extension
6+
Description: A brief description of the Plugin.
7+
Version: 0.92
8+
Author: affilinet
9+
Author URI: https://www.affili.net/
10+
License: GPLv2 or later
11+
*/
12+
13+
define("AFFILINET_PRODUCT_WIDGETS_PLUGIN_DIR", dirname(__FILE__) . DIRECTORY_SEPARATOR);
14+
define("AFFILINET_PRODUCT_WIDGETS_PLUGIN_FILE", plugin_basename( __FILE__) );
15+
16+
foreach (glob( AFFILINET_PRODUCT_WIDGETS_PLUGIN_DIR . "classes/*.php") as $filename) {
17+
include $filename;
18+
}
19+
20+
new AffilinetWidgetsPlugin();
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
4+
class AffilinetWidgetsApi
5+
{
6+
7+
const API_ENDPOINT = 'https://productwidget.com/api/v1.0/widgets';
8+
9+
/**
10+
* Login at the a token
11+
*
12+
* Returns false if password mismatch
13+
*
14+
* @param null $username
15+
* @param null $password
16+
*
17+
* @return bool|String $credentialToken
18+
*/
19+
public static function logon($username = null, $password = null)
20+
{
21+
22+
try {
23+
if ($password === null) {
24+
$password = get_option('affilinet_product_widgets_publisher_webservice_password');
25+
}
26+
if ($username === null) {
27+
$username = get_option('affilinet_product_widgets_publisher_id');
28+
}
29+
$logon_client = new \SoapClient('https://api.affili.net/V2.0/Logon.svc?wsdl');
30+
$params = array(
31+
"Username" => $username,
32+
"Password" => $password,
33+
"WebServiceType" => "Publisher"
34+
);
35+
$token = $logon_client->__soapCall("Logon", array($params));
36+
37+
if ($token !== false) {
38+
update_option('affilinet_product_widgets_webservice_login_is_correct', 'true', true);
39+
}
40+
41+
return $token;
42+
} catch (\SoapFault $e) {
43+
update_option('affilinet_product_widgets_webservice_login_is_correct', 'false', true);
44+
$errorMessage = __('Could not connect to affilinet API. Please recheck your Webservice Password and Publisher ID', 'affilinet-product-widgets');
45+
AffilinetWidgetsHelper::displayHugeAdminMessage($errorMessage, 'error', 'fa-exclamation-triangle');
46+
47+
return false;
48+
}
49+
}
50+
51+
/**
52+
* @return array
53+
*/
54+
public static function getMyWidgets()
55+
{
56+
$errorMessage = __('Could not connect to affilinet API. Please recheck your Webservice Password and Publisher ID', 'affilinet-product-widgets');
57+
58+
try {
59+
$token = self::logon();
60+
$publisherId = get_option('affilinet_product_widgets_publisher_id');
61+
if ( $token === false ) {
62+
// error already displayed
63+
return [];
64+
}
65+
66+
67+
$url = self::API_ENDPOINT . "?credentialToken=" . $token . "&publisherId=" . $publisherId;
68+
$response = wp_remote_get($url);
69+
if ( is_array( $response ) ) {
70+
return json_decode( $response['body'], true); // use the content
71+
} else {
72+
AffilinetWidgetsHelper::displayHugeAdminMessage($errorMessage, 'error', 'fa-exclamation-triangle');
73+
return [];
74+
}
75+
76+
}
77+
catch (\SoapFault $e) {
78+
$errorMessage = $e->getMessage();
79+
AffilinetWidgetsHelper::displayHugeAdminMessage($errorMessage, 'error', 'fa-exclamation-triangle');
80+
return [];
81+
}
82+
}
83+
84+
85+
86+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
4+
class AffilinetWidgetsHelper
5+
{
6+
/**
7+
* Helper to display an error message
8+
*
9+
* @param $message
10+
* @param string $type
11+
* @param bool $icon
12+
*/
13+
public static function displayHugeAdminMessage($message, $type = 'error', $icon = false)
14+
{
15+
wp_enqueue_style('font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
16+
add_action( 'admin_notices', function() use ($message , $type, $icon ){
17+
18+
?>
19+
<div class="notice-<?php echo $type?> notice" style="min-height:75px;">
20+
21+
<?php
22+
if ($icon !== false) {
23+
switch ($type) {
24+
case'error' : $color = 'rgb(230, 73, 64)';break;
25+
case'warning' : $color = 'rgb(255, 197, 2)';break;
26+
case'success' : $color = 'rgb(84, 190, 100)';break;
27+
case'info' :
28+
default:
29+
$color = 'rgb(23, 175, 218)';
30+
}
31+
?>
32+
<div style="width: 50px;padding: 10px 20px;display: inline-block;">
33+
<i class="fa <?php echo $icon; ?>" style="font-size: 40px; color: <?php echo $color;?>; position:absolute; margin-top:10px;"></i>
34+
</div>
35+
<?php
36+
}
37+
?>
38+
39+
40+
<p style="display: inline-block;position: absolute; margin-top: 18px;">
41+
<strong>affilinet Product Widgets</strong> <br>
42+
<?php echo $message;?>
43+
</p>
44+
<div class="clearfix"></div>
45+
</div>
46+
<?php
47+
48+
});
49+
50+
}
51+
52+
53+
/**
54+
* Returns current plugin version.
55+
*
56+
* @return string Plugin version
57+
*/
58+
public static function get_plugin_version()
59+
{
60+
if ( ! function_exists( 'get_plugins' ) )
61+
require_once ABSPATH . 'wp-admin/includes/plugin.php';
62+
$plugin_folder = get_plugin_data( AFFILINET_PRODUCT_WIDGETS_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'affilinet.php') ;
63+
64+
return $plugin_folder['Version'];
65+
}
66+
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
class AffilinetWidgetsView
4+
{
5+
public static function settings()
6+
{
7+
require_once AFFILINET_PRODUCT_WIDGETS_PLUGIN_DIR . 'views' . DIRECTORY_SEPARATOR . 'settings.php';
8+
}
9+
10+
11+
}

0 commit comments

Comments
 (0)