Override WordPress plugin class to change upload image size - php

I'm trying to change the function inside the handler class for uploading user image from front end using the the code which mostly works in situations like this one:
class Custom_Class extends Main_Class {
function __construct() {
remove_action('default_action', array($this));
add_action('new_action', array($this));
}
/* custom code from here on */
}
new Custom_Class
The goal is to change the $image->resize value to 320px, but I can not even remove the action, and the original code looks like this:
class UR_Form_Handler {
/**
* Hook in methods.
*/
public static function init() {
add_action( 'template_redirect', array( __CLASS__, 'redirect_reset_password_link' ) );
add_action( 'template_redirect', array( __CLASS__, 'save_profile_details' ) );
add_action( 'template_redirect', array( __CLASS__, 'save_change_password' ) );
add_action( 'wp_loaded', array( __CLASS__, 'process_login' ), 20 );
add_action( 'wp_loaded', array( __CLASS__, 'process_lost_password' ), 20 );
add_action( 'wp_loaded', array( __CLASS__, 'process_reset_password' ), 20 );
add_action( 'user_registration_before_customer_login_form', array( __CLASS__, 'export_confirmation_request' ) );
}
/**
* Save/update a profile fields if the form was submitted through the user account page.
*
* #return mixed
*/
public function save_profile_details() {
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
return;
}
if ( empty( $_POST['action'] ) || 'save_profile_details' !== $_POST['action'] || empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'save_profile_details' ) ) {
return;
}
$user_id = get_current_user_id();
if ( $user_id <= 0 ) {
return;
}
if ( has_action( 'uraf_profile_picture_buttons' ) ) {
if ( isset( $_POST['profile_pic_url'] ) && ! empty( $_POST['profile_pic_url'] ) ) {
update_user_meta( $user_id, 'user_registration_profile_pic_url', $_POST['profile_pic_url'] );
}
} else {
if ( isset( $_FILES['profile-pic'] ) && $_FILES['profile-pic']['size'] ) {
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$upload = $_FILES['profile-pic'];
$upload_overrides = array(
'action' => 'save_profile_details',
);
$uploaded = wp_handle_upload( $upload, $upload_overrides );
if ( $uploaded && ! isset( $uploaded['error'] ) ) {
$image = wp_get_image_editor( $uploaded['file'] );
if ( ! is_wp_error( $image ) ) {
$image->resize( 150, 150, true );
$image->save( $uploaded['file'] );
}
update_user_meta( $user_id, 'user_registration_profile_pic_url', $uploaded['url'] );
} else {
ur_add_notice( $uploaded['error'], 'error' );
}
} elseif ( UPLOAD_ERR_NO_FILE !== $_FILES['profile-pic']['error'] ) {
switch ( $_FILES['profile-pic']['error'] ) {
case UPLOAD_ERR_INI_SIZE:
ur_add_notice( __( 'File size exceed, please check your file size.', 'user-registration' ), 'error' );
break;
default:
ur_add_notice( __( 'Something went wrong while uploading, please contact your site administrator.', 'user-registration' ), 'error' );
break;
}
} elseif ( empty( $_POST['profile-pic-url'] ) ) {
$upload_dir = wp_upload_dir();
$profile_url = get_user_meta( $user_id, 'user_registration_profile_pic_url', true );
// Check if profile already set?
if ( $profile_url ) {
// Then delete file and user meta.
$profile_url = $upload_dir['basedir'] . explode( '/uploads', $profile_url )[1];
if ( ! empty( $profile_url ) && file_exists( $profile_url ) ) {
#unlink( $profile_url );
}
delete_user_meta( $user_id, 'user_registration_profile_pic_url' );
}
}
}
$form_id_array = get_user_meta( $user_id, 'ur_form_id' );
$form_id = 0;
if ( isset( $form_id_array[0] ) ) {
$form_id = $form_id_array[0];
}
$profile = user_registration_form_data( $user_id, $form_id );
foreach ( $profile as $key => $field ) {
if ( ! isset( $field['type'] ) ) {
$field['type'] = 'text';
}
// Get Value.
switch ( $field['type'] ) {
case 'checkbox':
if ( isset( $_POST[ $key ] ) && is_array( $_POST[ $key ] ) ) {
$_POST[ $key ] = $_POST[ $key ];
} else {
$_POST[ $key ] = (int) isset( $_POST[ $key ] );
}
break;
default:
$_POST[ $key ] = isset( $_POST[ $key ] ) ? ur_clean( $_POST[ $key ] ) : '';
break;
}
// Hook to allow modification of value.
$_POST[ $key ] = apply_filters( 'user_registration_process_myaccount_field_' . $key, $_POST[ $key ] );
$disabled = false;
if ( isset( $field['custom_attributes'] ) && isset( $field['custom_attributes']['readonly'] ) && isset( $field['custom_attributes']['disabled'] ) ) {
if ( 'readonly' === $field['custom_attributes']['readonly'] || 'disabled' === $field['custom_attributes']['disabled'] ) {
$disabled = true;
}
}
if ( ! empty( $_POST[ $key ] ) ) {
// Validation rules.
if ( ! empty( $field['validate'] ) && is_array( $field['validate'] ) ) {
foreach ( $field['validate'] as $rule ) {
switch ( $rule ) {
case 'email':
$_POST[ $key ] = strtolower( $_POST[ $key ] );
if ( ! is_email( $_POST[ $key ] ) ) {
ur_add_notice( sprintf( __( '%s is not a valid email address.', 'user-registration' ), '<strong>' . $field['label'] . '</strong>' ), 'error' );
}
break;
}
}
}
}
}// End foreach().
do_action( 'user_registration_after_save_profile_validation', $user_id, $profile );
if ( 0 === ur_notice_count( 'error' ) ) {
$user_data = array();
foreach ( $profile as $key => $field ) {
$new_key = str_replace( 'user_registration_', '', $key );
if ( in_array( $new_key, ur_get_user_table_fields() ) ) {
if ( $new_key === 'display_name' ) {
$user_data['display_name'] = $_POST[ $key ];
} else {
$user_data[ $new_key ] = $_POST[ $key ];
}
} else {
$update_key = $key;
if ( in_array( $new_key, ur_get_registered_user_meta_fields() ) ) {
$update_key = str_replace( 'user_', '', $new_key );
}
$disabled = isset( $field['custom_attributes']['disabled'] ) ? $field['custom_attributes']['disabled'] : '';
if ( $disabled !== 'disabled' ) {
update_user_meta( $user_id, $update_key, $_POST[ $key ] );
}
}
}
if ( count( $user_data ) > 0 ) {
$user_data['ID'] = get_current_user_id();
wp_update_user( $user_data );
}
do_action( 'user_registration_save_profile_details', $user_id, $form_id );
wp_safe_redirect( ur_get_endpoint_url( 'edit-profile', '', ur_get_page_permalink( 'myaccount' ) ) );
exit;
}
}
}
Either I get Fatal error: Cannot make static method UR_Form_Handler::save_profile_details() non static in class Custom_UR_Form_Handler, or the override doesn't work at all, I'm not sure if I'm even doing this the right way, maybe there's no need to override the entire function, but I'm stuck with this, any help or idea would be much appreciated, thanks.

Related

Adding YOAST Open Graph Image programmatically

I have a problem regarding the YOAST:og images. Normally you would have to add an image manually for each post/CPT post, but I would like to add the whole thing dynamically for a CPT post. It should be noted that I would like to store a dynamic image URL, depending on what is set for the contribution/post.
I have already tried the following approach:
function add_yoast_data_when_save( $post_id, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Don't save revisions and autosaves
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
return $post_id;
}
$yoast_opengraph_image = get_post_meta( $post_id, '_yoast_wpseo_opengraph-image', true );
$yoast_twitter_image = get_post_meta( $post_id, '_yoast_wpseo_twitter-image', true );
if ( $post->post_type == 'news' ) {
if ( empty( $yoast_opengraph_image ) || empty( $yoast_twitter_image ) ) {
$elementor_data = get_post_meta( $post_id, '_elementor_data', true );
$all_images_array = get_all_elementor_images( $elementor_data );
if ( isset( $all_images_array['teaser'][0] ) && ! empty( $all_images_array['teaser'][0] ) ) {
if ( empty( $yoast_opengraph_image ) ) {
update_post_meta( $post_id, '_yoast_wpseo_opengraph-image', $all_images_array['teaser'][0] );
}
if ( empty( $yoast_twitter_image ) ) {
update_post_meta( $post_id, '_yoast_wpseo_twitter-image', $all_images_array['teaser'][0] );
}
}
}
}
if ( $post->post_type == 'calendar' ) {
$status = get_post_meta( $post_id, 'status', true );
if ( $status == 'normal' ) {
$image = wp_get_attachment_url( get_post_meta( $post_id, 'image', true ) );
if ( empty( $yoast_opengraph_image ) || empty( $yoast_twitter_image ) ) {
if ( empty( $yoast_opengraph_image ) ) {
update_post_meta( $post_id, '_yoast_wpseo_opengraph-image', $image );
}
if ( empty( $yoast_twitter_image ) ) {
update_post_meta( $post_id, '_yoast_wpseo_twitter-image', $image );
}
}
} else {
$elementor_data = get_post_meta( $post_id, '_elementor_data', true );
$all_images_array = get_all_elementor_images( $elementor_data );
if ( isset( $all_images_array['teaser'][0] ) && ! empty( $all_images_array['teaser'][0] ) ) {
if ( empty( $yoast_opengraph_image ) ) {
update_post_meta( $post_id, '_yoast_wpseo_opengraph-image', $all_images_array['teaser'][0] );
}
if ( empty( $yoast_twitter_image ) ) {
update_post_meta( $post_id, '_yoast_wpseo_twitter-image', $all_images_array['teaser'][0] );
}
}
}
}
}
add_action( 'save_post', 'add_yoast_data_when_save', 10, 2 );
function get_all_elementor_images( $elementor_data ) {
$all_images_array = array();
if ( ! empty( $elementor_data ) ) {
$decode_json = json_decode( $elementor_data, true );
$result = array();
array_walk_recursive(
$decode_json,
function( $value, $key ) use ( &$result ) {
if ( $key === 'url' ) {
$result[] = $value;
}
}
);
foreach ( $result as $key_image => $image ) {
$attachment_id = attachment_url_to_postid( $image );
$quote = wp_get_attachment_image_src( $attachment_id, 'quote' );
$teaser = wp_get_attachment_image_src( $attachment_id, 'teaser' );
$col_6 = wp_get_attachment_image_src( $attachment_id, 'col-6' );
$col_4 = wp_get_attachment_image_src( $attachment_id, 'col-4' );
$col_3 = wp_get_attachment_image_src( $attachment_id, 'col-3' );
$col_2 = wp_get_attachment_image_src( $attachment_id, 'col-2' );
if ( ! empty( $quote[0] ) ) {
$all_images_array['quote'][] = $quote[0] . ',';
}
if ( ! empty( $teaser[0] ) ) {
$all_images_array['teaser'][] = $teaser[0] . ',';
}
if ( ! empty( $col_6[0] ) ) {
$all_images_array['col-6'][] = $col_6[0] . ',';
}
if ( ! empty( $col_4[0] ) ) {
$all_images_array['col-4'][] = $col_4[0] . ',';
}
if ( ! empty( $col_3[0] ) ) {
$all_images_array['col-3'][] = $col_3[0] . ',';
}
if ( ! empty( $col_2[0] ) ) {
$all_images_array['col-2'][] = $col_2[0] . ',';
}
}
}
return $all_images_array;
}
As you can see, I would like to update the YOAST metadata in the post metas on the "save_post" action hook. However, YOAST overwrites this parameter every time you update the post and I can't set this post meta dynamically.
In general, this complete function should be there so that editors should not maintain this data manually, but is set dynamically when the data is saved.
I hope you can help me with this topic.
Thank you in advance.

Profile photo upload locked at 600px. Ultimate Member Registration Form

This is the plugin that generates a profile photo upload button:
https://github.com/ultimatemember/Extended/blob/main/um-profile-photo/um-profile-photo.php
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Add new predefined field "Profile Photo" in UM Form Builder.
*
* #param array $arr field array settings.
*/
function um_predefined_fields_hook_profile_photo( $arr ) {
$arr['register_profile_photo'] = array(
'title' => __( 'Profile Photo', 'ultimate-member' ),
'metakey' => 'register_profile_photo',
'type' => 'image',
'label' => __('Change your profile photo','ultimate-member' ),
'upload_text' => __( 'Upload your photo here', 'ultimate-member' ),
'icon' => 'um-faicon-camera',
'crop' => 1,
'editable' => 1,
'max_size' => ( UM()->options()->get('profile_photo_max_size') ) ? UM()->options()->get('profile_photo_max_size') : 999999999,
'min_width' => str_replace('px','',UM()->options()->get('profile_photosize')),
'min_height' => str_replace('px','',UM()->options()->get('profile_photosize')),
);
return $arr;
}
add_filter( 'um_predefined_fields_hook', 'um_predefined_fields_hook_profile_photo', 99999, 1 );
/**
* Multiply Profile Photo with different sizes
*
* #param integer $user_id the user ID.
*/
function um_registration_set_profile_photo( $user_id, $args ) {
if( isset( $args['form_id'] )) $req = 'register_profile_photo-' . $args['form_id'];
else $req = 'register_profile_photo';
if( ! isset( $_REQUEST[$req] ) ) return;
//if ( strpos( $_REQUEST['register_profile_photo'], '_temp.') <= -1 ) {
//return;
//}
if( is_user_logged_in() ) {
UM()->files()->delete_core_user_photo( $user_id, 'profile_photo' );
}
$user_basedir = UM()->uploader()->get_upload_user_base_dir( $user_id, true );
$temp_dir = UM()->uploader()->get_core_temp_dir() . DIRECTORY_SEPARATOR;
$temp_profile_photo = array_slice( scandir( $temp_dir ), 2);
$temp_profile_id = isset( $_COOKIE['um-register-profile-photo'] ) ? $_COOKIE['um-register-profile-photo'] : null;
if( empty( $temp_profile_photo ) ) return;
foreach( $temp_profile_photo as $i => $p ){
if ( strpos($p, "_photo_{$temp_profile_id}_temp") !== false ) {
$profile_p = $p;
}
}
if( empty( $profile_p ) ) return;
$temp_image_path = $temp_dir . DIRECTORY_SEPARATOR . $profile_p;
$new_image_path = $user_basedir . DIRECTORY_SEPARATOR . $profile_p;
$image = wp_get_image_editor( $temp_image_path );
$file_info = wp_check_filetype_and_ext( $temp_image_path, $profile_p );
$ext = $file_info['ext'];
$new_image_name = str_replace( $profile_p, "profile_photo.{$ext}", $new_image_path );
$sizes = UM()->options()->get( 'photo_thumb_sizes' );
$quality = UM()->options()->get( 'image_compression' );
if ( ! is_wp_error( $image ) ) {
$image->save( $new_image_name );
$image->set_quality( $quality );
$sizes_array = array();
foreach( $sizes as $size ) {
$sizes_array[ ] = array ( 'width' => $size );
}
$image->multi_resize( $sizes_array );
delete_user_meta( $user_id, 'synced_profile_photo' );
update_user_meta( $user_id, 'profile_photo', "profile_photo.{$ext}" );
update_user_meta( $user_id, 'register_profile_photo', "profile_photo.{$ext}" );
#unlink( $temp_image_path );
}
}
add_action( 'um_after_user_account_updated', 'um_registration_set_profile_photo', 1, 2 );
add_action( 'um_registration_set_extra_data', 'um_registration_set_profile_photo', 1, 2 );
/**
* Set Temporary user id
*/
function um_register_profile_photo_set_temp_user_id() {
$temp_profile_id = isset( $_COOKIE['um-register-profile-photo'] ) ? $_COOKIE['um-register-profile-photo'] : null;
if ( ! $temp_profile_id ) {
setcookie( 'um-register-profile-photo', md5( time() ), time() + 3600, COOKIEPATH, COOKIE_DOMAIN );
}
}
add_action( 'template_redirect', 'um_register_profile_photo_set_temp_user_id' );
/**
* Set handler callback for filename
*/
function um_register_profile_photo_upload_handler( $override_handler ) {
if ( 'stream_photo' == UM()->uploader()->upload_image_type && 'register_profile_photo' == UM()->uploader()->field_key ) {
$override_handler['unique_filename_callback'] = 'um_register_profile_photo_name';
}
return $override_handler;
}
add_filter( 'um_image_upload_handler_overrides__register_profile_photo', 'um_register_profile_photo_upload_handler', 99999 );
/**
* Change filename
*/
function um_register_profile_photo_name( $dir, $filename, $ext ) {
$temp_profile_id = isset( $_COOKIE['um-register-profile-photo'] ) ? $_COOKIE['um-register-profile-photo'] : null;
return "profile_photo_{$temp_profile_id}_temp{$ext}";
}
/**
* Support profile photo uploader in Account form
*/
function um_register_display_profile_photo_in_account( $field_atts, $key, $data ) {
if ( 'register_profile_photo' == $key && um_is_core_page( 'account' ) ) {
$profile_photo = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . um_profile( 'profile_photo' ) . '?ts=' . current_time( 'timestamp' );
$field_atts['data-profile_photo'] = array( $profile_photo );
}
return $field_atts;
}
add_filter( 'um_field_extra_atts', 'um_register_display_profile_photo_in_account', 10, 3 );
/**
* Clear profile photo cache
*/
function um_register_display_profile_photo_script() {
if( ! um_is_core_page( 'account' ) ) return;
?>
<script type="text/javascript">
jQuery(document).on("ready", function(){
setTimeout(() => {
var register_profile_photo = jQuery("div[data-key='register_profile_photo']");
register_profile_photo.find(".um-field-area").find(".um-single-image-preview").find("img").attr("src", register_profile_photo.data("profile_photo"));
}, 1000);
var account_small_avatar = jQuery(".um-account-meta-img-b").find("a").find("img");
account_small_avatar.attr("src", account_small_avatar.attr("src") + "?ts=" + Math.floor(Date.now() / 1000) );
jQuery(document).ajaxSuccess(function(event, xhr, settings) {
if( typeof settings.data.indexOf !== "undefined" ){
if (settings.data.indexOf("action=um_resize_image") > -1) {
jQuery(".um-account .um-form form").submit();
}
}
});
});
</script>
<?php
}
add_action( 'wp_footer', 'um_register_display_profile_photo_script' );
/**
* Delete profile photo viam the account form
*/
function um_register_delete_profile_photo_from_account() {
if( isset( $_REQUEST['mode'] ) && "account" == $_REQUEST['mode'] ) {
UM()->files()->delete_core_user_photo( get_current_user_id(), 'profile_photo' );
}
wp_send_json_success();
}
add_action( 'wp_ajax_um_remove_file', 'um_register_delete_profile_photo_from_account', 1 );
When uploading the profile photo from the registration form with the 1:1 crop, it does not allow uploading a photo smaller than 600px, since it is not linked to the ultimate member settings.
This lock at 600px is located at line 1799 in the path ultimatemember/includes/core/class-fields.php
https://github.com/ultimatemember/ultimatemember/blob/master/includes/core/class-fields.php
if ( $array['min_width'] == '' && $array['crop'] == 1 ) {
$array['min_width'] = 600;
}
if ( $array['min_height'] == '' && $array['crop'] == 1 ) {
$array['min_height'] = 600;
}
if ( $array['min_width'] == '' && $array['crop'] == 3 ) {
$array['min_width'] = 600;
}
if ( $array['min_height'] == '' && $array['crop'] == 3 ) {
$array['min_height'] = 600;
Let's see if you can help me in the first code to unlock that 600px value and if you can, link it with the Ultimate Member profile photo settings.
Thank you very much for everything and greetings to the community.

Assign css class to php code for a shortcode

I have a lot of posts which have a particular shortcode like [rest massage=""][/rest] and a text in between, and also the text is unique that mean in all the site its not used, only in this shortcode. the shortcode doesn't have any class or id assigned. now I want to assign a css class to it so that i can design it. how can i do it with php or jquery.
I founded this in the shortcode.php
function rcp_restrict_shortcode( $atts, $content = null ) {
$atts = shortcode_atts( array(
'userlevel' => 'none',
'message' => '',
'paid' => false,
'level' => 0,
'subscription' => ''
), $atts, 'restrict' );
global $rcp_options, $user_ID;
if ( strlen( $atts['message'] ) > 0 ) {
$teaser = $atts['message'];
} else {
$teaser = rcp_get_restricted_content_message( ! empty( $atts['paid'] ) );
}
$subscriptions = array_map( 'trim', explode( ',', $atts['subscription'] ) );
$has_access = false;
$classes = 'rcp_restricted';
$customer = rcp_get_customer(); // currently logged in customer
$is_active = rcp_user_has_active_membership();
$has_access_level = rcp_user_has_access( get_current_user_id(), $atts['level'] );
if( $atts['paid'] ) {
if ( rcp_user_has_paid_membership() && $has_access_level ) {
$has_access = true;
}
$classes = 'rcp_restricted rcp_paid_only';
} elseif ( $has_access_level ) {
$has_access = true;
}
if ( ! empty( $subscriptions ) && ! empty( $subscriptions[0] ) ) {
if ( $is_active && ! empty( $customer ) && count( array_intersect( rcp_get_customer_membership_level_ids( $customer->get_id() ), $subscriptions ) ) ) {
$has_access = true;
} else {
$has_access = false;
}
}
if ( $atts['userlevel'] === 'none' && ! is_user_logged_in() ) {
$has_access = false;
}
if( 'none' != $atts['userlevel'] ) {
$roles = array_map( 'trim', explode( ',', $atts['userlevel'] ) );
foreach ( $roles as $role ) {
if ( current_user_can( strtolower( $role ) ) ) {
$has_access = true;
break;
} else {
$has_access = false;
}
}
}
plz help.

WP_List_Table Not showing any entries

Missing WP_List_Table Entries Screenshot Here
I'm trying to populate a list table in WordPress using WP_List_Table with country and state codes from WooCommerce, but it seems that either :display(), :prepare-items() or :column_default is failing me. Been staring myself blind for hours and I can't track down what's happening.
The class setup looks as follows:
<?php
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/screen.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class My_List_Table extends WP_List_Table {
public function __construct() {
parent::__construct(
array(
'singular' => __( 'state', 'my-text-domain' ),
'plural' => __( 'states', 'my-text-domain' ),
'ajax' => false,
)
);
}
public function get_columns() {
$columns = array(
'cb' => '<input type="checkbox" />',
'col_state_name' => __( 'Region Name', 'my-text-domain' ),
'col_state_id' => __( 'Region Code', 'my-text-domain' ),
);
return $columns;
}
public function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'col_state_id':
case 'col_state_name':
return $item[ $column_name ];
default:
return $item;
}
}
public function my_load_states() {
if ( get_option( 'my_countries' ) && get_option( 'my_current_country' ) && get_option( 'my_states_' . get_option( 'my_current_country' ) ) ) {
$states = get_option( 'my_states_' . get_option( 'my_current_country' ) );
update_option( 'myplugin', true );
return $states;
} else {
$states = WC()->countries->get_states( get_option( 'my_current_country' ) );
if ( is_array( $states ) ) {
return $states;
} else {
update_option( 'myplugin', true );
return '';
}
}
}
public function get_states() {
$data = array();
if ( get_option( 'my_states_' . get_option( 'my_current_country' ) ) && ! empty( get_option( 'my_states_' . get_option( 'my_current_country' ) ) ) && get_option( 'my_country' ) && in_array( get_option( 'my_current_country' ), get_option( 'my_countries' ), true ) ) {
$states = get_option( 'my_states_' . get_option( 'my_current_country' ) );
update_option( 'myplugin', true );
} else {
$states = $this->my_load_states();
}
if ( ! empty( $states ) ) {
foreach ( $states as $code => $name ) {
$temp = array(
'col_state_name' => $name,
'col_state_id' => $code,
);
array_push( $data, $temp );
}
return $data;
} else {
return '';
}
}
public function prepare_items() {
$this->_column_headers = $this->get_column_info();
$this->process_bulk_action();
$data = $this->get_states();
if ( ! empty( $data ) && is_array( $data ) ) {
usort( $data, array( &$this, 'usort_reorder' ) );
$per_page = count( $data );
$total_items = count( $data );
}
$current_page = $this->get_pagenum();
if ( is_array( $data ) && count( $data ) > 1 ) {
$found_data = array_slice( $data, ( ( $current_page - 1 ) * $per_page ), $per_page );
} else {
$found_data = $data;
$total_items = 0;
}
$this->set_pagination_args(
array(
'total_items' => $total_items,
'per_page' => $per_page,
)
);
$this->items = $found_data;
}
public function no_items() {
esc_html_e( 'No states avaliable.', 'my-text-domain' );
}
}
I'm initiating things through this:
<?php
class My_Admin {
...
public function my_add_menu_item() {
add_submenu_page(
'woocommerce',
'Locations',
'Locations',
'manage_options',
'my_states',
array( $this, 'my_settings_page' ),
);
}
public function my_settings_page() {
$slp_obj = new My_List_Table();
$slp_obj->prepare_items();
$slp_obj->display();
}
}
?>

Issues with PHP classes and WordPress

I am currently trying to add new options to the Rename wp-login.php because it is barely being updated. The owner also allowed me to add new options to it.
The plugin functionality allows you to 'rename' /wp-admin/ and wp-login.php to a different location. I barely have any knowledge about PHP classes and, you guessed it, the whole plugin is basically created using PHP classes.
What I am trying to do:
When the field is empty, revert everything to /wp-admin/. Basically a disable option; I've got that, but when the field is left blank and you'll save it, it saves the value which was entered before. That's a problem; It should be empty when you save it blank.
Currently the field is in the "Permalinks" section of the settings menu, but I want to put the option in a custom page.
Also, how does this plugin saves the value of the field? I can't find anything, not even a register_setting function.
When I say 'the field', I'm talking about this field: <input id="rwl-page-input" type="text" name="rwl_page" value="' . $this->new_login_slug() . '">
Code is as follows:
<?php
if ( defined( 'ABSPATH' ) && ! class_exists( 'Rename_WP_Login' ) ) {
class Rename_WP_Login {
private $wp_login_php;
private function basename() {
return plugin_basename( __FILE__ );
}
private function path() {
return trailingslashit( dirname( __FILE__ ) );
}
private function use_trailing_slashes() {
return '/' === substr( get_option( 'permalink_structure' ), -1, 1 );
}
private function user_trailingslashit( $string ) {
return $this->use_trailing_slashes() ? trailingslashit( $string ) : untrailingslashit( $string );
}
private function wp_template_loader() {
global $pagenow;
$pagenow = 'index.php';
if ( ! defined( 'WP_USE_THEMES' ) ) {
define( 'WP_USE_THEMES', true );
}
wp();
if ( $_SERVER['REQUEST_URI'] === $this->user_trailingslashit( str_repeat( '-/', 10 ) ) ) {
$_SERVER['REQUEST_URI'] = $this->user_trailingslashit( '/wp-login-php/' );
}
require_once ABSPATH . WPINC . '/template-loader.php';
die;
}
private function new_login_slug() {
if (
( $slug = get_option( 'rwl_page' ) ) || (
is_multisite() &&
is_plugin_active_for_network( $this->basename() ) &&
( $slug = get_site_option( 'rwl_page', 'login' ) )
) ||
( $slug = 'login' )
) {
return $slug;
}
}
public function new_login_url( $scheme = null ) {
if ( get_option( 'permalink_structure' ) ) {
return $this->user_trailingslashit( home_url( '/', $scheme ) . $this->new_login_slug() );
} else {
return home_url( '/', $scheme ) . '?' . $this->new_login_slug();
}
}
public function __construct() {
register_activation_hook( $this->basename(), array( $this, 'activate' ) );
register_uninstall_hook( $this->basename(), array( 'Rename_WP_Login', 'uninstall' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
add_action( 'network_admin_notices', array( $this, 'admin_notices' ) );
if ( is_multisite() && ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
add_filter( 'plugin_action_links_' . $this->basename(), array( $this, 'plugin_action_links' ) );
if ( is_multisite() && is_plugin_active_for_network( $this->basename() ) ) {
add_filter( 'network_admin_plugin_action_links_' . $this->basename(), array( $this, 'plugin_action_links' ) );
add_action( 'wpmu_options', array( $this, 'wpmu_options' ) );
add_action( 'update_wpmu_options', array( $this, 'update_wpmu_options' ) );
}
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 1 );
add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
add_filter( 'site_url', array( $this, 'site_url' ), 10, 4 );
add_filter( 'network_site_url', array( $this, 'network_site_url' ), 10, 3 );
add_filter( 'wp_redirect', array( $this, 'wp_redirect' ), 10, 2 );
add_filter( 'site_option_welcome_email', array( $this, 'welcome_email' ) );
remove_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );
}
public function activate() {
add_option( 'rwl_redirect', '1' );
}
public static function uninstall() {
global $wpdb;
if ( is_multisite() ) {
$blogs = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
if ( $blogs ) {
foreach ( $blogs as $blog ) {
switch_to_blog( $blog );
delete_option( 'rwl_page' );
}
restore_current_blog();
}
delete_site_option( 'rwl_page' );
} else {
delete_option( 'rwl_page' );
}
}
public function wpmu_options() {
echo (
'<h3>' .
_x( 'Rename wp-login.php', 'Text string for settings page', 'rename-wp-login' ) .
'</h3>' .
'<p>' .
__( 'This option allows you to set a networkwide default, which can be overridden by individual sites. Simply go to to the site’s permalink settings to change the url.', 'rename-wp-login' ) .
'</p>' .
'<table class="form-table">' .
'<tr valign="top">' .
'<th scope="row">' .
__( 'Networkwide default', 'rename-wp-login' ) .
'</th>' .
'<td>' .
'<input id="rwl-page-input" type="text" name="rwl_page" value="' . get_site_option( 'rwl_page', 'login' ) . '">' .
'</td>' .
'</tr>' .
'</table>'
);
}
public function update_wpmu_options() {
if (
( $rwl_page = sanitize_title_with_dashes( $_POST['rwl_page'] ) ) &&
strpos( $rwl_page, 'wp-login' ) === false &&
! in_array( $rwl_page, $this->forbidden_slugs() )
) {
update_site_option( 'rwl_page', $rwl_page );
}
}
public function admin_init() {
global $pagenow;
add_settings_section(
'rename-wp-login-section',
_x( 'Rename wp-login.php', 'Text string for settings page', 'rename-wp-login' ),
array( $this, 'rwl_section_desc' ),
'permalink'
);
add_settings_field(
'rwl-page',
'<label for="rwl-page">' . __( 'Login url', 'rename-wp-login' ) . '</label>',
array( $this, 'rwl_page_input' ),
'permalink',
'rename-wp-login-section'
);
if ( isset( $_POST['rwl_page'] ) && $pagenow === 'options-permalink.php' ) {
if (
( $rwl_page = sanitize_title_with_dashes( $_POST['rwl_page'] ) ) &&
strpos( $rwl_page, 'wp-login' ) === false &&
! in_array( $rwl_page, $this->forbidden_slugs() )
) {
if ( is_multisite() && $rwl_page === get_site_option( 'rwl_page', 'login' ) ) {
delete_option( 'rwl_page' );
} else {
update_option( 'rwl_page', $rwl_page );
}
}
}
if ( get_option( 'rwl_redirect' ) ) {
delete_option( 'rwl_redirect' );
if ( is_multisite() && is_super_admin() && is_plugin_active_for_network( $this->basename() ) ) {
$redirect = network_admin_url( 'settings.php#rwl-page-input' );
} else {
$redirect = admin_url( 'options-permalink.php#rwl-page-input' );
}
wp_safe_redirect( $redirect );
die;
}
}
public function rwl_section_desc() {
if ( is_multisite() && is_super_admin() && is_plugin_active_for_network( $this->basename() ) ) {
echo (
'<p>' .
sprintf(
__( 'To set a networkwide default, go to %s.', 'rename-wp-login' ),
'<a href="' . esc_url( network_admin_url( 'settings.php#rwl-page-input' ) ) . '">' .
__( 'Network Settings', 'rename-wp-login' ) .
'</a>'
) .
'</p>'
);
}
}
public function rwl_page_input() {
if ( get_option( 'permalink_structure' ) ) {
echo '<code>' . trailingslashit( home_url() ) . '</code> <input id="rwl-page-input" type="text" name="rwl_page" value="' . $this->new_login_slug() . '">' . ( $this->use_trailing_slashes() ? ' <code>/</code>' : '' );
} else {
echo '<code>' . trailingslashit( home_url() ) . '?</code> <input id="rwl-page-input" type="text" name="rwl_page" value="' . $this->new_login_slug() . '">';
}
}
public function admin_notices() {
global $pagenow;
if ( ! is_network_admin() && $pagenow === 'options-permalink.php' && isset( $_GET['settings-updated'] ) ) {
echo '<div class="notice notice-success is-dismissible"><p>' . sprintf( __( 'Your login page is now here: %s. Bookmark this page!', 'rename-wp-login' ), '<strong>' . $this->new_login_url() . '</strong>' ) . '</p></div>';
}
}
public function plugin_action_links( $links ) {
if ( is_network_admin() && is_plugin_active_for_network( $this->basename() ) ) {
array_unshift( $links,
'<a href="' . esc_url( network_admin_url( 'settings.php#rwl-page-input' ) ) . '">' .
__( 'Settings', 'rename-wp-login' ) .
'</a>'
);
} elseif ( ! is_network_admin() ) {
array_unshift( $links,
'<a href="' . esc_url( admin_url( 'options-permalink.php#rwl-page-input' ) ) . '">' .
__( 'Settings', 'rename-wp-login' ) .
'</a>'
);
}
return $links;
}
public function plugins_loaded() {
global $pagenow;
load_plugin_textdomain( 'rename-wp-login' );
if (
! is_multisite() && (
strpos( $_SERVER['REQUEST_URI'], 'wp-signup' ) !== false ||
strpos( $_SERVER['REQUEST_URI'], 'wp-activate' ) !== false
)
) {
wp_die( __( 'This feature is not enabled.', 'rename-wp-login' ), '', array( 'response' => 403 ) );
}
$request = parse_url( $_SERVER['REQUEST_URI'] );
if ( (
strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false ||
untrailingslashit( $request['path'] ) === site_url( 'wp-login', 'relative' )
) &&
! is_admin()
) {
$this->wp_login_php = true;
$_SERVER['REQUEST_URI'] = $this->user_trailingslashit( '/' . str_repeat( '-/', 10 ) );
$pagenow = 'index.php';
} elseif (
untrailingslashit( $request['path'] ) === home_url( $this->new_login_slug(), 'relative' ) || (
! get_option( 'permalink_structure' ) &&
isset( $_GET[$this->new_login_slug()] ) &&
empty( $_GET[$this->new_login_slug()] )
) ) {
$pagenow = 'wp-login.php';
}
}
public function wp_loaded() {
global $pagenow;
if ( is_admin() && ! is_user_logged_in() && ! defined( 'DOING_AJAX' ) ) {
wp_die( __( 'You must log in to access the admin area.', 'rename-wp-login' ), '', array( 'response' => 403 ) );
}
$request = parse_url( $_SERVER['REQUEST_URI'] );
if (
$pagenow === 'wp-login.php' &&
$request['path'] !== $this->user_trailingslashit( $request['path'] ) &&
get_option( 'permalink_structure' )
) {
wp_safe_redirect( $this->user_trailingslashit( $this->new_login_url() ) . ( ! empty( $_SERVER['QUERY_STRING'] ) ? '?' . $_SERVER['QUERY_STRING'] : '' ) );
die;
} elseif ( $this->wp_login_php ) {
if (
( $referer = wp_get_referer() ) &&
strpos( $referer, 'wp-activate.php' ) !== false &&
( $referer = parse_url( $referer ) ) &&
! empty( $referer['query'] )
) {
parse_str( $referer['query'], $referer );
if (
! empty( $referer['key'] ) &&
( $result = wpmu_activate_signup( $referer['key'] ) ) &&
is_wp_error( $result ) && (
$result->get_error_code() === 'already_active' ||
$result->get_error_code() === 'blog_taken'
) ) {
wp_safe_redirect( $this->new_login_url() . ( ! empty( $_SERVER['QUERY_STRING'] ) ? '?' . $_SERVER['QUERY_STRING'] : '' ) );
die;
}
}
$this->wp_template_loader();
} elseif ( $pagenow === 'wp-login.php' ) {
global $error, $interim_login, $action, $user_login;
#require_once ABSPATH . 'wp-login.php';
die;
}
}
public function site_url( $url, $path, $scheme, $blog_id ) {
return $this->filter_wp_login_php( $url, $scheme );
}
public function network_site_url( $url, $path, $scheme ) {
return $this->filter_wp_login_php( $url, $scheme );
}
public function wp_redirect( $location, $status ) {
return $this->filter_wp_login_php( $location );
}
public function filter_wp_login_php( $url, $scheme = null ) {
if ( strpos( $url, 'wp-login.php' ) !== false ) {
if ( is_ssl() ) {
$scheme = 'https';
}
$args = explode( '?', $url );
if ( isset( $args[1] ) ) {
parse_str( $args[1], $args );
$url = add_query_arg( $args, $this->new_login_url( $scheme ) );
} else {
$url = $this->new_login_url( $scheme );
}
}
return $url;
}
public function welcome_email( $value ) {
return str_replace( 'wp-login.php', trailingslashit( get_site_option( 'rwl_page', 'login' ) ), $value );
}
public function forbidden_slugs() {
$wp = new WP;
return array_merge( $wp->public_query_vars, $wp->private_query_vars );
}
}
new Rename_WP_Login;
}
I would really, really appreciate it if anyone can help me out with this!
The first
Also, how does this plugin saves the value of the field? I can't find anything, not even a register_setting function.
This plugin save the value by this function:
public function update_wpmu_options() {
if (
( $rwl_page = sanitize_title_with_dashes( $_POST['rwl_page'] ) ) &&
strpos( $rwl_page, 'wp-login' ) === false &&
! in_array( $rwl_page, $this->forbidden_slugs() )
) {
update_site_option( 'rwl_page', $rwl_page );
}
}
You can read more at: update_site_option
In admin_init function have
add_settings_field(
'rwl-page',
'<label for="rwl-page">' . __( 'Login url', 'rename-wp-login' ) . '</label>',
array( $this, 'rwl_page_input' ),
'permalink',
'rename-wp-login-section'
);
This function will get value saved. If you let the field blank this value won't be update. That's why you got old value of it.
If you want revert all to old version you can remove this plugin.
Hope it can help.
I'm just coder had try this plugin not dev it. xD

Categories