@@ -25,29 +25,29 @@ discard block |
||
| 25 | 25 | // Do something here |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | - private $maxLinksPerPage; |
|
| 28 | + private $maxLinksPerPage; |
|
| 29 | 29 | private $settings = null; |
| 30 | - private $linkCount = 0; |
|
| 30 | + private $linkCount = 0; |
|
| 31 | 31 | private $addLinks = true; |
| 32 | 32 | private $excludeTags = array(); |
| 33 | 33 | private $maxLinks = 0; |
| 34 | 34 | |
| 35 | - public function index(){ |
|
| 35 | + public function index(){ |
|
| 36 | 36 | $this->addAutomatedLinks(); |
| 37 | 37 | |
| 38 | - // If we have a crawl request check the CrawlID so we're sure we didn't hit another SS site running our module |
|
| 39 | - if( $crawl_id = $this->owner->request->getHeader('X-Crawl-Id') ){ |
|
| 40 | - return( $crawl_id == GlobalAutoLinkSettings::get_current()->CrawlID ) |
|
| 38 | + // If we have a crawl request check the CrawlID so we're sure we didn't hit another SS site running our module |
|
| 39 | + if( $crawl_id = $this->owner->request->getHeader('X-Crawl-Id') ){ |
|
| 40 | + return( $crawl_id == GlobalAutoLinkSettings::get_current()->CrawlID ) |
|
| 41 | 41 | ? $this->crawl_response() |
| 42 | 42 | : $this->owner->redirect(SEOTestSiteTreeController::getPermissionDeniedPage()->Link()); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - return array(); |
|
| 46 | - } |
|
| 45 | + return array(); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - private function crawl_response(){ |
|
| 48 | + private function crawl_response(){ |
|
| 49 | 49 | // Encoded version to detect which fields are being used |
| 50 | - $customize = array(); |
|
| 50 | + $customize = array(); |
|
| 51 | 51 | foreach( Config::inst()->get($this->owner->ClassName, 'db') as $field => $type ){ |
| 52 | 52 | if( strtolower( $type ) == 'htmltext' ){ |
| 53 | 53 | $data = ($this->owner->hasMethod($field)) ? $this->owner->$field() : $this->owner->$field; |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | '/<!--(.*?)-->/im', |
| 73 | 73 | ), '', $this->owner->customise($customize)->render()) |
| 74 | 74 | ); |
| 75 | - } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | 77 | /** |
| 78 | 78 | * Get the global settings and check if we should be adding |
@@ -94,13 +94,13 @@ discard block |
||
| 94 | 94 | return $this->settings; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Goes through all the automated link settings and adds |
|
| 99 | - * the links where necessary |
|
| 100 | - * |
|
| 101 | - * @return void |
|
| 102 | - */ |
|
| 103 | - public function addAutomatedLinks(){ |
|
| 97 | + /** |
|
| 98 | + * Goes through all the automated link settings and adds |
|
| 99 | + * the links where necessary |
|
| 100 | + * |
|
| 101 | + * @return void |
|
| 102 | + */ |
|
| 103 | + public function addAutomatedLinks(){ |
|
| 104 | 104 | if( GlobalAutoLinkSettings::$enabled && $this->owner->class != 'RedirectorPage' ) { |
| 105 | 105 | $this->getSettings(); |
| 106 | 106 | if( !$this->addLinks ) return; |
@@ -132,60 +132,60 @@ discard block |
||
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | 134 | } |
| 135 | - } |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * Parse the provided field and add the necessary links |
|
| 139 | - * |
|
| 140 | - * @param DOMDocument $html |
|
| 137 | + /** |
|
| 138 | + * Parse the provided field and add the necessary links |
|
| 139 | + * |
|
| 140 | + * @param DOMDocument $html |
|
| 141 | 141 | * @param String $field |
| 142 | - * @return string |
|
| 143 | - */ |
|
| 144 | - private function parseField( DOMDocument $html, $field ){ |
|
| 145 | - $this->owner->extend( 'beforeParseField', $html, $field ); |
|
| 146 | - |
|
| 147 | - // Remove Tags from Content we wown't be using |
|
| 148 | - $excluded = array(); |
|
| 149 | - foreach( $this->excludeTags as $eTag ){ |
|
| 150 | - while( $tags = $html->getElementsByTagName( $eTag ) ){ |
|
| 151 | - if( !$tags->length ) break 1; |
|
| 152 | - $tag = $tags->item(0); |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + private function parseField( DOMDocument $html, $field ){ |
|
| 145 | + $this->owner->extend( 'beforeParseField', $html, $field ); |
|
| 146 | + |
|
| 147 | + // Remove Tags from Content we wown't be using |
|
| 148 | + $excluded = array(); |
|
| 149 | + foreach( $this->excludeTags as $eTag ){ |
|
| 150 | + while( $tags = $html->getElementsByTagName( $eTag ) ){ |
|
| 151 | + if( !$tags->length ) break 1; |
|
| 152 | + $tag = $tags->item(0); |
|
| 153 | 153 | $value = $html->saveHTML( $tag ); |
| 154 | 154 | $key = (string) crc32( $value ); |
| 155 | 155 | |
| 156 | - // Convert back children nodes of this node if they were already hashed |
|
| 157 | - $excluded[$key] = str_replace( array_keys( $excluded ), array_values( $excluded ), $value ); |
|
| 156 | + // Convert back children nodes of this node if they were already hashed |
|
| 157 | + $excluded[$key] = str_replace( array_keys( $excluded ), array_values( $excluded ), $value ); |
|
| 158 | 158 | |
| 159 | 159 | $tag->parentNode->replaceChild( $html->createTextNode( $key ), $tag ); |
| 160 | - } |
|
| 161 | - } |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | 163 | $body = (string)$html->saveHTML( $html->getElementsByTagName('body')->item(0) ); |
| 164 | 164 | $content = preg_replace( array( '/\<body\>/is', '/\<\/body\>/is' ), '', $body, 1 ); |
| 165 | 165 | |
| 166 | - // Create the links |
|
| 167 | - $links = AutomatedLink::get()->sort('Priority'); |
|
| 168 | - foreach( $links as $link ){ |
|
| 169 | - // Check if self-linking is allowed and if current pagetype is allowed |
|
| 170 | - if( !$link->canBeAdded( $this->owner, $field ) ) continue; |
|
| 166 | + // Create the links |
|
| 167 | + $links = AutomatedLink::get()->sort('Priority'); |
|
| 168 | + foreach( $links as $link ){ |
|
| 169 | + // Check if self-linking is allowed and if current pagetype is allowed |
|
| 170 | + if( !$link->canBeAdded( $this->owner, $field ) ) continue; |
|
| 171 | 171 | |
| 172 | - $max = (int) ( $link->MaxLinksPerPage > 0 ) ? $link->MaxLinksPerPage : PHP_INT_MAX; |
|
| 172 | + $max = (int) ( $link->MaxLinksPerPage > 0 ) ? $link->MaxLinksPerPage : PHP_INT_MAX; |
|
| 173 | 173 | $escape = (string) preg_quote( $link->Phrase, '/' ); |
| 174 | - $regex = (string) ( $link->CaseSensitive ) ? "/(\b{$escape}\b)/" : "/(\b{$escape}\b)/i"; |
|
| 174 | + $regex = (string) ( $link->CaseSensitive ) ? "/(\b{$escape}\b)/" : "/(\b{$escape}\b)/i"; |
|
| 175 | 175 | |
| 176 | 176 | // Count the matches |
| 177 | 177 | preg_match_all( $regex, $content, $count ); |
| 178 | 178 | $count = ( is_array( $count ) && isset( $count[0] ) ) ? count( $count[0] ) : 0; |
| 179 | 179 | if( $count < 1 ) continue; |
| 180 | 180 | |
| 181 | - if( isset( $this->maxLinksPerPage[ $link->ID ] ) ) |
|
| 182 | - $max -= $this->maxLinksPerPage[ $link->ID ]; |
|
| 183 | - else |
|
| 184 | - $this->maxLinksPerPage[ $link->ID ] = 0; |
|
| 181 | + if( isset( $this->maxLinksPerPage[ $link->ID ] ) ) |
|
| 182 | + $max -= $this->maxLinksPerPage[ $link->ID ]; |
|
| 183 | + else |
|
| 184 | + $this->maxLinksPerPage[ $link->ID ] = 0; |
|
| 185 | 185 | |
| 186 | - for( $x = 0; $x < $count; $x++ ){ |
|
| 187 | - // Stop adding links if we reached the link or page limit |
|
| 188 | - if( $x >= $max || $this->linkCount >= $this->maxLinks ) break; |
|
| 186 | + for( $x = 0; $x < $count; $x++ ){ |
|
| 187 | + // Stop adding links if we reached the link or page limit |
|
| 188 | + if( $x >= $max || $this->linkCount >= $this->maxLinks ) break; |
|
| 189 | 189 | |
| 190 | 190 | // Check if there is anything else to replace else stop |
| 191 | 191 | preg_match( $regex, $content, $match ); |
@@ -195,18 +195,18 @@ discard block |
||
| 195 | 195 | $key = (string) crc32( $html ); |
| 196 | 196 | $excluded[ $key ] = (string) $html; |
| 197 | 197 | |
| 198 | - $content = preg_replace( $regex, $key, $content, 1 ); |
|
| 199 | - $this->linkCount++; |
|
| 198 | + $content = preg_replace( $regex, $key, $content, 1 ); |
|
| 199 | + $this->linkCount++; |
|
| 200 | 200 | $this->maxLinksPerPage[ $link->ID ]++; |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | - // Stop Adding links if we reached the page limit |
|
| 204 | - if( $this->linkCount >= $this->maxLinks ) break; |
|
| 205 | - } |
|
| 203 | + // Stop Adding links if we reached the page limit |
|
| 204 | + if( $this->linkCount >= $this->maxLinks ) break; |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | - // Re-add the excluded Tags |
|
| 208 | - $content = str_replace( array_keys( $excluded ), array_values( $excluded ), $content ); |
|
| 207 | + // Re-add the excluded Tags |
|
| 208 | + $content = str_replace( array_keys( $excluded ), array_values( $excluded ), $content ); |
|
| 209 | 209 | |
| 210 | - return $content; |
|
| 211 | - } |
|
| 210 | + return $content; |
|
| 211 | + } |
|
| 212 | 212 | } |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | * |
| 11 | 11 | * @see AutomatedLink |
| 12 | 12 | */ |
| 13 | -class SEOToolboxControllerExtension extends Extension{ |
|
| 13 | +class SEOToolboxControllerExtension extends Extension { |
|
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | 16 | * In here we can perform actions before the here mentioned $field |
@@ -21,23 +21,23 @@ discard block |
||
| 21 | 21 | * @param DOMDocument $html |
| 22 | 22 | * @param $field |
| 23 | 23 | */ |
| 24 | - public function beforeParseField(DOMDocument $html, $field){ |
|
| 24 | + public function beforeParseField(DOMDocument $html, $field) { |
|
| 25 | 25 | // Do something here |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | private $maxLinksPerPage; |
| 29 | 29 | private $settings = null; |
| 30 | - private $linkCount = 0; |
|
| 30 | + private $linkCount = 0; |
|
| 31 | 31 | private $addLinks = true; |
| 32 | 32 | private $excludeTags = array(); |
| 33 | 33 | private $maxLinks = 0; |
| 34 | 34 | |
| 35 | - public function index(){ |
|
| 35 | + public function index() { |
|
| 36 | 36 | $this->addAutomatedLinks(); |
| 37 | 37 | |
| 38 | 38 | // If we have a crawl request check the CrawlID so we're sure we didn't hit another SS site running our module |
| 39 | - if( $crawl_id = $this->owner->request->getHeader('X-Crawl-Id') ){ |
|
| 40 | - return( $crawl_id == GlobalAutoLinkSettings::get_current()->CrawlID ) |
|
| 39 | + if ($crawl_id = $this->owner->request->getHeader('X-Crawl-Id')) { |
|
| 40 | + return($crawl_id == GlobalAutoLinkSettings::get_current()->CrawlID) |
|
| 41 | 41 | ? $this->crawl_response() |
| 42 | 42 | : $this->owner->redirect(SEOTestSiteTreeController::getPermissionDeniedPage()->Link()); |
| 43 | 43 | } |
@@ -45,13 +45,13 @@ discard block |
||
| 45 | 45 | return array(); |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - private function crawl_response(){ |
|
| 48 | + private function crawl_response() { |
|
| 49 | 49 | // Encoded version to detect which fields are being used |
| 50 | 50 | $customize = array(); |
| 51 | - foreach( Config::inst()->get($this->owner->ClassName, 'db') as $field => $type ){ |
|
| 52 | - if( strtolower( $type ) == 'htmltext' ){ |
|
| 51 | + foreach (Config::inst()->get($this->owner->ClassName, 'db') as $field => $type) { |
|
| 52 | + if (strtolower($type) == 'htmltext') { |
|
| 53 | 53 | $data = ($this->owner->hasMethod($field)) ? $this->owner->$field() : $this->owner->$field; |
| 54 | - if( !$data ) continue; |
|
| 54 | + if (!$data) continue; |
|
| 55 | 55 | $tmp = new HTMLText('tmp'); |
| 56 | 56 | $tmp->setValue($data); |
| 57 | 57 | $data = base64_encode($tmp->forTemplate()); |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | } |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - if( in_array($this->owner->ClassName, ClassInfo::subclassesFor('ErrorPage')) ){ |
|
| 62 | + if (in_array($this->owner->ClassName, ClassInfo::subclassesFor('ErrorPage'))) { |
|
| 63 | 63 | header("HTTP/1.0 405 Instance of ErrorPage"); |
| 64 | 64 | die(); |
| 65 | 65 | } |
@@ -80,13 +80,13 @@ discard block |
||
| 80 | 80 | * |
| 81 | 81 | * @return GlobalAutoLinkSettings |
| 82 | 82 | */ |
| 83 | - private function getSettings(){ |
|
| 84 | - if( $this->settings === null ) { |
|
| 83 | + private function getSettings() { |
|
| 84 | + if ($this->settings === null) { |
|
| 85 | 85 | $this->settings = GlobalAutoLinkSettings::get_current(); |
| 86 | 86 | if (!$this->settings) return $this->addLinks = false; |
| 87 | 87 | |
| 88 | - $this->excludeTags = (array)$this->settings->ExcludeTags(); |
|
| 89 | - $this->maxLinks = (int)($this->settings->MaxLinksPerPage) ? $this->settings->MaxLinksPerPage : PHP_INT_MAX; |
|
| 88 | + $this->excludeTags = (array) $this->settings->ExcludeTags(); |
|
| 89 | + $this->maxLinks = (int) ($this->settings->MaxLinksPerPage) ? $this->settings->MaxLinksPerPage : PHP_INT_MAX; |
|
| 90 | 90 | |
| 91 | 91 | if (!in_array($this->owner->ClassName, $this->settings->AllowedIn())) $this->addLinks = false; |
| 92 | 92 | } |
@@ -100,33 +100,33 @@ discard block |
||
| 100 | 100 | * |
| 101 | 101 | * @return void |
| 102 | 102 | */ |
| 103 | - public function addAutomatedLinks(){ |
|
| 104 | - if( GlobalAutoLinkSettings::$enabled && $this->owner->class != 'RedirectorPage' ) { |
|
| 103 | + public function addAutomatedLinks() { |
|
| 104 | + if (GlobalAutoLinkSettings::$enabled && $this->owner->class != 'RedirectorPage') { |
|
| 105 | 105 | $this->getSettings(); |
| 106 | - if( !$this->addLinks ) return; |
|
| 106 | + if (!$this->addLinks) return; |
|
| 107 | 107 | |
| 108 | - foreach( $this->getSettings()->IncludeInFields() as $field ){ |
|
| 108 | + foreach ($this->getSettings()->IncludeInFields() as $field) { |
|
| 109 | 109 | // Check that the field provided by user exists in this object, is of type HTMLText and has content |
| 110 | - if( AutomatedLink::isFieldParsable( $this->owner->data(), $field ) ){ |
|
| 110 | + if (AutomatedLink::isFieldParsable($this->owner->data(), $field)) { |
|
| 111 | 111 | |
| 112 | 112 | // Create dummy object so we can parse the HTML |
| 113 | - $dummy = new HTMLText( $field ); |
|
| 114 | - $dummy->setValue( $this->owner->$field ); |
|
| 113 | + $dummy = new HTMLText($field); |
|
| 114 | + $dummy->setValue($this->owner->$field); |
|
| 115 | 115 | // Create DOMDocument Object |
| 116 | - $content = mb_convert_encoding( $dummy->forTemplate(), 'html-entities', GlobalAutoLinkSettings::$encoding ); |
|
| 116 | + $content = mb_convert_encoding($dummy->forTemplate(), 'html-entities', GlobalAutoLinkSettings::$encoding); |
|
| 117 | 117 | |
| 118 | - if( class_exists( 'HTML5_Parser' ) ){ |
|
| 119 | - $dom = HTML5_Parser::parse( $content ); |
|
| 120 | - }else{ |
|
| 118 | + if (class_exists('HTML5_Parser')) { |
|
| 119 | + $dom = HTML5_Parser::parse($content); |
|
| 120 | + }else { |
|
| 121 | 121 | $dom = new DOMDocument(); |
| 122 | - $dom->loadHTML( $content ); |
|
| 122 | + $dom->loadHTML($content); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Check current link count and if it's already exceeded do nothing |
| 126 | - $this->linkCount += (int) $dom->getElementsByTagName( 'a' )->length; |
|
| 127 | - if( $this->linkCount >= $this->maxLinks ) return; |
|
| 126 | + $this->linkCount += (int) $dom->getElementsByTagName('a')->length; |
|
| 127 | + if ($this->linkCount >= $this->maxLinks) return; |
|
| 128 | 128 | |
| 129 | - $parsed = $this->parseField( $dom, $field ); |
|
| 129 | + $parsed = $this->parseField($dom, $field); |
|
| 130 | 130 | $this->owner->data()->$field = $parsed; |
| 131 | 131 | $this->owner->$field = $parsed; |
| 132 | 132 | } |
@@ -141,71 +141,71 @@ discard block |
||
| 141 | 141 | * @param String $field |
| 142 | 142 | * @return string |
| 143 | 143 | */ |
| 144 | - private function parseField( DOMDocument $html, $field ){ |
|
| 145 | - $this->owner->extend( 'beforeParseField', $html, $field ); |
|
| 144 | + private function parseField(DOMDocument $html, $field) { |
|
| 145 | + $this->owner->extend('beforeParseField', $html, $field); |
|
| 146 | 146 | |
| 147 | 147 | // Remove Tags from Content we wown't be using |
| 148 | 148 | $excluded = array(); |
| 149 | - foreach( $this->excludeTags as $eTag ){ |
|
| 150 | - while( $tags = $html->getElementsByTagName( $eTag ) ){ |
|
| 151 | - if( !$tags->length ) break 1; |
|
| 152 | - $tag = $tags->item(0); |
|
| 153 | - $value = $html->saveHTML( $tag ); |
|
| 154 | - $key = (string) crc32( $value ); |
|
| 149 | + foreach ($this->excludeTags as $eTag) { |
|
| 150 | + while ($tags = $html->getElementsByTagName($eTag)) { |
|
| 151 | + if (!$tags->length) break 1; |
|
| 152 | + $tag = $tags->item(0); |
|
| 153 | + $value = $html->saveHTML($tag); |
|
| 154 | + $key = (string) crc32($value); |
|
| 155 | 155 | |
| 156 | 156 | // Convert back children nodes of this node if they were already hashed |
| 157 | - $excluded[$key] = str_replace( array_keys( $excluded ), array_values( $excluded ), $value ); |
|
| 157 | + $excluded[$key] = str_replace(array_keys($excluded), array_values($excluded), $value); |
|
| 158 | 158 | |
| 159 | - $tag->parentNode->replaceChild( $html->createTextNode( $key ), $tag ); |
|
| 159 | + $tag->parentNode->replaceChild($html->createTextNode($key), $tag); |
|
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - $body = (string)$html->saveHTML( $html->getElementsByTagName('body')->item(0) ); |
|
| 164 | - $content = preg_replace( array( '/\<body\>/is', '/\<\/body\>/is' ), '', $body, 1 ); |
|
| 163 | + $body = (string) $html->saveHTML($html->getElementsByTagName('body')->item(0)); |
|
| 164 | + $content = preg_replace(array('/\<body\>/is', '/\<\/body\>/is'), '', $body, 1); |
|
| 165 | 165 | |
| 166 | 166 | // Create the links |
| 167 | 167 | $links = AutomatedLink::get()->sort('Priority'); |
| 168 | - foreach( $links as $link ){ |
|
| 168 | + foreach ($links as $link) { |
|
| 169 | 169 | // Check if self-linking is allowed and if current pagetype is allowed |
| 170 | - if( !$link->canBeAdded( $this->owner, $field ) ) continue; |
|
| 170 | + if (!$link->canBeAdded($this->owner, $field)) continue; |
|
| 171 | 171 | |
| 172 | - $max = (int) ( $link->MaxLinksPerPage > 0 ) ? $link->MaxLinksPerPage : PHP_INT_MAX; |
|
| 173 | - $escape = (string) preg_quote( $link->Phrase, '/' ); |
|
| 174 | - $regex = (string) ( $link->CaseSensitive ) ? "/(\b{$escape}\b)/" : "/(\b{$escape}\b)/i"; |
|
| 172 | + $max = (int) ($link->MaxLinksPerPage > 0) ? $link->MaxLinksPerPage : PHP_INT_MAX; |
|
| 173 | + $escape = (string) preg_quote($link->Phrase, '/'); |
|
| 174 | + $regex = (string) ($link->CaseSensitive) ? "/(\b{$escape}\b)/" : "/(\b{$escape}\b)/i"; |
|
| 175 | 175 | |
| 176 | 176 | // Count the matches |
| 177 | - preg_match_all( $regex, $content, $count ); |
|
| 178 | - $count = ( is_array( $count ) && isset( $count[0] ) ) ? count( $count[0] ) : 0; |
|
| 179 | - if( $count < 1 ) continue; |
|
| 177 | + preg_match_all($regex, $content, $count); |
|
| 178 | + $count = (is_array($count) && isset($count[0])) ? count($count[0]) : 0; |
|
| 179 | + if ($count < 1) continue; |
|
| 180 | 180 | |
| 181 | - if( isset( $this->maxLinksPerPage[ $link->ID ] ) ) |
|
| 182 | - $max -= $this->maxLinksPerPage[ $link->ID ]; |
|
| 181 | + if (isset($this->maxLinksPerPage[$link->ID])) |
|
| 182 | + $max -= $this->maxLinksPerPage[$link->ID]; |
|
| 183 | 183 | else |
| 184 | - $this->maxLinksPerPage[ $link->ID ] = 0; |
|
| 184 | + $this->maxLinksPerPage[$link->ID] = 0; |
|
| 185 | 185 | |
| 186 | - for( $x = 0; $x < $count; $x++ ){ |
|
| 186 | + for ($x = 0; $x < $count; $x++) { |
|
| 187 | 187 | // Stop adding links if we reached the link or page limit |
| 188 | - if( $x >= $max || $this->linkCount >= $this->maxLinks ) break; |
|
| 188 | + if ($x >= $max || $this->linkCount >= $this->maxLinks) break; |
|
| 189 | 189 | |
| 190 | 190 | // Check if there is anything else to replace else stop |
| 191 | - preg_match( $regex, $content, $match ); |
|
| 192 | - if( !is_array( $match ) || !count( $match ) ) break; |
|
| 191 | + preg_match($regex, $content, $match); |
|
| 192 | + if (!is_array($match) || !count($match)) break; |
|
| 193 | 193 | |
| 194 | - if( !$html = (string) $link->getHTML( $match[0] ) ) continue; |
|
| 195 | - $key = (string) crc32( $html ); |
|
| 196 | - $excluded[ $key ] = (string) $html; |
|
| 194 | + if (!$html = (string) $link->getHTML($match[0])) continue; |
|
| 195 | + $key = (string) crc32($html); |
|
| 196 | + $excluded[$key] = (string) $html; |
|
| 197 | 197 | |
| 198 | - $content = preg_replace( $regex, $key, $content, 1 ); |
|
| 198 | + $content = preg_replace($regex, $key, $content, 1); |
|
| 199 | 199 | $this->linkCount++; |
| 200 | - $this->maxLinksPerPage[ $link->ID ]++; |
|
| 200 | + $this->maxLinksPerPage[$link->ID]++; |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | // Stop Adding links if we reached the page limit |
| 204 | - if( $this->linkCount >= $this->maxLinks ) break; |
|
| 204 | + if ($this->linkCount >= $this->maxLinks) break; |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | // Re-add the excluded Tags |
| 208 | - $content = str_replace( array_keys( $excluded ), array_values( $excluded ), $content ); |
|
| 208 | + $content = str_replace(array_keys($excluded), array_values($excluded), $content); |
|
| 209 | 209 | |
| 210 | 210 | return $content; |
| 211 | 211 | } |
@@ -51,7 +51,9 @@ discard block |
||
| 51 | 51 | foreach( Config::inst()->get($this->owner->ClassName, 'db') as $field => $type ){ |
| 52 | 52 | if( strtolower( $type ) == 'htmltext' ){ |
| 53 | 53 | $data = ($this->owner->hasMethod($field)) ? $this->owner->$field() : $this->owner->$field; |
| 54 | - if( !$data ) continue; |
|
| 54 | + if( !$data ) { |
|
| 55 | + continue; |
|
| 56 | + } |
|
| 55 | 57 | $tmp = new HTMLText('tmp'); |
| 56 | 58 | $tmp->setValue($data); |
| 57 | 59 | $data = base64_encode($tmp->forTemplate()); |
@@ -83,12 +85,16 @@ discard block |
||
| 83 | 85 | private function getSettings(){ |
| 84 | 86 | if( $this->settings === null ) { |
| 85 | 87 | $this->settings = GlobalAutoLinkSettings::get_current(); |
| 86 | - if (!$this->settings) return $this->addLinks = false; |
|
| 88 | + if (!$this->settings) { |
|
| 89 | + return $this->addLinks = false; |
|
| 90 | + } |
|
| 87 | 91 | |
| 88 | 92 | $this->excludeTags = (array)$this->settings->ExcludeTags(); |
| 89 | 93 | $this->maxLinks = (int)($this->settings->MaxLinksPerPage) ? $this->settings->MaxLinksPerPage : PHP_INT_MAX; |
| 90 | 94 | |
| 91 | - if (!in_array($this->owner->ClassName, $this->settings->AllowedIn())) $this->addLinks = false; |
|
| 95 | + if (!in_array($this->owner->ClassName, $this->settings->AllowedIn())) { |
|
| 96 | + $this->addLinks = false; |
|
| 97 | + } |
|
| 92 | 98 | } |
| 93 | 99 | |
| 94 | 100 | return $this->settings; |
@@ -103,7 +109,9 @@ discard block |
||
| 103 | 109 | public function addAutomatedLinks(){ |
| 104 | 110 | if( GlobalAutoLinkSettings::$enabled && $this->owner->class != 'RedirectorPage' ) { |
| 105 | 111 | $this->getSettings(); |
| 106 | - if( !$this->addLinks ) return; |
|
| 112 | + if( !$this->addLinks ) { |
|
| 113 | + return; |
|
| 114 | + } |
|
| 107 | 115 | |
| 108 | 116 | foreach( $this->getSettings()->IncludeInFields() as $field ){ |
| 109 | 117 | // Check that the field provided by user exists in this object, is of type HTMLText and has content |
@@ -117,14 +125,16 @@ discard block |
||
| 117 | 125 | |
| 118 | 126 | if( class_exists( 'HTML5_Parser' ) ){ |
| 119 | 127 | $dom = HTML5_Parser::parse( $content ); |
| 120 | - }else{ |
|
| 128 | + } else{ |
|
| 121 | 129 | $dom = new DOMDocument(); |
| 122 | 130 | $dom->loadHTML( $content ); |
| 123 | 131 | } |
| 124 | 132 | |
| 125 | 133 | // Check current link count and if it's already exceeded do nothing |
| 126 | 134 | $this->linkCount += (int) $dom->getElementsByTagName( 'a' )->length; |
| 127 | - if( $this->linkCount >= $this->maxLinks ) return; |
|
| 135 | + if( $this->linkCount >= $this->maxLinks ) { |
|
| 136 | + return; |
|
| 137 | + } |
|
| 128 | 138 | |
| 129 | 139 | $parsed = $this->parseField( $dom, $field ); |
| 130 | 140 | $this->owner->data()->$field = $parsed; |
@@ -148,7 +158,9 @@ discard block |
||
| 148 | 158 | $excluded = array(); |
| 149 | 159 | foreach( $this->excludeTags as $eTag ){ |
| 150 | 160 | while( $tags = $html->getElementsByTagName( $eTag ) ){ |
| 151 | - if( !$tags->length ) break 1; |
|
| 161 | + if( !$tags->length ) { |
|
| 162 | + break 1; |
|
| 163 | + } |
|
| 152 | 164 | $tag = $tags->item(0); |
| 153 | 165 | $value = $html->saveHTML( $tag ); |
| 154 | 166 | $key = (string) crc32( $value ); |
@@ -167,7 +179,9 @@ discard block |
||
| 167 | 179 | $links = AutomatedLink::get()->sort('Priority'); |
| 168 | 180 | foreach( $links as $link ){ |
| 169 | 181 | // Check if self-linking is allowed and if current pagetype is allowed |
| 170 | - if( !$link->canBeAdded( $this->owner, $field ) ) continue; |
|
| 182 | + if( !$link->canBeAdded( $this->owner, $field ) ) { |
|
| 183 | + continue; |
|
| 184 | + } |
|
| 171 | 185 | |
| 172 | 186 | $max = (int) ( $link->MaxLinksPerPage > 0 ) ? $link->MaxLinksPerPage : PHP_INT_MAX; |
| 173 | 187 | $escape = (string) preg_quote( $link->Phrase, '/' ); |
@@ -176,22 +190,31 @@ discard block |
||
| 176 | 190 | // Count the matches |
| 177 | 191 | preg_match_all( $regex, $content, $count ); |
| 178 | 192 | $count = ( is_array( $count ) && isset( $count[0] ) ) ? count( $count[0] ) : 0; |
| 179 | - if( $count < 1 ) continue; |
|
| 193 | + if( $count < 1 ) { |
|
| 194 | + continue; |
|
| 195 | + } |
|
| 180 | 196 | |
| 181 | - if( isset( $this->maxLinksPerPage[ $link->ID ] ) ) |
|
| 182 | - $max -= $this->maxLinksPerPage[ $link->ID ]; |
|
| 183 | - else |
|
| 184 | - $this->maxLinksPerPage[ $link->ID ] = 0; |
|
| 197 | + if( isset( $this->maxLinksPerPage[ $link->ID ] ) ) { |
|
| 198 | + $max -= $this->maxLinksPerPage[ $link->ID ]; |
|
| 199 | + } else { |
|
| 200 | + $this->maxLinksPerPage[ $link->ID ] = 0; |
|
| 201 | + } |
|
| 185 | 202 | |
| 186 | 203 | for( $x = 0; $x < $count; $x++ ){ |
| 187 | 204 | // Stop adding links if we reached the link or page limit |
| 188 | - if( $x >= $max || $this->linkCount >= $this->maxLinks ) break; |
|
| 205 | + if( $x >= $max || $this->linkCount >= $this->maxLinks ) { |
|
| 206 | + break; |
|
| 207 | + } |
|
| 189 | 208 | |
| 190 | 209 | // Check if there is anything else to replace else stop |
| 191 | 210 | preg_match( $regex, $content, $match ); |
| 192 | - if( !is_array( $match ) || !count( $match ) ) break; |
|
| 211 | + if( !is_array( $match ) || !count( $match ) ) { |
|
| 212 | + break; |
|
| 213 | + } |
|
| 193 | 214 | |
| 194 | - if( !$html = (string) $link->getHTML( $match[0] ) ) continue; |
|
| 215 | + if( !$html = (string) $link->getHTML( $match[0] ) ) { |
|
| 216 | + continue; |
|
| 217 | + } |
|
| 195 | 218 | $key = (string) crc32( $html ); |
| 196 | 219 | $excluded[ $key ] = (string) $html; |
| 197 | 220 | |
@@ -201,7 +224,9 @@ discard block |
||
| 201 | 224 | } |
| 202 | 225 | |
| 203 | 226 | // Stop Adding links if we reached the page limit |
| 204 | - if( $this->linkCount >= $this->maxLinks ) break; |
|
| 227 | + if( $this->linkCount >= $this->maxLinks ) { |
|
| 228 | + break; |
|
| 229 | + } |
|
| 205 | 230 | } |
| 206 | 231 | |
| 207 | 232 | // Re-add the excluded Tags |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | * Automated Link Report is a report that lists all automated |
| 9 | 9 | * links and how they affect the website |
| 10 | 10 | */ |
| 11 | -class AutomatedLinkReport extends SS_Report{ |
|
| 11 | +class AutomatedLinkReport extends SS_Report { |
|
| 12 | 12 | |
| 13 | 13 | protected $title = 'Automated Link Report'; |
| 14 | 14 | protected $description = 'Shows a list of all automated links and how they affect the site'; |
@@ -16,11 +16,11 @@ discard block |
||
| 16 | 16 | // Configs |
| 17 | 17 | private static $run_in_realtime = true; |
| 18 | 18 | |
| 19 | - public function Title(){ |
|
| 19 | + public function Title() { |
|
| 20 | 20 | return (string) $this->title; |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | - public function columns(){ |
|
| 23 | + public function columns() { |
|
| 24 | 24 | return array( |
| 25 | 25 | 'Title' => array( |
| 26 | 26 | 'title' => 'Page Title', |
@@ -34,8 +34,8 @@ discard block |
||
| 34 | 34 | ); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - public function sourceRecords( $params, $sort, $limit ){ |
|
| 38 | - if( !Config::inst()->get( $this->class, 'run_in_realtime' ) ) { |
|
| 37 | + public function sourceRecords($params, $sort, $limit) { |
|
| 38 | + if (!Config::inst()->get($this->class, 'run_in_realtime')) { |
|
| 39 | 39 | return AutomatedLinkPageResult::get(); |
| 40 | 40 | } |
| 41 | 41 | |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | $gridConfig->removeComponentsByType('GridFieldPrintButton'); |
| 54 | 54 | $gridConfig->removeComponentsByType('GridFieldExportButton'); |
| 55 | 55 | |
| 56 | - $gridConfig->addComponents ( |
|
| 56 | + $gridConfig->addComponents( |
|
| 57 | 57 | new GridFieldPrintAllAutomatedLinksButton('buttons-after-left'), |
| 58 | 58 | new GridFieldExportAllAutomatedLinksButton('buttons-after-left') |
| 59 | 59 | ); |