/** * Customise the display of posts as they are being rendered by the browser * * @param string $content * @return string */ public function TheContent($content = '') { $searches = array(); $replaces = array(); # Use title as alt where there's a title and an empty alt attribute $searches[] = '/ title="([^"]+)"([^>]*) alt=""/'; $replaces[] = '$2 alt="$1"'; # Remove title where there's also a populated alt attribute $searches[] = '/ title="[^"]+"([^>]* alt="[^"]+")/'; $replaces[] = '$1'; # Remove link to original image, which has no navigation $searches[] = '/<a href="[^"]+.(jpg|gif|png|jpeg)"[^>]*>(<img [^>]*>)<\\/a>/'; $replaces[] = '$2'; # Strip these by providing no replacement text $searches[] = '/ title="[a-z0-9-]*"/'; # remove meaningless title attributes containing filenames $replaces[] = ''; $content = preg_replace($searches, $replaces, $content); $content = str_replace("http://www.stoolball.org.uk", "https://www.stoolball.org.uk", $content); if (is_home()) { # Take out and remember images $content = preg_replace_callback('/(<img[^>]*>)/', array($this, 'ExtractImage'), $content); $strip_these = array('/<h[0-9][^>]*>.*?<\\/h[0-9]>/', '/<p class="wp-caption-text">.*?<\\/p>/', '/<a[^>]* class="more-link[^>]*>.*?<\\/a>/', '/style="width: [0-9]+px;?"/', '/<div[^>]*><\\/div>/'); $content = preg_replace($strip_these, '', $content); # Don't want home page to be too long, so cut off after first para $pos = strpos($content, '</p>'); if ($pos) { $pos = $pos + 4; # length of </p> $content = substr($content, 0, $pos); } # If there were images, put the first one at the start of the text if (count($this->a_matches)) { # Remove unused class, width and height $image = preg_replace('/ (class|width|height)="[A-Za-z0-9-_ ]+"/', '', $this->a_matches[0]); # Try to isolate the src attribute and swop it for the corresponding thumbnail $pos = strpos($image, ' src="'); if ($pos !== false) { $pos = $pos + 6; # move to start to attr value $len = strpos($image, '"', $pos); if ($len !== false) { # Get path to image on server $wordpress_image_folder = $this->settings->GetServerRoot(); if (SiteContext::IsDevelopment()) { $wordpress_image_folder .= "../"; } # on dev setup, WordPress image uploads are outside web root require_once 'Zend/Uri.php'; $uri = Zend_Uri::factory(substr($image, $pos, $len - $pos)); /* @var $uri Zend_Uri_Http */ # Change it to the thumbnail path and see whether the thumbnail exists $thumbnail = $wordpress_image_folder . $uri->getPath(); $pos = strrpos($thumbnail, '.'); if ($pos !== false) { $thumbnail = substr($thumbnail, 0, $pos) . "-150x150" . substr($thumbnail, $pos); if (file_exists($thumbnail)) { # if it does exist, update the original image tag with the thumbnail suffix and size # important to do all these checks because thumbnails don't exist for images smaller than 150px $image = preg_replace('/\\.(jpg|jpeg|gif|png)"/', '-150x150.$1" width="150" height="150"', $image); } } } } # Add image before content $content = $image . $content; } } else { # Increase image width by 10px (caption must be 100%, so space around image must be margin on inner element, not padding on this element) $content = preg_replace_callback('/class="wp-caption([A-Za-z0-9-_ ]*)" style="width: ([0-9]+)px;?"/', array($this, 'ReplaceImageWidth'), $content); # Add extra XHTML around photo captions as hook for CSS $content = preg_replace('/(<p class="wp-caption-text">[^<]*<\\/p>)/', '<div class="photoCaption">$1</div>', $content); } $protector = new EmailAddressProtector($this->settings); $content = $protector->ApplyEmailProtection($content, is_object(AuthenticationManager::GetUser()) and AuthenticationManager::GetUser()->IsSignedIn()); return $content; }