Skip to content

Commit

Permalink
Merge pull request #45 from rsingel/develop
Browse files Browse the repository at this point in the history
Version 4.0
  • Loading branch information
anikolaenko committed Apr 26, 2016
2 parents e668b98 + cf42cbe commit c1e3907
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 409 deletions.
376 changes: 233 additions & 143 deletions Contextly.php

Large diffs are not rendered by default.

20 changes: 14 additions & 6 deletions ContextlySettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,22 @@ private function displaySettingsAutoloadStuff( $button_id, $disabled_flag = fals
if ( is_admin() && isset( $options["api_key"] ) && $options["api_key"] )
{
$contextly_object = new Contextly();
$contextly_object->loadContextlyAjaxJSScripts();
$contextly_object->insertKitScripts( array(
'foreign' => array(
'wp/widgets' => true,
),
'preload' => 'wp/widgets',
) );
?>
<script>
jQuery( document ).ready(
function () {
Contextly.SettingsAutoLogin.doLogin( <?php echo json_encode( $button_id ) ?>, <?php echo json_encode( $disabled_flag ) ?> );
}
);
<?php print $contextly_object->getWpDataJS(); ?>
Contextly.ready('load', 'wp/widgets', function() {
jQuery( document ).ready(
function () {
Contextly.WPSettingsAutoLogin.doLogin( <?php echo json_encode( $button_id ) ?>, <?php echo json_encode( $disabled_flag ) ?> );
}
);
});
</script>
<?php
}
Expand Down
4 changes: 3 additions & 1 deletion ContextlySiderailWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ public function widget( $args, $instance ) {
return;
}

$classes = Contextly::WIDGET_SIDERAIL_CLASS . ' ' . Contextly::WIDGET_PLACEMENT_CLASS;

print $args['before_widget'];
print '<div class="ctx-siderail-container"></div>';
printf( '<div class="%s"></div>', esc_attr( $classes ) );
print $args['after_widget'];
}

Expand Down
2 changes: 1 addition & 1 deletion ContextlySocialWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function widget( $args, $instance ) {
$classes = Contextly::WIDGET_SOCIAL_CLASS . ' ' . Contextly::WIDGET_PLACEMENT_CLASS;

print $args['before_widget'];
print '<div class="' . esc_attr( $classes ) . '"></div>';
printf( '<div class="%s"></div>', esc_attr( $classes ) );
print $args['after_widget'];
}

Expand Down
102 changes: 45 additions & 57 deletions Kit.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* @method ContextlyWpApiTransport newApiTransport()
* @method ContextlyWpSharedSession newApiSession()
* @method ContextlyWpWidgetsEditor newWidgetsEditor()
* @method ContextlyWpAssetsRenderer newWpAssetsRenderer()
* @method ContextlyWpOverlayPage newWpOverlayPage()
*/
class ContextlyWpKit extends ContextlyKit {
Expand Down Expand Up @@ -50,13 +49,45 @@ protected function getClassesMap() {

$map['ApiTransport'] = 'ContextlyWpApiTransport';
$map['ApiSession'] = 'ContextlyWpSharedSession';
$map['AssetsPackage'] = 'ContextlyWpAssetsPackage';
$map['WidgetsEditor'] = 'ContextlyWpWidgetsEditor';
$map['WpAssetsRenderer'] = 'ContextlyWpAssetsRenderer';
$map['WpOverlayPage'] = 'ContextlyWpOverlayPage';

return $map;
}

public function getLoaderName() {
// We only override the loader name in dev mode.
// See ContextlyWpAssetsPackage::getJs() for live mode.
if ($this->isDevMode() && CONTEXTLY_MOD !== false) {
return 'mods/' . CONTEXTLY_MOD;
}
else {
return 'loader';
}
}

}

class ContextlyWpAssetsPackage extends ContextlyKitAssetsPackage {

function getJs() {
$js = parent::getJs();

// Since we don't ship aggregated mod configs and can't use the modified loader
// package, have to replace the asset itself.
if ($this->kit->isLiveMode() && CONTEXTLY_MOD !== false) {
foreach ($js as &$name) {
if ($name === 'loader') {
$name = 'mods--' . CONTEXTLY_MOD;
}
}
unset($name);
}

return $js;
}

}

class ContextlyWpApiTransport implements ContextlyKitApiTransportInterface {
Expand Down Expand Up @@ -153,61 +184,10 @@ public function getToken() {

}

class ContextlyWpAssetsRenderer extends ContextlyKitAssetsRenderer {

public function resourceHandle($key) {
return 'contextly-kit-' . str_replace( '/', '-', $key );
}

/**
* Returns "version" parameter suitable for wp_enqueue_style() and
* wp_enqueue_script().
*/
protected function getAssetsVersion() {
if ($this->kit->isCdnEnabled()) {
return NULL;
}
else {
return $this->kit->version();
}
}

public function renderCss() {
$version = $this->getAssetsVersion();
foreach ($this->assets->buildCssUrls() as $key => $url) {
wp_enqueue_style($this->resourceHandle( $key ), $url, array(), $version);
}
}

public function renderJs() {
$version = $this->getAssetsVersion();
foreach ($this->assets->buildJsUrls() as $key => $url) {
wp_enqueue_script( $this->resourceHandle( $key ), $url, array(), $version, true );
}
}

public function renderAll() {
$this->renderCss();
$this->renderJs();
$this->renderTpl();
$this->renderInlineJs();
}

public function renderTpl() {
// TODO: Implement for widgets rendering later.
}

public function renderInlineJs() {
}

public function getInlineJs() {
return $this->assets->buildInlineJs(TRUE);
}

}

/**
* Handles the page required for the overlay dialogs.
*
* @property ContextlyWpKit $kit
*/
class ContextlyWpOverlayPage extends ContextlyKitBase {

Expand All @@ -223,13 +203,21 @@ public function registerPage() {
}

public function display() {
global $contextly;

$type = $_GET['editor-type'];
if (!in_array( $type, array( 'link', 'snippet', 'sidebar' ), TRUE )) {
$GLOBALS['contextly']->return404();
$contextly->return404();
}

$overrides = $this->kit->newOverridesManager( $contextly->getKitSettingsOverrides() )
->compile();

print $this->kit->newOverlayDialog($type)
->render();
->render( array(
'loader' => $this->kit->getLoaderName(),
'code' => $overrides,
) );
exit;
}

Expand Down
2 changes: 1 addition & 1 deletion Urls.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static public function getMainServerUrl() {
}

static public function getPluginCdnUrl( $file, $type = 'js' ) {
return 'https://rest.contextly.com/wp-plugin/' . CONTEXTLY_PLUGIN_VERSION . '/' . $type . '/' . $file;
return 'https://assets.contextly.com/wp-plugin/' . CONTEXTLY_PLUGIN_VERSION . '/' . $type . '/' . $file;
}

}
11 changes: 8 additions & 3 deletions contextly-linker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
Plugin URI: http://contextly.com
Description: Adds the Contextly content recommendation tool to your site. Contextly provides related, trending, evergreen and personalized recommendations to help your readers keep reading. Includes text, video and product recommendations to show off your best content.
Author: Contextly
Version: 3.4
Version: 4.0
*/

define ( "CONTEXTLY_MODE", 'live' );
if ( ! defined( "CONTEXTLY_MODE" ) ) {
define ( "CONTEXTLY_MODE", 'live' );
}

// Force all live clients to use HTTPS connection
$is_https = is_ssl();
Expand All @@ -17,8 +19,11 @@

define ( "CONTEXTLY_HTTPS", $is_https );
define ( "CONTEXTLY_PLUGIN_FILE", __FILE__ );
define ( "CONTEXTLY_PLUGIN_VERSION", '3.4' );
define ( "CONTEXTLY_PLUGIN_VERSION", '4.0' );
define ( "CONTEXTLY_CDN_VERSION", 'branch' );
if ( ! defined( "CONTEXTLY_MOD" ) ) {
define ( "CONTEXTLY_MOD", false );
}

require_once ( "kit/server/Kit.php" );
ContextlyKit::registerAutoload();
Expand Down
62 changes: 52 additions & 10 deletions js/contextly-post-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},

buildAjaxConfig: function (method, addon) {
var settings = Contextly.Settings;
var settings = Contextly.WPSettings;

var result = $.extend(true, {
url : settings.getAjaxUrl(),
Expand All @@ -29,7 +29,7 @@
result.data = {
action: 'contextly_widgets_editor_request',
nonce: settings.getAjaxNonce(),
post_id: Contextly.Settings.getPageId(),
post_id: settings.getEditorPostId(),
method: method,
params: params
};
Expand Down Expand Up @@ -77,7 +77,46 @@
return this.data;
},

setSnippet: function (savedSnippet) {
/**
* Makes a safe clone of a variable created in an iframe.
*
* IE 8-11 doesn't allow to run methods on objects that have been created
* in an iframe that doesn't exist anymore. To solve the problem, we clone
* the object making sure no methods are copied.
*/
cloneFrameObject: function(obj) {
var copy;
if (Contextly.Utils.isString(obj)) {
// Make sure we return a primitive.
copy = '' + obj;
}
else if (Contextly.Utils.isArray(obj)) {
copy = [];
for (var i = 0; i < obj.length; i++) {
copy[i] = this.cloneFrameObject(obj[i]);
}
}
else if (Contextly.Utils.isObject(obj)) {
copy = {};
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (Contextly.Utils.isFunction(obj[key])) {
Contextly.Utils.error('Unable to clone function created in iframe');
}

copy[key] = this.cloneFrameObject(obj[key]);
}
}
}
else {
copy = obj;
}
return copy;
},

setSnippet: function (savedFrameSnippet) {
var savedSnippet = this.cloneFrameObject(savedFrameSnippet);

this.data.snippet = savedSnippet;
this.updateAdminControls();
this.updateWidgetsPreview();
Expand All @@ -101,7 +140,9 @@
this.fireEvent('contextlyWidgetRemoved', 'snippet', id, removedSnippet);
},

setSidebar: function (savedSidebar) {
setSidebar: function (savedFrameSidebar) {
var savedSidebar = this.cloneFrameObject(savedFrameSidebar);

this.data.sidebars[savedSidebar.id] = savedSidebar;
this.fireEvent('contextlyWidgetUpdated', 'sidebar', savedSidebar.id, savedSidebar);
},
Expand All @@ -116,7 +157,10 @@
this.fireEvent('contextlyWidgetRemoved', 'sidebar', id, removedSidebar);
},

setAutoSidebar: function (savedAutoSidebar, removedId) {
setAutoSidebar: function (savedFrameAutoSidebar, frameRemovedId) {
var savedAutoSidebar = this.cloneFrameObject(savedFrameAutoSidebar);
var removedId = this.cloneFrameObject(frameRemovedId);

this.data.auto_sidebar = savedAutoSidebar;
this.fireEvent('contextlyWidgetUpdated', 'auto-sidebar', savedAutoSidebar.id || removedId, savedAutoSidebar);
},
Expand Down Expand Up @@ -145,7 +189,7 @@
},

buildEditorUrl: function (type) {
var s = Contextly.Settings;
var s = Contextly.WPSettings;

var url = s.getEditorUrl();
if (url.indexOf('?') === -1) {
Expand Down Expand Up @@ -259,7 +303,7 @@
$('#publish')
.unbind('click.contextlyPublishConfirmation')
.bind('click.contextlyPublishConfirmation', this.proxy(function () {
var wp_settings = Contextly.Settings.getWPSettings();
var wp_settings = Contextly.WPSettings.getWPSettings();

if (wp_settings.publish_confirmation && wp_settings.publish_confirmation == "1" && this.data !== null) {
// Put snippet and sidebars together for a check.
Expand Down Expand Up @@ -295,7 +339,7 @@

setTimeout(this.proxy(function() {
this.isUpdateQueued = false;
Contextly.WPPageView.loadWidgets();
Contextly.ready('widgets');
}), 1);
},

Expand Down Expand Up @@ -432,6 +476,4 @@

});

Contextly.PostEditor.loadData();

})( jQuery );
Loading

0 comments on commit c1e3907

Please sign in to comment.