| 1 |
<?php |
| 2 |
/** |
| 3 |
* Our action that enqueues all required admin scripts. |
| 4 |
* |
| 5 |
* @package ForceRefresh |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace JordanLeven\Plugins\ForceRefresh; |
| 9 |
|
| 10 |
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; |
| 11 |
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; |
| 12 |
|
| 13 |
/** |
| 14 |
* Function to get the contents of the plugin's README file. |
| 15 |
* |
| 16 |
* @return mixed String if file is located. |
| 17 |
*/ |
| 18 |
function get_plugin_readme() { |
| 19 |
$readme_file_location = dirname( get_main_plugin_file() ) . '/README.txt'; |
| 20 |
|
| 21 |
if ( ! file_exists( $readme_file_location ) ) { |
| 22 |
return null; |
| 23 |
} |
| 24 |
|
| 25 |
$filesystem = new \WP_Filesystem_Direct( true ); |
| 26 |
return $filesystem->get_contents( $readme_file_location ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Function to get the parsed array of release notes. |
| 31 |
* |
| 32 |
* @param string $readme The readme contents. |
| 33 |
* |
| 34 |
* @return array An array of release notes. |
| 35 |
*/ |
| 36 |
function get_release_notes_from_plugin_readme( string $readme ): array { |
| 37 |
// Remove the "== Changelog ==" string and everything before. |
| 38 |
$release_notes = preg_replace( '/.* Changelog \=\=/s', '', $readme ); |
| 39 |
$release_notes_split = explode( "\n", $release_notes ); |
| 40 |
$release_notes_split_filtered = array_filter( $release_notes_split ); |
| 41 |
|
| 42 |
return $release_notes_split_filtered; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Function to get the release note node. |
| 47 |
* |
| 48 |
* @param string $current_plugin_version The currently-loaded version of Force Refresh. |
| 49 |
* @param string $version_number The version number of this node. |
| 50 |
* |
| 51 |
* @return array The release note node. |
| 52 |
*/ |
| 53 |
function get_release_note_node( string $current_plugin_version, string $version_number ): array { |
| 54 |
return array( |
| 55 |
'date' => null, |
| 56 |
'isCurrentVersion' => $current_plugin_version === $version_number, |
| 57 |
'notes' => array(), |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Function to get the release note date from a note. |
| 63 |
* |
| 64 |
* @param string $note The note to parse. |
| 65 |
* |
| 66 |
* @return string The note release date. |
| 67 |
*/ |
| 68 |
function get_release_note_date( string $note ): string { |
| 69 |
return str_replace( array( '_', '' ), '', $note ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Function to get a release header node from a note. |
| 74 |
* |
| 75 |
* @param string $note The note. |
| 76 |
* |
| 77 |
* @return array The release note header node. |
| 78 |
*/ |
| 79 |
function get_release_note_header_node( string $note ): array { |
| 80 |
$release_header = str_replace( array( '#', '*' ), '', $note ); |
| 81 |
return array( |
| 82 |
'sectionHeader' => $release_header, |
| 83 |
'sectionNotes' => array(), |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Function to modify the current version index and notes based on a specific release note. |
| 89 |
* |
| 90 |
* @param string $current_plugin_version The currently-loaded version of Force Refresh. |
| 91 |
* @param int $current_version_i The index of the current release version. |
| 92 |
* @param array $notes_formatted The key/value pairs of release notes. |
| 93 |
* @param string $note The note to parse. |
| 94 |
* |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
function assign_release_note_based_on_role( string $current_plugin_version, &$current_version_i, &$notes_formatted, $note ): void { |
| 98 |
$note = trim( $note ); |
| 99 |
switch ( true ) { |
| 100 |
// If the line is a version number. |
| 101 |
case preg_match( '/#### [0-9].*/', $note ): |
| 102 |
$version_number = str_replace( array( '#', ' ' ), '', $note ); |
| 103 |
$notes_formatted[ $version_number ] = get_release_note_node( $current_plugin_version, $version_number ); |
| 104 |
|
| 105 |
$current_version_i = $version_number; |
| 106 |
break; |
| 107 |
|
| 108 |
// Release dates. |
| 109 |
case preg_match( '/^\_/', $note ): |
| 110 |
$notes_formatted[ $current_version_i ]['date'] = get_release_note_date( $note ); |
| 111 |
break; |
| 112 |
|
| 113 |
// Release headers. |
| 114 |
case preg_match( '/^##### .*/', $note ): |
| 115 |
array_push( |
| 116 |
$notes_formatted[ $current_version_i ]['notes'], |
| 117 |
get_release_note_header_node( $note ) |
| 118 |
); |
| 119 |
break; |
| 120 |
|
| 121 |
// If the line is a release note. |
| 122 |
default: |
| 123 |
$release_note = str_replace( array( '#', '*' ), '', $note ); |
| 124 |
$all_notes = &$notes_formatted[ $current_version_i ]['notes']; |
| 125 |
$release_notes_length = count( $all_notes ); |
| 126 |
|
| 127 |
array_push( |
| 128 |
$all_notes[ $release_notes_length - 1 ]['sectionNotes'], |
| 129 |
trim( $release_note ), |
| 130 |
); |
| 131 |
break; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Function to get release notes as formatted JSON. |
| 137 |
* |
| 138 |
* @param string $current_plugin_version The currently-loaded version of Force Refresh. |
| 139 |
* |
| 140 |
* @return array An array of release notes. |
| 141 |
*/ |
| 142 |
function get_release_notes_json( string $current_plugin_version ) { |
| 143 |
$readme_contents = get_plugin_readme(); |
| 144 |
if ( ! $readme_contents ) { |
| 145 |
return null; |
| 146 |
} |
| 147 |
|
| 148 |
$release_notes = get_release_notes_from_plugin_readme( $readme_contents ); |
| 149 |
|
| 150 |
$notes_formatted = array(); |
| 151 |
$current_version_index = null; |
| 152 |
|
| 153 |
foreach ( $release_notes as $k => $v ) { |
| 154 |
assign_release_note_based_on_role( $current_plugin_version, $current_version_index, $notes_formatted, $v ); |
| 155 |
} |
| 156 |
|
| 157 |
return $notes_formatted; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Function to get the release notes. |
| 162 |
* |
| 163 |
* @param string $current_plugin_version The currently-loaded version of Force Refresh. |
| 164 |
* |
| 165 |
* @return array Array of release notes. |
| 166 |
*/ |
| 167 |
function get_release_notes( string $current_plugin_version ) { |
| 168 |
return get_release_notes_json( $current_plugin_version ); |
| 169 |
} |
| 170 |
|