Skip to content

Commit e760dfa

Browse files
committed
add admin page and feed link to Megafono
1 parent 3db7e99 commit e760dfa

File tree

5 files changed

+182
-2
lines changed

5 files changed

+182
-2
lines changed

megafono-wordpress-admin-page.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
class MegafonoWordpressAdminPage {
4+
function __construct( ) {
5+
add_menu_page( 'Megafono', 'Megafono', 'manage_options', 'megafono', array( $this, 'render'), 'dashicons-megaphone', 61 );
6+
}
7+
8+
public function render() {
9+
global $wpdb;
10+
$table_name = $wpdb->prefix . 'megafono_settings';
11+
$uid = $wpdb->get_row( "SELECT value FROM $table_name WHERE name='uid' LIMIT 1");
12+
13+
if ( $_SERVER["REQUEST_METHOD"] == "POST" ){
14+
$this->handle_post($uid);
15+
16+
$uid = $wpdb->get_row( "SELECT value FROM $table_name WHERE name='uid' LIMIT 1");
17+
}
18+
19+
$uid_value = $uid ? $uid->value : '';
20+
21+
include_once( 'views/admin.php' );
22+
}
23+
24+
public function handle_post($uid) {
25+
if( check_admin_referer( 'megafono-settings-save', 'megafono-settings' ) && isset($_POST['megafono_uid']) ) {
26+
global $wpdb;
27+
$table_name = $wpdb->prefix . 'megafono_settings';
28+
29+
if ( $uid == null ) {
30+
$wpdb->insert(
31+
$table_name,
32+
array(
33+
'name' => 'uid',
34+
'value' => $_POST['megafono_uid'],
35+
)
36+
);
37+
} else {
38+
$wpdb->update(
39+
$table_name,
40+
array(
41+
'value' => $_POST['megafono_uid'],
42+
),
43+
array(
44+
'name' => 'uid'
45+
)
46+
);
47+
}
48+
}
49+
}
50+
}
51+
?>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
global $megafono_db_version;
4+
$megafono_db_version = '1.0';
5+
6+
function uninstall() {
7+
delete_option('megafono_db_version');
8+
}
9+
10+
register_uninstall_hook( __FILE__, 'uninstall' );
11+
register_deactivation_hook( __FILE__, 'uninstall' );
12+
13+
14+
class MegafonoWordpressDataManagement {
15+
public function __construct() {
16+
register_activation_hook( __FILE__, array($this, 'install') );
17+
register_activation_hook( __FILE__, array($this, 'install_data') );
18+
19+
add_action( 'plugins_loaded', array($this, 'update') );
20+
add_action( 'admin_init', array($this, 'update') );
21+
}
22+
23+
24+
public function install() {
25+
global $wpdb;
26+
global $megafono_db_version;
27+
28+
$table_name = $wpdb->prefix . 'megafono_settings';
29+
$charset_collate = $wpdb->get_charset_collate();
30+
31+
$sql = "CREATE TABLE $table_name (
32+
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
33+
name varchar(55) NOT NULL,
34+
value varchar(55)
35+
) $charset_collate;";
36+
37+
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
38+
dbDelta( $sql );
39+
40+
add_option( 'megafono_db_version', $megafono_db_version );
41+
42+
$this->install_data();
43+
}
44+
45+
public function install_data() {
46+
global $wpdb;
47+
48+
$table_name = $wpdb->prefix . 'megafono_settings';
49+
50+
// $wpdb->insert(
51+
// $table_name,
52+
// array(
53+
// 'name' => 'uid',
54+
// 'value' => '',
55+
// )
56+
// );
57+
}
58+
59+
public function update() {
60+
global $megafono_db_version;
61+
62+
if ( get_site_option( 'megafono_db_version' ) != $megafono_db_version ) {
63+
$this->install();
64+
}
65+
}
66+
}
67+
?>

megafono-wordpress-updater.php

100644100755
File mode changed.

megafono-wordpress.php

100644100755
Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,56 @@
1212
*/
1313

1414
require_once( 'megafono-wordpress-updater.php' );
15+
require_once( 'megafono-wordpress-admin-page.php' );
16+
require_once( 'megafono-wordpress-data-management.php' );
1517

1618
class MegafonoWordpress {
1719
public function __construct() {
18-
add_filter( 'embed_defaults', array( $this, 'megafono_embed_defaults'), 10, 2 );
20+
add_filter( 'embed_defaults', array( $this, 'embed_defaults'), 10, 2 );
21+
add_action( 'admin_menu', array( $this, 'admin_menu') );
22+
23+
add_filter( 'feed_link', array( $this, 'redirect_feed_link' ) );
24+
25+
new MegafonoWordpressDataManagement();
1926

2027
if( is_admin() ) {
2128
new MegafonoWordpressUpdater( __FILE__, 'megafono', 'megafono-wordpress' );
2229
}
2330
}
2431

25-
public function megafono_embed_defaults( $embed_size, $url ) {
32+
public function embed_defaults( $embed_size, $url ) {
2633
if ( $this->is_megafono_url($url) ) {
2734
$embed_size['height'] = 190;
2835
}
2936

3037
return $embed_size;
3138
}
3239

40+
public function redirect_feed_link($url) {
41+
global $wpdb;
42+
$table_name = $wpdb->prefix . 'megafono_settings';
43+
$uid = $wpdb->get_row( "SELECT value FROM $table_name WHERE name='uid' LIMIT 1");
44+
45+
if ( strpos( $url, 'comments' ) )
46+
return $url;
47+
48+
if ( strpos( $url, 'podcast' ) && isset($uid) && isset($uid->value) && strlen($uid->value) > 0 ) {
49+
return esc_url( "https://feed.megafono.host/$uid->value" );
50+
} else {
51+
return $url;
52+
}
53+
}
54+
55+
public function admin_menu() {
56+
new MegafonoWordpressAdminPage();
57+
}
58+
59+
3360
private function is_megafono_url($url) {
3461
return preg_match('/megafono\.host/', $url) || preg_match('/^(.*)\/e\/([\w-]+)$/', $url) ;
3562
}
63+
64+
3665
}
3766

3867
new MegafonoWordpress();

views/admin.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<div class="wrap">
2+
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
3+
4+
5+
<form method="post" action="<?php echo esc_html( admin_url( 'admin.php?page=megafono' ) ); ?>">
6+
<table class="form-table">
7+
<tbody>
8+
<tr class="form-field form-required">
9+
<th scope="row">
10+
<label for="megafono_uid">Megafono UID</label>
11+
</th>
12+
<td>
13+
<input type='text' id='megafono_uid' name='megafono_uid' value="<?php echo $uid_value ?>"></input>
14+
<br>
15+
<span class="description">
16+
Para encontrar basta olhar para sua URL do Megafono, o UID, normalmente, é o nome do seu podcast com letra minusculas, sem espaço(trocados por _ or -) e sem acentos.
17+
<br />
18+
<br />
19+
Exemplos (em negrito o UID):<br />
20+
O Duke: https://feed.megafono.host/<strong>duke</strong><br />
21+
Megafono Changelog: https://feed.megafono.host/<strong>megafono-changelog</strong>
22+
23+
</span>
24+
</td>
25+
<tr>
26+
</tbody>
27+
</table>
28+
<?php
29+
wp_nonce_field( 'megafono-settings-save', 'megafono-settings' );
30+
submit_button();
31+
?>
32+
</form>
33+
</div>

0 commit comments

Comments
 (0)