Beispiel #1
0
 public static function render($html)
 {
     $html = preg_replace('@\\<head(.*?)\\>@si', '<head>', $html, 1);
     // Prepare "html"
     // Replace "doctype"
     preg_match('@(.*?)\\<html>@si', $html, $htmlTagMatch);
     $htmlDoctypeReplace = '';
     if (strstr(strtolower($htmlTagMatch[1]), 'dtd xhtml')) {
         // is xhtml
         $htmlDoctypeReplace = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
     } else {
         if (strstr(strtolower($htmlTagMatch[1]), 'dtd html 4')) {
             // is html4
             $htmlDoctypeReplace = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
         } else {
             if (strstr(strtolower($htmlTagMatch[1]), '<!doctype html')) {
                 //is html5
                 $htmlDoctypeReplace = '<!doctype html>';
             }
         }
     }
     if (!empty($htmlDoctypeReplace)) {
         $html = str_replace($htmlTagMatch[1], $htmlDoctypeReplace, $html);
     }
     // Render
     $doc = phpQuery::newDocument($html);
     //
     $docHtml = $doc->find('html');
     if (!$docHtml->attr('id')) {
         $docHtml->attr('id', 'tidio-editor-page');
     }
     // If "head" dosen't exsits we gonna added ownself
     $eleHead = $doc->find('head');
     if (!$eleHead->length) {
         $headContent = '';
         preg_match('@\\<head\\>(.*?)\\<\\/head\\>@si', $html, $headMatches);
         if (!empty($headMatches[1])) {
             $headContent = $headMatches[1];
         }
         //
         $eleHtml = $doc->find('html');
         $eleHtml->prepend('<head__>' . $headContent . '</head__>');
     }
     // Plugin List
     if (!empty(self::$apiData)) {
         $editedElements = array();
         foreach (self::$apiData['wysiwyg_elements'] as $e) {
             // compare url
             if (!self::compareURL($e)) {
                 continue;
             }
             //
             $ele = null;
             if (!empty($e['data']['selector_source'])) {
                 $ele = $doc->find($e['data']['selector_source']);
             }
             if (!$ele || !$ele->length) {
                 $ele = $doc->find($e['selector']);
             }
             if (!$ele || !$ele->length) {
                 continue;
             }
             //
             if ($e['type'] == 'edit') {
                 $ele->html($e['html']);
             }
             if ($e['type'] == 'delete') {
                 $ele->remove();
             }
             $editedElements[] = $e['id'];
         }
         self::$editedElements = $editedElements;
     }
     // wp - compability mode
     if (class_exists('TidioPluginsScheme')) {
         foreach (TidioPluginsScheme::$insertCode as $e) {
             self::$htmlElements[] = array('placement' => 'prependHead', 'html' => $e);
         }
     }
     // Append JavaScript
     $head = $doc->find('head');
     //
     $head->prepend('<script type="text/javascript" src="//www.tidioelements.com/redirect/' . self::$projectPublicKey . '.js"></script>');
     $head->prepend('<script type="text/javascript">var tidioElementsEditedElements = ' . json_encode(self::$editedElements) . ';</script>');
     //
     foreach (self::$htmlElements as $e) {
         if ($e['placement'] == 'prependHead') {
             $head->prepend($e['html']);
         }
     }
     // Render HTML
     $docHtml = $doc->htmlOuter();
     $docHtml = str_replace('head__', 'head', $docHtml);
     return $docHtml;
 }