| 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 modify the current version index and notes based on a specific release note. |
| 47 |
* |
| 48 |
* @param string $current_plugin_version The currently-loaded version of Force Refresh. |
| 49 |
* @param int $current_version_i The index of the current release version. |
| 50 |
* @param array $notes_formatted The key/value pairs of release notes. |
| 51 |
* @param string $note The note to parse. |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
function assign_release_note_based_on_role( string $current_plugin_version, &$current_version_i, &$notes_formatted, $note ): void { |
| 56 |
switch ( true ) { |
| 57 |
// If the line is a version number. |
| 58 |
case preg_match( '/#### [0-9].*/', $note ): |
| 59 |
$version_number = str_replace( array( '#', ' ' ), '', $note ); |
| 60 |
$notes_formatted[ $version_number ] = array( |
| 61 |
'date' => null, |
| 62 |
'isCurrentVersion' => $current_plugin_version === $version_number, |
| 63 |
'notes' => array(), |
| 64 |
); |
| 65 |
|
| 66 |
$current_version_i = $version_number; |
| 67 |
break; |
| 68 |
|
| 69 |
// Release dates. |
| 70 |
case preg_match( '/^\_/', $note ): |
| 71 |
$release_date_formatted = str_replace( array( '_', '' ), '', $note ); |
| 72 |
|
| 73 |
$notes_formatted[ $current_version_i ]['date'] = $release_date_formatted; |
| 74 |
break; |
| 75 |
|
| 76 |
// Release headers. |
| 77 |
case preg_match( '/^##### .*/', $note ): |
| 78 |
$release_header = str_replace( array( '#', '*' ), '', $note ); |
| 79 |
|
| 80 |
array_push( |
| 81 |
$notes_formatted[ $current_version_i ]['notes'], |
| 82 |
array( |
| 83 |
'sectionHeader' => $release_header, |
| 84 |
'sectionNotes' => array(), |
| 85 |
), |
| 86 |
); |
| 87 |
break; |
| 88 |
|
| 89 |
// If the line is a release note. |
| 90 |
default: |
| 91 |
$release_note = str_replace( array( '#', '*' ), '', $note ); |
| 92 |
$all_notes = &$notes_formatted[ $current_version_i ]['notes']; |
| 93 |
$release_notes_length = count( $all_notes ); |
| 94 |
|
| 95 |
array_push( |
| 96 |
$all_notes[ $release_notes_length - 1 ]['sectionNotes'], |
| 97 |
trim( $release_note ), |
| 98 |
); |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Function to get release notes as formatted JSON. |
| 105 |
* |
| 106 |
* @param string $current_plugin_version The currently-loaded version of Force Refresh. |
| 107 |
* |
| 108 |
* @return array An array of release notes. |
| 109 |
*/ |
| 110 |
function get_release_notes_json( string $current_plugin_version ) { |
| 111 |
$readme_contents = get_plugin_readme(); |
| 112 |
if ( ! $readme_contents ) { |
| 113 |
return null; |
| 114 |
} |
| 115 |
|
| 116 |
$release_notes = get_release_notes_from_plugin_readme( $readme_contents ); |
| 117 |
|
| 118 |
$notes_formatted = array(); |
| 119 |
$current_version_index = null; |
| 120 |
|
| 121 |
foreach ( $release_notes as $k => $v ) { |
| 122 |
assign_release_note_based_on_role( $current_plugin_version, $current_version_index, $notes_formatted, $v ); |
| 123 |
} |
| 124 |
|
| 125 |
return $notes_formatted; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Function to get the release notes. |
| 130 |
* |
| 131 |
* @param string $current_plugin_version The currently-loaded version of Force Refresh. |
| 132 |
* |
| 133 |
* @return array Array of release notes. |
| 134 |
*/ |
| 135 |
function get_release_notes( string $current_plugin_version ) { |
| 136 |
return get_release_notes_json( $current_plugin_version ); |
| 137 |
} |
| 138 |
|