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