Просмотр исходного кода

#1135 Embedly & Iframely re-integration

Alex Trofimov 8 лет назад
Родитель
Сommit
f4fd243d5b

+ 113 - 0
inc/classes/BxDolEmbed.php

@@ -0,0 +1,113 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+/**
+ * Embed provider.
+ *
+ * Create an embed from a link.
+ *
+ * @section editor_create Creating the Editor object:
+ *
+ *
+ * Add record to 'sys_objects_embeds' table:
+ *
+ * - object: name of the embed object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing
+ * - title: title of the embed provider, dmay be isplayed in the Studio.
+ * - override_class_name: user defined class name which is derived from one of base editor classes.
+ * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
+ *
+ *
+ * @section example Example of usage
+ *
+ * Generate HTML for a link which will be converted to an embed later:
+ *
+ * @code
+ *  $oEmbed = BxDolEmbed::getObjectInstance(); // get default embed object instance
+ *  if ($oEmbed) // check if editor is available for using
+ *      echo $oEmbed->getLinkHTML ('https://una.io', 'UNA.IO'); // output HTML which will be automatically converted into embed upon page load
+ *  else
+ *      echo '<a href="https://una.io">UNA.IO</a>';
+ * @endcode
+ *
+ */
+class BxDolEmbed extends BxDolFactory implements iBxDolFactoryObject
+{
+    protected $_sObject;
+    protected $_aObject;
+
+    /**
+     * Constructor
+     * @param $aObject array of editor options
+     */
+    protected function __construct($aObject)
+    {
+        parent::__construct();
+
+        $this->_sObject = $aObject['object'];
+        $this->_aObject = $aObject;
+    }
+
+    /**
+     * Get editor object instance by object name
+     * @param $sObject object name
+     * @return object instance or false on error
+     */
+    static public function getObjectInstance($sObject = false, $oTemplate = false)
+    {
+        if (!$sObject)
+            $sObject = getParam('sys_embed_default');
+
+        if (isset($GLOBALS['bxDolClasses']['BxDolEmbed!'.$sObject]))
+            return $GLOBALS['bxDolClasses']['BxDolEmbed!'.$sObject];
+
+        $aObject = BxDolEmbedQuery::getObject($sObject);
+        if (!$aObject || !is_array($aObject))
+            return false;
+
+        if (empty($aObject['override_class_name']))
+            return false;
+
+        $sClass = $aObject['override_class_name'];
+        if (!empty($aObject['override_class_file']))
+            require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
+
+        $o = new $sClass($aObject, $oTemplate);
+
+        return ($GLOBALS['bxDolClasses']['BxDolEmbed!'.$sObject] = $o);
+    }
+
+    /**
+     * Get object name
+     */
+    public function getObjectName ()
+    {
+        return $this->_sObject;
+    }
+
+    /**
+     * Print HTML which will be automatically converted into embed upon page load
+     * @param $sLink - link
+     * @param $sTitle - title or use link for the title if omitted
+     * @param $sMaxWidth - try to restrict max width of embed (works in supported embed providers only)
+     */
+    public function getLinkHTML ($sLink, $sTitle = '', $sMaxWidth = '')
+    {
+        // override this function in particular editor class
+    }
+
+    /**
+     * Add css/js files which are needed for embed display and functionality.
+     */
+    public function addJsCss ()
+    {
+        // override this function in particular editor class
+    }
+}
+
+/** @} */

+ 37 - 0
inc/classes/BxDolEmbedQuery.php

@@ -0,0 +1,37 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+/**
+ * Database queries for embed objects.
+ * @see BxDolEmbed
+ */
+class BxDolEmbedQuery extends BxDolDb
+{
+    protected $_aObject;
+
+    public function __construct($aObject)
+    {
+        parent::__construct();
+        $this->_aObject = $aObject;
+    }
+
+    static public function getObject ($sObject)
+    {
+        $oDb = BxDolDb::getInstance();
+        $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_embeds` WHERE `object` = ?", $sObject);
+        $aObject = $oDb->getRow($sQuery);
+        if (!$aObject || !is_array($aObject))
+            return false;
+
+        return $aObject;
+    }
+
+}
+
+/** @} */

+ 0 - 12
inc/classes/BxDolTemplate.php

@@ -480,18 +480,6 @@ class BxDolTemplate extends BxDolFactory implements iBxDolSingleton
             '_copyright'
         ));
 
-        //--- Load Iframely if Key is available and Embed.ly is disabled ---//
-        $sIframely = getParam('sys_iframely_api_key');
-        if(!empty($sIframely)) {
-            $this->addJsSystem(array(
-                bx_proto() . '://cdn.iframe.ly/embed.js?key=' . md5($sIframely)
-            ));
-
-            $this->addJsOption(array(
-            	'sys_iframely_api_key'
-            ));
-        }
-
         //--- Load injection's cache ---//
         if (getParam('sys_db_cache_enable')) {
             $oDb = BxDolDb::getInstance();

+ 1 - 10
inc/js/jquery.dolConverLinks.js

@@ -1,16 +1,7 @@
 (function( $ ){
 	$.fn.dolConverLinks = function(options) {
-
-		if(bx_get_param('sys_iframely_api_key') != '')
-			return this.dolIframely(options);
-
-		return this;
-	};
-
-	$.fn.dolIframely = function(options) {
 		return this.each(function() {
-			iframely.load($(this).get(0));
+			bx_embed_link($(this).get(0));
 		});
 	};
-
 })( jQuery );

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
install/sql/system.sql


+ 21 - 0
modules/boonex/decorous/data/template/system/scripts/BxTemplEmbedEmbedly.php

@@ -0,0 +1,21 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaTemplate UNA Template Classes
+ * @{
+ */
+
+/**
+ * @see BxDolEmbed
+ */
+class BxTemplEmbedEmbedly extends BxBaseEmbedEmbedly
+{
+    public function __construct ($aObject, $oTemplate = false)
+    {
+        parent::__construct ($aObject, $oTemplate);
+    }
+}
+
+/** @} */

+ 21 - 0
modules/boonex/decorous/data/template/system/scripts/BxTemplEmbedIframely.php

@@ -0,0 +1,21 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaTemplate UNA Template Classes
+ * @{
+ */
+
+/**
+ * @see BxDolEmbed
+ */
+class BxTemplEmbedIframely extends BxBaseEmbedIframely
+{
+    public function __construct ($aObject, $oTemplate = false)
+    {
+        parent::__construct ($aObject, $oTemplate);
+    }
+}
+
+/** @} */

+ 21 - 0
modules/boonex/ocean/data/template/system/scripts/BxTemplEmbedEmbedly.php

@@ -0,0 +1,21 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaTemplate UNA Template Classes
+ * @{
+ */
+
+/**
+ * @see BxDolEmbed
+ */
+class BxTemplEmbedEmbedly extends BxBaseEmbedEmbedly
+{
+    public function __construct ($aObject, $oTemplate = false)
+    {
+        parent::__construct ($aObject, $oTemplate);
+    }
+}
+
+/** @} */

+ 21 - 0
modules/boonex/ocean/data/template/system/scripts/BxTemplEmbedIframely.php

@@ -0,0 +1,21 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaTemplate UNA Template Classes
+ * @{
+ */
+
+/**
+ * @see BxDolEmbed
+ */
+class BxTemplEmbedIframely extends BxBaseEmbedIframely
+{
+    public function __construct ($aObject, $oTemplate = false)
+    {
+        parent::__construct ($aObject, $oTemplate);
+    }
+}
+
+/** @} */

+ 21 - 0
modules/boonex/protean/data/template/system/scripts/BxTemplEmbedEmbedly.php

@@ -0,0 +1,21 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaTemplate UNA Template Classes
+ * @{
+ */
+
+/**
+ * @see BxDolEmbed
+ */
+class BxTemplEmbedEmbedly extends BxBaseEmbedEmbedly
+{
+    public function __construct ($aObject, $oTemplate = false)
+    {
+        parent::__construct ($aObject, $oTemplate);
+    }
+}
+
+/** @} */

+ 21 - 0
modules/boonex/protean/data/template/system/scripts/BxTemplEmbedIframely.php

@@ -0,0 +1,21 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaTemplate UNA Template Classes
+ * @{
+ */
+
+/**
+ * @see BxDolEmbed
+ */
+class BxTemplEmbedIframely extends BxBaseEmbedIframely
+{
+    public function __construct ($aObject, $oTemplate = false)
+    {
+        parent::__construct ($aObject, $oTemplate);
+    }
+}
+
+/** @} */

+ 6 - 16
modules/boonex/timeline/classes/BxTimelineTemplate.php

@@ -674,17 +674,12 @@ class BxTimelineTemplate extends BxBaseModNotificationsTemplate
 
         $bTmplVarsEmbed = false;
         $aTmplVarsEmbed = array();
-        $sIframelyKey = $this->_oDb->getParam('sys_iframely_api_key');
-        $sEmbedlyKey = $this->_oDb->getParam('sys_embedly_api_key');
-        $sEmbedlyPattern = $this->_oDb->getParam('sys_embedly_api_pattern');
-        if(!empty($sIframelyKey) || (!empty($sEmbedlyKey) && (empty($sEmbedlyPattern) || preg_match("/" . $sEmbedlyPattern . "/i", $aLink['url'])))) {
+        $oEmbed = BxDolEmbed::getObjectInstance();
+        if($oEmbed) {
             $bTmplVarsEmbed = true;
             $aTmplVarsEmbed = array(
                 'style_prefix' => $sStylePrefix,
-            	'embed' => $this->parseLink($aLink['url'], $aLink['title'], array(
-            		'class' => 'bx-link',
-            		'title' => $aLink['title']
-            	))
+                'embed' => $oEmbed->getLinkHTML($aLink['url'], $aLink['title'], 300),
             );
         }
         else {
@@ -985,17 +980,12 @@ class BxTimelineTemplate extends BxBaseModNotificationsTemplate
             foreach($aContent['links'] as $aLink) {
                 $bTmplVarsEmbed = false;
                 $aTmplVarsEmbed = array();
-                $sIframelyKey = $this->_oDb->getParam('sys_iframely_api_key');
-                $sEmbedlyKey = $this->_oDb->getParam('sys_embedly_api_key');
-                $sEmbedlyPattern = $this->_oDb->getParam('sys_embedly_api_pattern');
-                if(!empty($sIframelyKey) || (!empty($sEmbedlyKey) && (empty($sEmbedlyPattern) || preg_match("/" . $sEmbedlyPattern . "/i", $aLink['url'])))) {
+                $oEmbed = BxDolEmbed::getObjectInstance();
+                if ($oEmbed) {
                     $bTmplVarsEmbed = true;
                     $aTmplVarsEmbed = array(
                         'style_prefix' => $sStylePrefix,
-                    	'embed' => $this->parseLink($aLink['url'], $aLink['title'], array(
-                    		'class' => 'bx-link',
-                    		'title' => $aLink['title']
-                    	))
+                        'embed' => $oEmbed->getLinkHTML($aLink['url'], $aLink['title']),
                     );
                 }
                 else {

+ 0 - 4
modules/boonex/timeline/js/post.js

@@ -149,7 +149,6 @@ BxTimelinePost.prototype.afterFormAttachLinkSubmit = function (oForm, oData)
 
 			var oItem = $(oData.item).hide();
 			$('#' + $this._aHtmlIds['attach_link_form_field']).prepend(oItem).find('#' + oItem.attr('id')).bx_anim('show', $this._sAnimationEffect, $this._sAnimationSpeed, function() {
-				$(this).find('a.bx-link').dolConverLinks();
 			});
 
 	        return;
@@ -230,7 +229,6 @@ BxTimelinePost.prototype.addAttachLink = function(oElement, sUrl) {
 
 			var oItem = $(oData.item).hide();
 			$('#' + $this._aHtmlIds['attach_link_form_field']).prepend(oItem).find('#' + oItem.attr('id')).bx_anim('show', $this._sAnimationEffect, $this._sAnimationSpeed, function() {
-				$(this).find('a.bx-link').dolConverLinks();
 			});
 	    },
 	    'json'
@@ -296,7 +294,6 @@ BxTimelinePost.prototype._onGetPost = function(oData) {
 
 			oItem = bDivider ? oDivider.after(oContent).next('.' + this.sClassItem + ':hidden') : oContent.prependTo(oItems);
 			oItem.bx_anim('show', this._sAnimationEffect, this._iAnimationSpeed, function() {
-				$(this).find('a.bx-link').dolConverLinks();
 				$(this).find('.bx-tl-item-text .bx-tl-content').checkOverflowHeight($this.sSP + '-overflow', function(oElement) {
 					$this.onFindOverflow(oElement);
 				});
@@ -307,7 +304,6 @@ BxTimelinePost.prototype._onGetPost = function(oData) {
 
 		case 'outline':
 			this.prependMasonry(oContent, function(oItems) {
-				$(oItems).find('a.bx-link').dolConverLinks();
 				$(oItems).find('.bx-tl-item-text .bx-tl-content').checkOverflowHeight($this.sSP + '-overflow', function(oElement) {
 					$this.onFindOverflow(oElement);
 				});

+ 4 - 4
modules/boonex/timeline/template/css/post.css

@@ -59,11 +59,11 @@ div.bx-tl-ali-thumbnail img {
 	width: 100px;
 	height: 100px;
 }
-div.bx-tl-ali-embed iframe {
-    width: 100%;
-	height: auto;
+div.bx-tl-ali-embed {
+	max-height: 250px;
+    overflow-y: scroll;
 }
 div.bx-tl-ali-actions .bx-btn {
 	display: inline-block;
 	float: none;
-}
+}

+ 0 - 3
template/_header_js.html

@@ -3,9 +3,6 @@
 
     $(document).ready(function () {
         bx_time('__lang__');
-
-        // init link conversion
-        $('a.bx-link').dolConverLinks();
     });
 
     (function(w) {

+ 4 - 0
template/embedly.html → template/embed_embedly_integration.html

@@ -16,4 +16,8 @@ $(document).ready(function () {
     });
 });
 </bx_if:key>
+function bx_embed_link(e)
+{
+    embedly('card', e);
+}
 </script>

+ 1 - 0
template/embed_embedly_link.html

@@ -0,0 +1 @@
+<a class="embedly-card" data-card-align="left" data-card-controls="0" data-card-recommend="0" data-card-width="__width__" href="__link__" __attrs__>__title__</a>

+ 7 - 0
template/embed_iframely_integration.html

@@ -0,0 +1,7 @@
+<script async charset="utf-8" src="//cdn.iframe.ly/embed.js?api_key=__key__"></script>
+<script>
+function bx_embed_link(e)
+{
+    iframely.load(e);
+}
+</script>

+ 1 - 0
template/embed_iframely_link.html

@@ -0,0 +1 @@
+<a data-iframely-url href="__link__" __attrs__>__title__</a>

+ 61 - 0
template/scripts/BxBaseEmbedEmbedly.php

@@ -0,0 +1,61 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaBaseView UNA Base Representation Classes
+ * @{
+ */
+
+/**
+ * Iframely integration.
+ * @see BxDolEmbed
+ */
+class BxBaseEmbedEmbedly extends BxDolEmbed
+{
+    public function __construct ($aObject, $oTemplate)
+    {
+        parent::__construct ($aObject);
+
+        if ($oTemplate)
+            $this->_oTemplate = $oTemplate;
+        else
+            $this->_oTemplate = BxDolTemplate::getInstance();
+    }
+
+    public function getLinkHTML ($sLink, $sTitle = '', $sMaxWidth = '')
+    {
+        $aAttrs = array(
+            'title' => bx_html_attribute($sTitle),
+        );
+
+        // check for external link
+        if (strncmp(BX_DOL_URL_ROOT, $sLink, strlen(BX_DOL_URL_ROOT)) !== 0) {
+            $aAttrs['target'] = '_blank';
+
+            if (getParam('sys_add_nofollow') == 'on')
+                $aAttrs['rel'] = 'nofollow';
+        }
+
+        return $this->_oTemplate->parseHtmlByName('embed_embedly_link.html', array(
+            'link' => $sLink,
+            'title' => $sTitle,
+            'attrs' => bx_convert_array2attrs($aAttrs),
+            'width' => $sMaxWidth,
+        ));
+    }
+
+    public function addJsCss ()
+    {
+        $sKey = getParam('sys_embedly_api_key');
+
+        return $this->_oTemplate->parseHtmlByName('embed_embedly_integration.html', array(
+            'bx_if:key' => array (
+                'condition' => !empty($sKey),
+                'content' => array('key' => $sKey),
+            )
+        ));
+    }
+}
+
+/** @} */

+ 58 - 0
template/scripts/BxBaseEmbedIframely.php

@@ -0,0 +1,58 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaBaseView UNA Base Representation Classes
+ * @{
+ */
+
+/**
+ * Iframely integration.
+ * @see BxDolEmbed
+ */
+class BxBaseEmbedIframely extends BxDolEmbed
+{
+    public function __construct ($aObject, $oTemplate)
+    {
+        parent::__construct ($aObject);
+
+        if ($oTemplate)
+            $this->_oTemplate = $oTemplate;
+        else
+            $this->_oTemplate = BxDolTemplate::getInstance();
+    }
+
+    public function getLinkHTML ($sLink, $sTitle = '', $sMaxWidth = '')
+    {
+        $aAttrs = array(
+            'title' => bx_html_attribute($sTitle),
+        );
+
+        // check for external link
+        if (strncmp(BX_DOL_URL_ROOT, $sLink, strlen(BX_DOL_URL_ROOT)) !== 0) {
+            $aAttrs['target'] = '_blank';
+
+            if (getParam('sys_add_nofollow') == 'on')
+                $aAttrs['rel'] = 'nofollow';
+        }
+
+        return $this->_oTemplate->parseHtmlByName('embed_iframely_link.html', array(
+            'link' => $sLink,
+            'title' => $sTitle,
+            'attrs' => bx_convert_array2attrs($aAttrs),
+            'width' => $sMaxWidth,
+        ));
+    }
+
+    public function addJsCss ()
+    {
+        $sKey = getParam('sys_iframely_api_key');
+
+        return $this->_oTemplate->parseHtmlByName('embed_iframely_integration.html', array(
+            'key' => $sKey,
+        ));
+    }
+}
+
+/** @} */

+ 8 - 14
template/scripts/BxBaseFunctions.php

@@ -609,20 +609,14 @@ class BxBaseFunctions extends BxDolFactory implements iBxDolSingleton
         return $sContent;
     }
 
-    protected function getInjFooterEmbedly() 
-    {
-        $sKey = getParam('sys_embedly_api_key');
-
-        // TODO: remove this condition when this code is moved to the separate module
-        if (empty($sKey))
-            return '';
-        
-        return $this->_oTemplate->parseHtmlByName('embedly.html', array(
-            'bx_if:key' => array (
-                'condition' => !empty($sKey),
-                'content' => array('key' => $sKey),
-            )
-        ));
+    protected function getInjFooterEmbed() 
+    {    
+        // Load embed files
+        $oEmbed = BxDolEmbed::getObjectInstance(false);
+        if ($oEmbed) 
+            return $oEmbed->addJsCss();
+
+        return '';
     }
 }
 

Некоторые файлы не были показаны из-за большого количества измененных файлов