Skip to content

Commit

Permalink
Move function cp_get_markdown_contents to new Helpers trait (#26)
Browse files Browse the repository at this point in the history
Co-authored-by: xxsimoxx <simone@gieffeedizioni.it>
  • Loading branch information
xxsimoxx and xxsimoxx authored Apr 30, 2024
1 parent 35dc606 commit 104df17
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
36 changes: 36 additions & 0 deletions classes/Helpers.trait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace ClassicPress\Directory;

trait Helpers
{

/**
* Get all substrings within text that are found between two other, specified strings
*
* Avoids parsing HTML with regex
*
* Returns an array
*
* See https://stackoverflow.com/a/27078384
*/
function get_markdown_contents( $str, $startDelimiter, $endDelimiter ) {
$contents = [];
$startDelimiterLength = strlen( $startDelimiter );
$endDelimiterLength = strlen( $endDelimiter );
$startFrom = $contentStart = $contentEnd = 0;

while ( $contentStart = strpos( $str, $startDelimiter, $startFrom ) ) {
$contentStart += $startDelimiterLength;
$contentEnd = strpos( $str, $endDelimiter, $contentStart );
if ( $contentEnd === false ) {
break;
}
$contents[] = substr( $str, $contentStart, $contentEnd - $contentStart );
$startFrom = $contentEnd + $endDelimiterLength;
}

return $contents;
}

}
3 changes: 2 additions & 1 deletion classes/PluginInstall.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class PluginInstall
{
use Helpers;

private $local_cp_plugins = false;

Expand Down Expand Up @@ -385,7 +386,7 @@ public function render_menu()
foreach ( $plugins as $plugin ) {
$slug = $plugin['meta']['slug'];
$content = $plugin['content']['rendered'];
$markdown_contents = cp_get_markdown_contents( $content, '<div class="markdown-heading">', '</div>' );
$markdown_contents = self::get_markdown_contents( $content, '<div class="markdown-heading">', '</div>' );
foreach ( $markdown_contents as $markdown_content ) {
$content = str_replace( '<div class="markdown-heading">' . $markdown_content . '</div>', $markdown_content, $content );
}
Expand Down
4 changes: 3 additions & 1 deletion classes/ThemeInstall.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
class ThemeInstall
{

use Helpers;

private $local_cp_themes = false;

private $page = null;
Expand Down Expand Up @@ -371,7 +373,7 @@ public function render_menu()
foreach ($themes as $theme) {
$slug = $theme['meta']['slug'];
$content = $theme['content']['rendered'];
$markdown_contents = cp_get_markdown_contents( $content, '<div class="markdown-heading">', '</div>' );
$markdown_contents = self::get_markdown_contents( $content, '<div class="markdown-heading">', '</div>' );
foreach ( $markdown_contents as $markdown_content ) {
$content = str_replace( '<div class="markdown-heading">' . $markdown_content . '</div>', $markdown_content, $content );
}
Expand Down
3 changes: 3 additions & 0 deletions classicpress-directory-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
require_once 'includes/constants.php';
require_once 'includes/functions.php';

// Load Helpers trait.
require_once 'classes/Helpers.trait.php';

// Load Plugin Update functionality class.
require_once 'classes/PluginUpdate.class.php';
$plugin_update = new PluginUpdate();
Expand Down
28 changes: 0 additions & 28 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,3 @@
if (!defined('ABSPATH')) {
die();
}

/**
* Get all substrings within text that are found between two other, specified strings
*
* Avoids parsing HTML with regex
*
* Returns an array
*
* See https://stackoverflow.com/a/27078384
*/
function cp_get_markdown_contents( $str, $startDelimiter, $endDelimiter ) {
$contents = [];
$startDelimiterLength = strlen( $startDelimiter );
$endDelimiterLength = strlen( $endDelimiter );
$startFrom = $contentStart = $contentEnd = 0;

while ( $contentStart = strpos( $str, $startDelimiter, $startFrom ) ) {
$contentStart += $startDelimiterLength;
$contentEnd = strpos( $str, $endDelimiter, $contentStart );
if ( $contentEnd === false ) {
break;
}
$contents[] = substr( $str, $contentStart, $contentEnd - $contentStart );
$startFrom = $contentEnd + $endDelimiterLength;
}

return $contents;
}

0 comments on commit 104df17

Please sign in to comment.