Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin Update: July 2024 #47

Merged
merged 9 commits into from
Jul 26, 2024
Merged
81 changes: 35 additions & 46 deletions b2w-redirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
define( 'RT_B2WR_BLOG_URL', get_bloginfo( 'url' ) );

/**
* Add option to Tools Menu
* Add option to Tools Menu
*
* @return void
*/
function rt_blogger_to_wordpress_add_option() {
function rt_blogger_to_wordpress_add_option(): void {

add_management_page( __( 'Blogger To WordPress Redirection', 'blogger-to-wordpress-redirection' ), __( 'Blogger To WordPress Redirection', 'blogger-to-wordpress-redirection' ), 'administrator', 'rt-blogger-to-wordpress-redirection', 'rt_blogger_to_wordpress_administrative_page' );
add_management_page( __( 'Blogger To WordPress Redirection', 'blogger-to-wordpress-redirection' ), __( 'Blogger To WordPress Redirection', 'blogger-to-wordpress-redirection' ), 'manage_options', 'rt-blogger-to-wordpress-redirection', 'rt_blogger_to_wordpress_administrative_page' );

wp_enqueue_script( 'rt-blogger-to-wordpress-redirection-js', ( RT_B2WR_PLUGIN_URL . 'js/b2w-redirection-ajax.js' ), array( 'jquery', 'postbox' ), filemtime( RT_B2WR_PLUGIN_DIR . 'js/b2w-redirection-ajax.js' ), true );

Expand All @@ -39,52 +41,33 @@ function rt_blogger_to_wordpress_add_option() {
wp_enqueue_style( 'dashboard' );

}

}

add_action( 'admin_menu', 'rt_blogger_to_wordpress_add_option' );

/**
* Administrative Page - Begin
*
* @return void
*/
function rt_blogger_to_wordpress_administrative_page() {

require_once RT_B2WR_PLUGIN_DIR . 'templates/settings.php';

}

/**
* Get Configuration, called via AJAX
*/
function rt_b2wr_get_config() {

function rt_blogger_to_wordpress_administrative_page(): void {
global $wpdb;

if ( ! isset( $_POST['nonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'b2w_admin_nonce' )
) {
return;
}

// get all blogger domains, if avaliable.
$sql = "SELECT DISTINCT meta_value FROM {$wpdb->postmeta} where meta_key = 'blogger_blog'";

// unprepared sql ok.
$results = $wpdb->get_results( $sql ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.NoCaching

require_once RT_B2WR_PLUGIN_DIR . 'templates/get-config.php';

die();

require_once RT_B2WR_PLUGIN_DIR . 'templates/settings.php';
}

add_action( 'wp_ajax_rt_b2wr_get_config', 'rt_b2wr_get_config' );


/**
* Redirection Function (!important)
* Redirection Function (!important)
*
* @return void
*/
function rt_blogger_to_wordpress_redirection() {
function rt_blogger_to_wordpress_redirection(): void {

global $wpdb;

Expand Down Expand Up @@ -125,22 +108,21 @@ function rt_blogger_to_wordpress_redirection() {

// wp redirect ok.
wp_redirect( get_permalink( $wpurl[0][0] ) ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect

exit;
} else {
wp_safe_redirect( home_url(), 301 );
exit;
}
}

wp_safe_redirect( home_url(), 301 );
exit();
}

add_action( 'init', 'rt_blogger_to_wordpress_redirection' );

/**
* Verify Configuration
* Verify Configuration
*
* @return void
*/
function rt_b2wr_verify_config() {
function rt_b2wr_verify_config(): void {

global $wpdb;

Expand All @@ -166,18 +148,24 @@ function rt_b2wr_verify_config() {
$blogger_url = 'https://' . $domain_name . $rand_col2[0]->meta_value;
$local_url = get_permalink( $rand_post_id );

require_once RT_B2WR_PLUGIN_DIR . 'templates/verify-config.php';

echo wp_json_encode(
array(
'blogger_url' => esc_url( $blogger_url ),
'local_url' => esc_url( $local_url ),
)
);

die();

}

add_action( 'wp_ajax_rt_b2wr_verify_config', 'rt_b2wr_verify_config' );

/**
* Get Latest Feeds - Begin
*
* @return void
*/
function rt_get_feeds_from_blogger_to_wp() {
function rt_get_feeds_from_blogger_to_wp(): void {

require_once ABSPATH . WPINC . '/feed.php';

Expand All @@ -189,24 +177,26 @@ function rt_get_feeds_from_blogger_to_wp() {
}

require_once RT_B2WR_PLUGIN_DIR . 'templates/feeds.php';

}

/**
* Update Notice - Begin
*
* @return void
*/
function rt_blogger_to_wordpress_update_notice() {
function rt_blogger_to_wordpress_update_notice(): void {

if ( empty( get_option( 'rtb2wr206' ) ) ) {
require_once RT_B2WR_PLUGIN_DIR . 'template/update-notice.php';
}

}

/**
* Hide Notice block
*
* @return void
*/
function rt_b2wr_hide_notice_block() {
function rt_b2wr_hide_notice_block(): void {

if ( ! isset( $_POST['update_nonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['update_nonce'] ) ), 'b2w_update_nonce' )
Expand All @@ -215,7 +205,6 @@ function rt_b2wr_hide_notice_block() {
}

update_option( 'rtb2wr206', 'done' );

}

add_action( 'wp_ajax_rt_b2wr_hide_notice_block', 'rt_b2wr_hide_notice_block' );
48 changes: 17 additions & 31 deletions js/b2w-redirection-ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,12 @@
/**
* Gets configuration and appends to dom.
*
* @param {String} nonce
*
* @returns {void}
*/
function rt_start_config( nonce ){

if ( jQuery( '#get_config' ).html() != '' ) {
jQuery( '#get_config' ).html( '' );
}

jQuery.ajax(
{
url:'admin-ajax.php',
type: 'POST',
data: 'action=rt_b2wr_get_config&nonce=' + nonce,
success: function(result){
jQuery( '#get_config' ).append( result );
}
}
);
function rt_start_config(){
jQuery( '#get_config' ).show();
}

/**
* Generates blogger code
*
Expand All @@ -38,14 +23,15 @@ function rt_start_config( nonce ){
* @returns {void}
*/
function generate_code( domain_name, curr_domain, nonce){

let blogger_code = jQuery( '#blogger-code-textarea' ).val();
blogger_code = blogger_code.replace( /{{curr_domain}}/g, curr_domain );
jQuery( '#blogger-code-textarea' ).val( blogger_code );
jQuery( '#redirection-domain-name-1' ).attr( 'href', domain_name );
jQuery( '#redirection-domain-name-1' ).text( domain_name );
jQuery( '#redirection-domain-name-2' ).text( domain_name );
jQuery( '#check_config' ).attr( 'onClick', 'check_configuration("' + domain_name + '","' + nonce + '")' );
jQuery( '#code_here' ).show();
var response = jQuery( '#code_here' ).html();
response = response.replace( /{{curr_domain}}/g, curr_domain );
response = response.replace( /{{domain_name}}/g, domain_name );
response = response.replace( /{{nonce}}/g, nonce );
jQuery( '#code_here' ).html( response );

jQuery( '#verify_config' ).hide();
}

/**
Expand All @@ -57,18 +43,18 @@ function generate_code( domain_name, curr_domain, nonce){
* @returns {void}
*/
function check_configuration( domain_name, nonce ) {

if ( jQuery( '#verify_config' ).html() != '' ) {
jQuery( '#verify_config' ).html( '' );
}

jQuery.ajax(
{
url:'admin-ajax.php',
type: 'POST',
data: 'action=rt_b2wr_verify_config&dname=' + domain_name + '&config_nonce=' + nonce,
success: function(result) {
jQuery( '#verify_config' ).append( result );
const { blogger_url, local_url } = JSON.parse(result);
jQuery( '#blogger_url' ).attr( 'href', blogger_url );
jQuery( '#local_url' ).attr( 'href', local_url );
jQuery( '#blogger_url' ).text( blogger_url );
jQuery( '#local_url' ).text( local_url );
jQuery( '#verify_config' ).show();
}
}
);
Expand Down
Loading