Browse Source

Initial commit

master
trader418 1 month ago
parent
commit
8c63344890
3 changed files with 103 additions and 0 deletions
  1. 27
    0
      gitea-plugin/gitea-plugin.php
  2. 9
    0
      gitea-plugin/gitea.api.php
  3. 67
    0
      gitea-plugin/giteaWidget.php

+ 27
- 0
gitea-plugin/gitea-plugin.php View File

@@ -0,0 +1,27 @@
1
+<?php
2
+/**
3
+* Plugin Name: Gitea Showcase Plugin
4
+* Plugin URI: https://trader418.me/
5
+* Description: Add a widget to showcase Gitea repos
6
+* Version: 1.0
7
+* Author: trader418
8
+* Author URI: https://trader418.me/
9
+**/
10
+add_action( 'admin_menu', 'Gitea_Showcase_menu' );
11
+function Gitea_Showcase_menu() {
12
+	add_menu_page( 'Gitea Showcase Title', 'Gitea Showcase', 'manage_options', 'GiteaShowcase', 'Gitea_Showcase_options', "icondoesntexist" );
13
+}
14
+function Gitea_Showcase_options() {
15
+	if ( !current_user_can( 'manage_options' ) )  {
16
+		wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
17
+	}
18
+	echo '<div class="wrap">';
19
+	echo '<h1>The unofficial Gitea showcase plugin was created by Trader418. https://trader418.me</h1>';
20
+	echo '<p>It may not be a fully featured plugin but it works for what I need :)</p>';
21
+	echo '</div>';
22
+}
23
+require('giteaWidget.php');
24
+function register_gitea_showcase() {
25
+	register_widget( 'Gitea_Showcase_Widget' );
26
+}
27
+add_action( 'widgets_init', 'register_gitea_showcase' );

+ 9
- 0
gitea-plugin/gitea.api.php View File

@@ -0,0 +1,9 @@
1
+<?PHP
2
+function cGet($url) {
3
+        $ch = curl_init();
4
+        curl_setopt($ch, CURLOPT_URL, $url);
5
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
6
+        $output = curl_exec($ch);
7
+        curl_close($ch);  
8
+		return $output;
9
+}

+ 67
- 0
gitea-plugin/giteaWidget.php View File

@@ -0,0 +1,67 @@
1
+<?PHP
2
+class Gitea_Showcase_Widget extends WP_Widget {
3
+
4
+	public function __construct() {
5
+		parent::__construct(
6
+		'Gitea_Showcase_Widget',
7
+		__( 'Gitea Showcase Widget', 'text_domain' ),
8
+		array(
9
+			'customize_selective_refresh' => true,
10
+		)
11
+		);
12
+	}
13
+	public function form( $instance ) {	
14
+		$defaults = array(
15
+			'url'    => '',
16
+			'username'    => '',
17
+		);
18
+		extract( wp_parse_args( ( array ) $instance, $defaults ) ); ?>
19
+		<p>
20
+			<label for="<?php echo esc_attr( $this->get_field_id( 'url' ) ); ?>"><?php _e( 'Gitea URL', 'text_domain' ); ?></label>
21
+			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'url' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'url' ) ); ?>" type="text" value="<?php echo esc_attr( $url ); ?>" />
22
+		</p>
23
+		<p>
24
+			<label for="<?php echo esc_attr( $this->get_field_id( 'username' ) ); ?>"><?php _e( 'Gitea username', 'text_domain' ); ?></label>
25
+			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'username' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'username' ) ); ?>" type="text" value="<?php echo esc_attr( $username ); ?>" />
26
+		</p>
27
+		<?PHP
28
+	}
29
+	public function update( $new_instance, $old_instance ) {
30
+		$instance = $old_instance;
31
+		$instance['url']    = isset( $new_instance['url'] ) ? wp_strip_all_tags( $new_instance['url'] ) : '';
32
+		$instance['username']    = isset( $new_instance['username'] ) ? wp_strip_all_tags( $new_instance['username'] ) : '';
33
+		return $instance;
34
+	}
35
+
36
+	public function widget( $args, $instance ) {
37
+		extract( $args );
38
+		$url = isset( $instance['url'] ) ? apply_filters( 'widget_url', $instance['url'] ) : '';
39
+		$username = isset( $instance['username'] ) ? apply_filters( 'widget_username', $instance['username'] ) : '';
40
+		echo $before_widget;
41
+	    echo '<div class="widget-text wp_widget_plugin_box">';
42
+		?>
43
+		<h3 class="widget-title"><span>My Popular Gitea Repo's</span></h3>
44
+		<?PHP
45
+		if ( $url && $username ) {
46
+			require('gitea.api.php');
47
+			$_res = json_decode(cGet($url."/api/v1/users/". $username ."/repos"), true);
48
+			echo '<ul>';
49
+			if (is_array($_res)) {
50
+				foreach($_res as $repo){
51
+					if ($repo['mirror'] != 1) {
52
+						echo "<li>";
53
+						echo "<a href='".$repo['html_url']."' target='_blank'>".$repo['name']."</a>";
54
+						echo "</li>";
55
+					}
56
+				}
57
+			}
58
+			
59
+			echo "</ul>";
60
+			
61
+		} else {
62
+			?>Error, please check the Gitea URL you have set<?PHP
63
+		}
64
+		echo '</div>';
65
+		echo $after_widget;
66
+	}
67
+}

Loading…
Cancel
Save