PluginProbe
Force Refresh / 2.10.2
Force Refresh v2.10.2
3.2.1 3.2.0 3.1.2 3.1.1 3.1.0 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.11.0 2.11.1 2.12.0 All 54 releases
force-refresh / includes / actions / admin / inc-release-notes.php

inc-release-notes.php in Force Refresh 2.10.2, at includes/actions/admin/inc-release-notes.php

169 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 switch ( true ) {
99 // If the line is a version number.
100 case preg_match( '/#### [0-9].*/', $note ):
101 $version_number = str_replace( array( '#', ' ' ), '', $note );
102 $notes_formatted[ $version_number ] = get_release_note_node( $current_plugin_version, $version_number );
103
104 $current_version_i = $version_number;
105 break;
106
107 // Release dates.
108 case preg_match( '/^\_/', $note ):
109 $notes_formatted[ $current_version_i ]['date'] = get_release_note_date( $note );
110 break;
111
112 // Release headers.
113 case preg_match( '/^##### .*/', $note ):
114 array_push(
115 $notes_formatted[ $current_version_i ]['notes'],
116 get_release_note_header_node( $note )
117 );
118 break;
119
120 // If the line is a release note.
121 default:
122 $release_note = str_replace( array( '#', '*' ), '', $note );
123 $all_notes = &$notes_formatted[ $current_version_i ]['notes'];
124 $release_notes_length = count( $all_notes );
125
126 array_push(
127 $all_notes[ $release_notes_length - 1 ]['sectionNotes'],
128 trim( $release_note ),
129 );
130 break;
131 }
132 }
133
134 /**
135 * Function to get release notes as formatted JSON.
136 *
137 * @param string $current_plugin_version The currently-loaded version of Force Refresh.
138 *
139 * @return array An array of release notes.
140 */
141 function get_release_notes_json( string $current_plugin_version ) {
142 $readme_contents = get_plugin_readme();
143 if ( ! $readme_contents ) {
144 return null;
145 }
146
147 $release_notes = get_release_notes_from_plugin_readme( $readme_contents );
148
149 $notes_formatted = array();
150 $current_version_index = null;
151
152 foreach ( $release_notes as $k => $v ) {
153 assign_release_note_based_on_role( $current_plugin_version, $current_version_index, $notes_formatted, $v );
154 }
155
156 return $notes_formatted;
157 }
158
159 /**
160 * Function to get the release notes.
161 *
162 * @param string $current_plugin_version The currently-loaded version of Force Refresh.
163 *
164 * @return array Array of release notes.
165 */
166 function get_release_notes( string $current_plugin_version ) {
167 return get_release_notes_json( $current_plugin_version );
168 }
169