1+ <?php
2+ /*
3+ Plugin Name: WS GitHub Sync Wordpress
4+ Plugin URI: https://github.com/WebShells/git-sync-wordpress
5+ Description: Display GitHub repository issues, commits, and pull requests using shortcodes.
6+ Version: 1.0
7+ Author: WebShells ( WebShells Services Co. )
8+ Author URI: https://www.wshells.ws
9+ Text Domain: github-sync-wordpress
10+ */
11+
12+ function time_since_creation ($ created_at ) {
13+ $ current_time = time ();
14+ $ time_diff = $ current_time - strtotime ($ created_at );
15+
16+ if ($ time_diff < 60 * 60 * 24 ) { // Less than 1 day
17+ return 'Today ' ;
18+ } elseif ($ time_diff < 60 * 60 * 24 * 30 ) { // Less than 30 days
19+ $ days_ago = floor ($ time_diff / (60 * 60 * 24 ));
20+ return $ days_ago . ' ' . ($ days_ago > 1 ? 'days ' : 'day ' ) . ' ago ' ;
21+ } else { // More than 30 days
22+ $ months_ago = floor ($ time_diff / (60 * 60 * 24 * 30 ));
23+ return $ months_ago . ' ' . ($ months_ago > 1 ? 'months ' : 'month ' ) . ' ago ' ;
24+ }
25+ }
26+
27+ function gitsync_issues_shortcode ($ atts ) {
28+ $ atts = shortcode_atts (array (
29+ 'token ' => '' ,
30+ 'repository ' => '' ,
31+ 'owner ' => '' ,
32+ ), $ atts , 'gitsync_issues ' );
33+
34+ // Make API request for open issues using cURL
35+ $ issues_api_url = "https://api.github.com/repos/ {$ atts ['owner ' ]}/ {$ atts ['repository ' ]}/issues?state=open " ;
36+ $ headers = array (
37+ 'Authorization: token ' . $ atts ['token ' ],
38+ 'User-Agent: Your-User-Agent ' , // Add your own User-Agent here
39+ );
40+
41+ // Initialize cURL session for issues
42+ $ ch = curl_init ();
43+ curl_setopt ($ ch , CURLOPT_URL , $ issues_api_url );
44+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
45+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
46+
47+ // Execute cURL session for issues
48+ $ issues_response = curl_exec ($ ch );
49+ $ http_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
50+
51+ // Close cURL session for issues
52+ curl_close ($ ch );
53+
54+ // Check if the API request for issues was successful
55+ if ($ http_code !== 200 ) {
56+ return '<p>Error retrieving issues data from GitHub API.</p> ' ;
57+ }
58+
59+ // Check if the response is valid JSON
60+ $ issues_data = json_decode ($ issues_response , true );
61+
62+ // Display the data
63+ $ output = '' ; // Initialize output variable
64+
65+ if (!empty ($ issues_data )) {
66+ // Sort issues by created_at date in descending order (newest to oldest)
67+ usort ($ issues_data , function ($ a , $ b ) {
68+ return strtotime ($ b ['created_at ' ]) - strtotime ($ a ['created_at ' ]);
69+ });
70+
71+ // Get the latest 10 issues
72+ $ last_10_issues = array_slice ($ issues_data , 0 , 10 );
73+
74+ // Display issues
75+ $ output .= '<ul> ' ;
76+ foreach ($ last_10_issues as $ issue ) {
77+ // Check if the issue is not a pull request
78+ if (!isset ($ issue ['pull_request ' ])) {
79+ // Generate list items for each issue
80+ $ output .= '<li> ' ;
81+
82+ // Make the author image clickable and link to the user's GitHub profile
83+ $ output .= '<a href=" ' . esc_url ($ issue ['user ' ]['html_url ' ]) . '" target="_blank"> ' ;
84+ $ output .= '<img src=" ' . esc_url ($ issue ['user ' ]['avatar_url ' ]) . '" alt=" ' . esc_attr ($ issue ['user ' ]['login ' ]) . '" width="24" height="24" style="vertical-align: middle; margin-right: 8px;" /> ' ;
85+ $ output .= '</a> ' ;
86+
87+ // Issue Number (Clickable)
88+ $ output .= '<a href=" ' . esc_url ($ issue ['html_url ' ]) . '" target="_blank">Issue # ' . esc_html ($ issue ['number ' ]) . '</a> ' ;
89+
90+ // Issue Title
91+ $ output .= ' - ' . esc_html ($ issue ['title ' ]);
92+
93+ // Calculate the time since the issue was created
94+ $ time_since_creation = time_since_creation ($ issue ['created_at ' ]);
95+ $ output .= ' - Since ( ' . $ time_since_creation . ') ' ;
96+
97+ // Make the author name clickable and link to the user's GitHub profile
98+ $ output .= ' - <a href=" ' . esc_url ($ issue ['user ' ]['html_url ' ]) . '" target="_blank"> ' . esc_html ($ issue ['user ' ]['login ' ]) . '</a> ' ;
99+
100+ $ output .= '</li> ' ;
101+ }
102+ }
103+ $ output .= '</ul> ' ;
104+ } else {
105+ $ output .= '<p>No open issues found.</p> ' ;
106+ }
107+
108+ return $ output ;
109+ }
110+
111+ add_shortcode ('gitsync_issues ' , 'gitsync_issues_shortcode ' );
112+
113+ function gitsync_commits_shortcode ($ atts ) {
114+ $ atts = shortcode_atts (array (
115+ 'token ' => '' ,
116+ 'repository ' => '' ,
117+ 'owner ' => '' ,
118+ ), $ atts , 'gitsync_commits ' );
119+
120+ // Make API request for open commits using cURL
121+ $ commits_api_url = "https://api.github.com/repos/ {$ atts ['owner ' ]}/ {$ atts ['repository ' ]}/commits?per_page=10&sort=author-date&direction=desc " ;
122+ $ headers = array (
123+ 'Authorization: token ' . $ atts ['token ' ],
124+ 'User-Agent: Your-User-Agent ' , // Add your own User-Agent here
125+ );
126+
127+ // Initialize cURL session for commits
128+ $ ch = curl_init ();
129+ curl_setopt ($ ch , CURLOPT_URL , $ commits_api_url );
130+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
131+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
132+
133+ // Execute cURL session for commits
134+ $ commits_response = curl_exec ($ ch );
135+ $ http_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
136+
137+ // Close cURL session for commits
138+ curl_close ($ ch );
139+
140+ // Check if the API request for commits was successful
141+ if ($ http_code !== 200 ) {
142+ return '<p>Error retrieving commits data from GitHub API.</p> ' ;
143+ }
144+
145+ // Check if the response is valid JSON
146+ $ commits_data = json_decode ($ commits_response , true );
147+
148+ // Display the data
149+ $ output = '' ; // Initialize output variable
150+
151+ if (!empty ($ commits_data )) {
152+ // Display commits
153+ $ output .= '<ul> ' ;
154+ foreach ($ commits_data as $ commit ) {
155+ // Check if the commit message contains "Translate-URL:", and if so, exclude the exact line that contains it
156+ $ commit_message = explode ("\n" , $ commit ['commit ' ]['message ' ]);
157+ $ filtered_message = '' ;
158+ $ found_translate_url = false ;
159+ foreach ($ commit_message as $ line ) {
160+ if (strpos ($ line , 'Translate-URL: ' ) !== false ) {
161+ $ found_translate_url = true ;
162+ } else {
163+ // Remove line breaks within the commit message
164+ $ filtered_message .= str_replace ("\n" , "" , $ line ) . " " ;
165+ }
166+ }
167+
168+ // Generate list items for each commit
169+ $ output .= '<li> ' ;
170+
171+ // Make the author image clickable and link to the user's GitHub profile
172+ $ output .= '<a href=" ' . esc_url ($ commit ['author ' ]['html_url ' ]) . '" target="_blank"> ' ;
173+ $ output .= '<img src=" ' . esc_url ($ commit ['author ' ]['avatar_url ' ]) . '" alt=" ' . esc_attr ($ commit ['commit ' ]['author ' ]['name ' ]) . '" width="24" height="24" style="vertical-align: middle; margin-right: 8px;" /> ' ;
174+ $ output .= '</a> ' ;
175+
176+ // Commit Number (Clickable)
177+ $ commit_number = substr ($ commit ['sha ' ], 0 , 7 ); // Extract the first 7 characters
178+ $ output .= '<a href=" ' . esc_url ($ commit ['html_url ' ]) . '" target="_blank">Commit # ' . esc_html ($ commit_number ) . '</a> ' ;
179+
180+ // Commit Message (remove any line breaks within the commit message)
181+ $ output .= ' - ' . esc_html ($ filtered_message );
182+
183+ // Calculate the time since the commit was created
184+ $ time_since_creation = time_since_creation ($ commit ['commit ' ]['author ' ]['date ' ]);
185+ $ output .= ' - Since ( ' . $ time_since_creation . ') ' ;
186+
187+ // Make the author name clickable and link to the user's GitHub profile
188+ $ output .= ' - <a href=" ' . esc_url ($ commit ['author ' ]['html_url ' ]) . '" target="_blank"> ' . esc_html ($ commit ['commit ' ]['author ' ]['name ' ]) . '</a> ' ;
189+
190+ $ output .= '</li> ' ;
191+ }
192+ $ output .= '</ul> ' ;
193+ } else {
194+ $ output .= '<p>No new commits found.</p> ' ;
195+ }
196+
197+ return $ output ;
198+ }
199+
200+ add_shortcode ('gitsync_commits ' , 'gitsync_commits_shortcode ' );
201+
202+ function gitsync_pull_requests_shortcode ($ atts ) {
203+ $ atts = shortcode_atts (array (
204+ 'token ' => '' ,
205+ 'repository ' => '' ,
206+ 'owner ' => '' ,
207+ ), $ atts , 'gitsync_pull_requests ' );
208+
209+ // Make API request for open pull requests using cURL
210+ $ pulls_api_url = "https://api.github.com/repos/ {$ atts ['owner ' ]}/ {$ atts ['repository ' ]}/pulls?state=open " ;
211+ $ headers = array (
212+ 'Authorization: token ' . $ atts ['token ' ],
213+ 'User-Agent: Your-User-Agent ' , // Add your own User-Agent here
214+ );
215+
216+ // Initialize cURL session for pull requests
217+ $ ch = curl_init ();
218+ curl_setopt ($ ch , CURLOPT_URL , $ pulls_api_url );
219+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
220+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
221+
222+ // Execute cURL session for pull requests
223+ $ pulls_response = curl_exec ($ ch );
224+ $ http_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
225+
226+ // Close cURL session for pull requests
227+ curl_close ($ ch );
228+
229+ // Check if the API request for pull requests was successful
230+ if ($ http_code !== 200 ) {
231+ return '<p>Error retrieving pull requests data from GitHub API.</p> ' ;
232+ }
233+
234+ // Check if the response is valid JSON
235+ $ pulls_data = json_decode ($ pulls_response , true );
236+
237+ // Display the data
238+ $ output = '' ; // Initialize output variable
239+
240+ if (!empty ($ pulls_data )) {
241+ // Sort pull requests by created_at date in descending order (newest to oldest)
242+ usort ($ pulls_data , function ($ a , $ b ) {
243+ return strtotime ($ b ['created_at ' ]) - strtotime ($ a ['created_at ' ]);
244+ });
245+
246+ // Get the latest 10 pull requests
247+ $ last_10_pulls = array_slice ($ pulls_data , 0 , 10 );
248+
249+ // Display pull requests
250+ $ output .= '<ul> ' ;
251+ foreach ($ last_10_pulls as $ pull ) {
252+ // Generate list items for each pull request
253+ $ output .= '<li> ' ;
254+
255+ // Make the author image clickable and link to the user's GitHub profile
256+ $ output .= '<a href=" ' . esc_url ($ pull ['user ' ]['html_url ' ]) . '" target="_blank"> ' ;
257+ $ output .= '<img src=" ' . esc_url ($ pull ['user ' ]['avatar_url ' ]) . '" alt=" ' . esc_attr ($ pull ['user ' ]['login ' ]) . '" width="24" height="24" style="vertical-align: middle; margin-right: 8px;" /> ' ;
258+ $ output .= '</a> ' ;
259+
260+ // Pull Request Number (Clickable)
261+ $ output .= '<a href=" ' . esc_url ($ pull ['html_url ' ]) . '" target="_blank">PR # ' . esc_html ($ pull ['number ' ]) . '</a> ' ;
262+
263+ // Pull Request Title
264+ $ output .= ' - ' . esc_html ($ pull ['title ' ]);
265+
266+ // Calculate the time since the pull request was created
267+ $ time_since_creation = time_since_creation ($ pull ['created_at ' ]);
268+ $ output .= ' - Since ( ' . $ time_since_creation . ') ' ;
269+
270+ // Make the author name clickable and link to the user's GitHub profile
271+ $ output .= ' - <a href=" ' . esc_url ($ pull ['user ' ]['html_url ' ]) . '" target="_blank"> ' . esc_html ($ pull ['user ' ]['login ' ]) . '</a> ' ;
272+
273+ $ output .= '</li> ' ;
274+ }
275+ $ output .= '</ul> ' ;
276+ } else {
277+ $ output .= '<p>No open pull requests found.</p> ' ;
278+ }
279+
280+ return $ output ;
281+ }
282+
283+ add_shortcode ('gitsync_pull_requests ' , 'gitsync_pull_requests_shortcode ' );
284+ ?>
0 commit comments