PluginProbe
Force Refresh / 2.9.1
Force Refresh v2.9.1
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.9.1, at includes/actions/admin/inc.release-notes.php

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