Skip to content

Commit

Permalink
Changed loading of CSS and JS by using wp_enqueue_
Browse files Browse the repository at this point in the history
Refactor all functions to prevent conflicts, now there're all static method of a class
unify method to obtain path of plugin (to get url to assets)
  • Loading branch information
drzippie committed Jul 15, 2015
1 parent ccae5ed commit 8d67bfa
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 83 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
92 changes: 56 additions & 36 deletions inc/XowlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,79 @@
*
* If not, visit http://gnu.org/licenses/agpl-3.0.html.
*
* @author Ximdex DevTeam <[email protected]>
* @version $Revision$
* @author Ximdex DevTeam <[email protected]>
* @version $Revision$
*/
class XowlClient {

class XowlClient
{

private static $initiated = false;

public static function init() {


public static function init()
{

}

/**
* <p>Callback function for 'xowl/enhance' endpoint</p>
* <p>This function calls Ximdex Xowl endpoint to analyze and enhance the content</p>
*/
function xowl_enhance_content() {
function xowl_enhance_content()
{
$content = filter_input(INPUT_POST, 'content');
echo $service->suggest($content);
}


private static function init_hooks() {


private static function init_hooks()
{
}

public static function plugin_activation() {
add_option('xowl_endpoint', 'http://xowl.ximdex.net/api/v1/enhance' , true );

public static function plugin_activation()
{
add_option('xowl_endpoint', 'http://xowl.ximdex.net/api/v1/enhance', true);
add_option('xowl_apikey', '', true);
add_option('xowl_register', 'http://xowl.ximdex.net/register/signup', '', true);
}

public static function plugin_deactivation() {
}

public static function xowl_register_tinymce_plugin($plugin_array) {
$plugin_array['xowl_button'] = XOWL_PLUGIN_URL . '/tinymce/xowl_client/editor_plugin.js';
public static function plugin_deactivation()
{
}

public static function xowl_register_tinymce_plugin($plugin_array)
{


$plugin_array['xowl_button'] = XowlClient::urlTo( '/tinymce/xowl_client/editor_plugin.js' ) ;
return $plugin_array;
}

//Adding the new button to the Tiny's toolbar
public static function xowl_add_tinymce_button($buttons) {
public static function xowl_add_tinymce_button($buttons)
{
$buttons[] = "xowl_button";
return $buttons;
}

//Links in config tab
public static function admin_plugin_settings_link($links) {
public static function admin_plugin_settings_link($links)
{
$settings_link = '<a href="' . esc_url(self::get_page_url()) . '">' . __('Settings', 'xowl') . '</a>';
array_unshift($links, $settings_link);
return $links;
}

private static function get_page_url($page = 'config') {

private static function get_page_url($page = 'config')
{
$args = array(
'page' => 'xowl-config'
);
$url = add_query_arg($args, admin_url('options-general.php'));
return $url;
}

public static function admin_menu() {

public static function admin_menu()
{
$hook = add_options_page('Xowl Configuration', 'Xowl Service', 'manage_options', 'xowl-config', array(
'XowlClient',
'display_page'
Expand All @@ -88,19 +101,26 @@ public static function admin_menu() {
'admin_help'
));
}

private static function updateVar($name) {

private static function updateVar($name)
{
if (!empty($_POST[$name]) && get_option($name) != $_POST[$name]) {
update_option($name, trim( $_POST[$name]) );
update_option($name, trim($_POST[$name]));
}
}

public static function display_page() {

public static function display_page()
{
wp_enqueue_style('xowl-admin-css', XowlClient::urlTo( 'assets/css/styles.css' ) , "",XOWL_VERSION, "screen" );
wp_enqueue_script('xowl-admin-js', XowlClient::urlTo( 'assets/js/config-form.js' ) , array( "jquery") , XOWL_VERSION );
self::updateVar('xowl_endpoint');
self::updateVar('xowl_apikey');
require_once ('xowl-config.php');
require_once('xowl-config.php');
}
public static function urlTo( $route = '' ) {
return XOWL_PLUGIN_URL . $route ;
}

public static function admin_help() {
public static function admin_help()
{
}
}
10 changes: 4 additions & 6 deletions inc/xowl-config.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<link rel="stylesheet" href="<?php echo XOWL_PLUGIN_URL . '/assets/css/styles.css'?>"/>

<form action="" method="POST">
<div class="xowl-settings">
<div class="xowl-header">
<img src="<?php echo plugins_url('../assets/imgs/logo-xowl.png', __FILE__); ?>" alt="XOWL Service" title="XOWL Service">
<img class="xowl-flower" src="<?php echo plugins_url('../assets/imgs/h2-flower.png', __FILE__) ?>" alt="Ximdex" title="Ximdex">
<img src="<?php echo XowlClient::urlTo( 'assets/imgs/logo-xowl.png' ) ; ?>" alt="XOWL Service" title="XOWL Service">
<img class="xowl-flower" src="<?php echo XowlClient::urlTo( 'assets/imgs/h2-flower.png' ) ; ?>" alt="Ximdex" title="Ximdex">

<input class="button-xowl-save-changes button button-primary" type="submit" value="<?php echo _e( 'Save all the changes') ?>">
</div>
Expand Down Expand Up @@ -47,8 +46,8 @@

<button id="xowl-check-token" type="button" class="button button-primary">Check token</button>

<img id="xowl-check-icon-1" class="xowl-check-icon" src="<?php echo plugins_url('../assets/imgs/check-icon-1.png', __FILE__); ?>">
<img id="xowl-check-icon-2" class="xowl-check-icon" src="<?php echo plugins_url('../assets/imgs/check-icon-2.png', __FILE__); ?>">
<img id="xowl-check-icon-1" class="xowl-check-icon" src="<?php echo XowlClient::urlTo( 'assets/imgs/check-icon-1.png' ); ?>">
<img id="xowl-check-icon-2" class="xowl-check-icon" src="<?php echo XowlClient::urlTo( 'assets/imgs/check-icon-2.png' ); ?>">
</div>
</div>
</div>
Expand Down Expand Up @@ -84,4 +83,3 @@
</div>
</form>

<script src="<?php echo plugins_url('../assets/js/config-form.js', __FILE__); ?>"></script>
88 changes: 47 additions & 41 deletions wp-xowl-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,64 @@

// Constants definition
defined('ABSPATH') or die('Do not execute me naked, please!');
//Avoiding direct execution!
//Avoiding direct execution!
define('XOWL_VERSION', '0.1');
define('XOWL_MINIMUM_WP_VERSION', '4.2');
define('XOWL_PLUGIN_URL', plugin_dir_url(__FILE__));
define('XOWL_PLUGIN_DIR', plugin_dir_path(__FILE__));

require_once (XOWL_PLUGIN_DIR . '/inc/XowlClient.php');
require_once(XOWL_PLUGIN_DIR . '/inc/XowlClient.php');

register_activation_hook(__FILE__, array('XowlClient', 'plugin_activation'));
register_deactivation_hook(__FILE__, array('XowlClient', 'plugin_deactivation'));
Xowl_Plugin::init() ;

// Activate the main stuff of the plugin
add_action('init', array('XowlClient', 'init'));

// Adding settings menu
add_action('admin_menu', array('XowlClient', 'admin_menu'), 5);
class Xowl_Plugin
{

// Add the TinyMCE Xowl Plugin
add_filter("mce_external_plugins", array('XowlClient', "xowl_register_tinymce_plugin"));
add_filter('mce_buttons', array('XowlClient', 'xowl_add_tinymce_button'));

// Pass variables from wordpress to tinymce
foreach (array('post.php', 'post-new.php') as $hook) {
add_action("admin_head-$hook", 'xowl_post_head');
}

function xowl_post_head() {


$conf = array(
'xowl_endpoint' => get_option('xowl_endpoint'),
'xowl_apikey' => get_option('xowl_apikey'),
'xowl_plugin_url' => XOWL_PLUGIN_URL ,
'xowl_css' => XOWL_PLUGIN_URL . 'assets/css/styles.css' ,
static public function post_head()
{
$conf = array(
'xowl_endpoint' => get_option('xowl_endpoint'),
'xowl_apikey' => get_option('xowl_apikey'),
'xowl_plugin_url' => XowlClient::urlTo( ),
'xowl_css' => XowlClient::urlTo( 'assets/css/styles.css' ),
);

$confJson = json_encode( $conf ) ;
echo <<<EOF
<script type='text/javascript'>
var xowlPlugin = {$confJson}
</script>
EOF;
$confJson = json_encode($conf);
echo "<script type=\"text/javascript\">var xowlPlugin = {$confJson};</script>";

}

static public function filter_post_data($data, $postarr)
{
$changeFrom = array('/<a class=\\\"xowl-suggestion\\\" (.*) data-(.*)>/iUs');
$data['post_content'] = preg_replace($changeFrom, '<a \1>', $data['post_content']);
return $data;
}

static public function init()
{
register_activation_hook(__FILE__, array('XowlClient', 'plugin_activation'));
register_deactivation_hook(__FILE__, array('XowlClient', 'plugin_deactivation'));

// Activate the main stuff of the plugin
add_action('init', array('XowlClient', 'init'));

// Adding settings menu
add_action('admin_menu', array('XowlClient', 'admin_menu'), 5);

// Add the TinyMCE Xowl Plugin
add_filter("mce_external_plugins", array('XowlClient', "xowl_register_tinymce_plugin"));
add_filter('mce_buttons', array('XowlClient', 'xowl_add_tinymce_button'));

// Pass variables from wordpress to tinymce
foreach (array('post.php', 'post-new.php') as $hook) {
add_action("admin_head-$hook", array('Xowl_Plugin' , 'post_head'));
}
// Capture post content and filter link attributes
add_filter('wp_insert_post_data', array('Xowl_Plugin' , 'filter_post_data'), '99', 2);
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array('XowlClient', 'admin_plugin_settings_link'), 10, 2);
}
}


// Capture post content and filter link attributes
add_filter('wp_insert_post_data', 'filter_post_data', '99', 2);

function filter_post_data($data, $postarr) {
$changeFrom = array('/<a class=\\\"xowl-suggestion\\\" (.*) data-(.*)>/iUs');
$data['post_content'] = preg_replace($changeFrom, '<a \1>', $data['post_content']);
return $data;
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array('XowlClient', 'admin_plugin_settings_link'), 10, 2);

0 comments on commit 8d67bfa

Please sign in to comment.