Wordpress sortable column by IP address - php

I have been able to add a sortable column to my Wordpress userpage that displays the users last know IP address.
The problem I seem to be having is getting the column to sort by the IP's numbers and not the usernames alphabetically.
I feel im missing something simple and could use another set of eyes.
<?php
class Register_IP_Multisite {
public function __construct() {
add_action( 'init', array( &$this, 'init' ) );
}
public function init() {
add_action( 'user_register', array( $this,'log_ip') );
add_action( 'edit_user_profile', array( $this,'edit_user_profile') );
add_action( 'manage_users_custom_column', array( $this,'columns'), 10, 3);
add_filter( 'manage_users_sortable_columns', array( $this ,'users_sortable_columns_wsp') );
add_filter( 'request', array( $this ,'users_orderby_column_wsp') );
add_filter( 'plugin_row_meta', array( $this ,'donate_link'), 10, 2 );
if ( is_multisite() ) {
add_filter('wpmu_users_columns', array( $this ,'column_header_signup_ip'));
} else {
add_filter('manage_users_columns', array( $this ,'column_header_signup_ip'));
}
}
public function log_ip($user_id){
$ip = $_SERVER['REMOTE_ADDR']; //Get the IP of the person registering
update_user_meta($user_id, 'signup_ip', $ip); //Add user metadata to the usermeta table
}
public function edit_user_profile() {
$user_id = (int) $_GET['user_id'];
?>
<h3><?php _e('Signup IP Address', 'register-ip-mutisite'); ?></h3>
<p style="text-indent:15px;"><?php
$ip_address = get_user_meta($user_id, 'signup_ip', true);
echo $ip_address;
?></p>
<?php
}
public function column_header_signup_ip($column_headers) {
$column_headers['signup_ip'] = __('IP Address', 'register-ip-multisite');
return $column_headers;
}
public function users_sortable_columns_wsp($column_headers) {
$customwsp = array(
// meta column id => sortby value used in query
'signup_ip' => 'user_ip_address',
);
return wp_parse_args($customwsp, $column_headers);
}
public function users_orderby_column_wsp( $varswsp ) {
if ( isset( $varswsp['orderby'] ) && 'signup_ip' == $varswsp['orderby'] ) {
$varswsp = array_merge( $varswsp, array(
'meta_key' => 'signup_ip',
'orderby' => 'meta_value'
) );
}
return $varswsp;
}
public function columns($value, $column_name, $user_id) {
global $modewsp;
$modewsp = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
if ( $column_name == 'signup_ip' ) {
$ip = get_user_meta($user_id, 'signup_ip', true);
if ($ip != ""){
$theip = $ip;
if ( has_filter('ripm_show_ip') ) {
$theip = apply_filters('ripm_show_ip', $theip);
}
return $theip;
} else {
$theip = '<em>'.__('None Recorded', 'register-ip-multisite').'</em>';
return $theip;
}
}
$user_ip_address = strtotime(get_date_from_gmt($user->signup_ip));
return $value;
}
}
new Register_IP_Multisite();

Related

Using gform_after_submission with gform_notification

I am trying to use gform_after_submission to grab a field value(url of a file) then pass the value to a notification so it can be sent as an attachment. This is what I have so far:
add_action("gform_after_submission", "after_submission", 10, 2);
function after_submission($entry, $form){
$pdf = $entry["3"];
return $pdf;
}
add_filter( 'gform_notification_3', 'add_notification_attachments', 10, 3 );
function add_notification_attachments( $notification, $form, $entry ) {
if ( $notification['name'] == 'Admin Notification' ) {
$path = 'path/to/file.pdf';
$notification['attachments'] = array( $path );
}
return $notification;
}
Untested but you shouldn't need the first action, only the notification filter.
add_filter( 'gform_notification_3', 'add_notification_attachments', 10, 3 );
function add_notification_attachments( $notification, $form, $entry ) {
if ( $notification['name'] == 'Admin Notification' ) {
$path = $entry['3'];
$notification['attachments'] = array( $path );
}
return $notification;
}
Thanks Dave, couldn't see your updated code but here's what I managed to do once you told me about converting the URL to a path & it now works perfectly:
function get_file_path_from_url( $file_url ){
return realpath($_SERVER['DOCUMENT_ROOT'] . parse_url( $file_url, PHP_URL_PATH ));
}
add_filter( 'gform_notification_3', 'add_notification_attachments', 10, 3 );
function add_notification_attachments( $notification, $form, $entry ) {
if ( $notification['name'] == 'Admin Notification' ) {
$path = $entry['3'];
$path = get_file_path_from_url($path);
$notification['attachments'] = array( $path );
}
return $notification;
}

Using ACF number field as price for WooCommerce custom product type

I've seen online that you can add all Custom Post Types to a WooCommerce cart as long as the CPT has a price field. The only issue is that you have to tell WooCommerce what CPT field contains the price.
Afterwords you can easily create an add-to-cart url like this:
https://yourdomain.com/?add-to-cart=XXX (XXX being the Custom Post Type post ID)
Why is this handy?
I have an online menu (just to inform my guest what we serve) but because of Corona we have to close our doors so I want these dishes to be ordered online.
The code should be something like this but the CPT is not added to cart:
add_filter('woocommerce_get_price', 'yl_get_dish_price', 20,2);
function yl_get_dish_price($price,$post) {
if ($post->post->post_type === 'dish') {
$price = get_field('price', $post->ID);
}
return $price;
}
UPDATE
I took this answer from here https://stackoverflow.com/a/60320662/10291365
class YL_Dish_Product extends WC_Product {
protected $post_type = 'dish';
public function get_type() {
return 'dish';
}
public function __construct( $product = 0 ) {
$this->supports[] = 'ajax_add_to_cart';
parent::__construct( $product );
}
// maybe overwrite other functions from WC_Product
}
class YL_Data_Store_CPT extends WC_Product_Data_Store_CPT {
public function read( &$product ) { // this is required
$product->set_defaults();
$post_object = get_post( $product->get_id() );
if ( ! $product->get_id() || ! $post_object || 'dish' !== $post_object->post_type ) {
throw new Exception( __( 'Invalid product.', 'woocommerce' ) );
}
$product->set_props(
array(
'name' => $post_object->post_title,
'slug' => $post_object->post_name,
'date_created' => 0 < $post_object->post_date_gmt ? wc_string_to_timestamp( $post_object->post_date_gmt ) : null,
'date_modified' => 0 < $post_object->post_modified_gmt ? wc_string_to_timestamp( $post_object->post_modified_gmt ) : null,
'status' => $post_object->post_status,
'description' => $post_object->post_content,
'short_description' => $post_object->post_excerpt,
'parent_id' => $post_object->post_parent,
'menu_order' => $post_object->menu_order,
'reviews_allowed' => 'open' === $post_object->comment_status,
)
);
$this->read_attributes( $product );
$this->read_downloads( $product );
$this->read_visibility( $product );
$this->read_product_data( $product );
$this->read_extra_data( $product );
$product->set_object_read( true );
}
// maybe overwrite other functions from WC_Product_Data_Store_CPT
}
class YL_WC_Order_Item_Product extends WC_Order_Item_Product {
public function set_product_id( $value ) {
if ( $value > 0 && 'dish' !== get_post_type( absint( $value ) ) ) {
$this->error( 'order_item_product_invalid_product_id', __( 'Invalid product ID', 'woocommerce' ) );
}
$this->set_prop( 'product_id', absint( $value ) );
}
}
function YL_woocommerce_data_stores( $stores ) {
// the search is made for product-$post_type so note the required 'product-' in key name
$stores['product-dish'] = 'YL_Data_Store_CPT';
return $stores;
}
add_filter( 'woocommerce_data_stores', 'YL_woocommerce_data_stores' , 11, 1 );
function YL_woo_product_class( $class_name , $product_type , $product_id ) {
if ($product_type == 'dish')
$class_name = 'YL_Dish_Product';
return $class_name;
}
add_filter('woocommerce_product_class','YL_woo_product_class',25,3 );
function my_woocommerce_product_get_price( $price, $product ) {
if ($product->get_type() == 'dish' ) {
$price = 10; // or get price how ever you see fit
}
return $price;
}
add_filter('woocommerce_get_price','my_woocommerce_product_get_price',20,2);
add_filter('woocommerce_product_get_price', 'my_woocommerce_product_get_price', 10, 2 );
// required function for allowing posty_type to be added; maybe not the best but it works
function YL_woo_product_type($false,$product_id) {
if ($false === false) { // don't know why, but this is how woo does it
global $post;
// maybe redo it someday?!
if (is_object($post) && !empty($post)) { // post is set
if ($post->post_type == 'dish' && $post->ID == $product_id)
return 'dish';
else {
$product = get_post( $product_id );
if (is_object($product) && !is_wp_error($product)) { // post not set but it's a dish
if ($product->post_type == 'dish')
return 'dish';
} // end if
}
} else if(wp_doing_ajax()) { // has post set (usefull when adding using ajax)
$product_post = get_post( $product_id );
if ($product_post->post_type == 'dish')
return 'dish';
} else {
$product = get_post( $product_id );
if (is_object($product) && !is_wp_error($product)) { // post not set but it's a dish
if ($product->post_type == 'dish')
return 'dish';
} // end if
} // end if // end if
} // end if
return false;
}
add_filter('woocommerce_product_type_query','YL_woo_product_type',12,2 );
function YL_woocommerce_checkout_create_order_line_item_object($item, $cart_item_key, $values, $order) {
$product = $values['data'];
if ($product->get_type() == 'dish') {
return new YL_WC_Order_Item_Product();
} // end if
return $item ;
}
add_filter( 'woocommerce_checkout_create_order_line_item_object', 'YL_woocommerce_checkout_create_order_line_item_object', 20, 4 );
function cod_woocommerce_checkout_create_order_line_item($item,$cart_item_key,$values,$order) {
if ($values['data']->get_type() == 'dish') {
$item->update_meta_data( '_dish', 'yes' ); // add a way to recognize custom post type in ordered items
return;
} // end if
}
add_action( 'woocommerce_checkout_create_order_line_item', 'cod_woocommerce_checkout_create_order_line_item', 20, 4 );
function YL_woocommerce_get_order_item_classname($classname, $item_type, $id) {
global $wpdb;
$is_IA = $wpdb->get_var("SELECT meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = {$id} AND meta_key = '_dish'");
if ('yes' === $is_IA) { // load the new class if the item is our custom post
$classname = 'YL_WC_Order_Item_Product';
} // end if
return $classname;
}
add_filter( 'woocommerce_get_order_item_classname', 'YL_woocommerce_get_order_item_classname', 20, 3 );
The above code does add a CPT to your cart (GREAT!!) but the price is always set to 10,00
So the code below doesn't give the right price :(
add_filter('woocommerce_get_price', 'yl_get_dish_price', 20,2);
function yl_get_dish_price($price,$post) {
if ($post->post->post_type === 'dish') {
$price = get_field('price', $post->ID);
}
return $price;
}
Any idea?
Since WooCommerce 3, the hook woocommerce_get_price is obsolete and deprecated… It's replaced by the following composite hook:
add_filter( 'woocommerce_product_get_price', 'yl_get_dish_price', 20, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'yl_get_dish_price', 20, 2 );
function yl_get_dish_price( $price, $product ) {
if ( $product->is_type('dish') ) {
$price = get_field( 'price', $product->get_id() );
}
return $price;
}
It could and should better work.

WordPress: Order admin user column by number

I want to order an admin column in the user overview list by a custom ID.
To do so, I added a function to sort the column but it doesn't work as expected.
It kind of sorts the fields but in a strange way. I guess there is an other sorting in place or the function has an error?!
This is the code for adding the ID to the user:
add_action( 'edit_user_profile', 'wcv_store_custom_vendor_id', 0 );
//add_action( 'wcv_admin_after_store_address', 'wcv_store_custom_vendor_id' );
function wcv_store_custom_vendor_id( $user ) {
?>
<table class="form-table">
<tbody>
<tr>
<th><label for="_wcv_custom_vendor_id"><?php _e( 'Vendor ID', 'wcvendors-pro' ); ?></label></th>
<td><input type="text" name="_wcv_custom_vendor_id" id="_wcv_custom_vendor_id" value="<?php echo get_user_meta( $user->ID, '_wcv_custom_vendor_id', true ); ?>" class="regular-text"></td>
</tr>
</tbody>
</table>
<?php
}
// Save the details on the back end when updating the user
add_action( 'wcvendors_update_admin_user', 'save_wcv_custom_vendor_id' );
function save_wcv_custom_vendor_id( $user_id ){
if ( isset( $_POST['_wcv_custom_vendor_id'] ) ) {
update_user_meta( $user_id, '_wcv_custom_vendor_id', $_POST['_wcv_custom_vendor_id'] );
}
Here's what I have so far:
// Add column to admin
add_action('manage_users_columns', 'add_custom_vendor_id_column', 10, 1 );
function add_custom_vendor_id_column( $columns ) {
$columns['wcv_custom_vendor_id'] = __('Vendor-ID');
return $columns;
}
// fetching the status, thanks to LoicTheAztec
add_filter('manage_users_custom_column', 'add_data_to_vendor_id_column', 10, 3);
function add_data_to_vendor_id_column( $value, $column_name, $user_id ) {
if ( 'wcv_custom_vendor_id' == $column_name ) {
if( get_user_meta( $user_id, '_wcv_custom_vendor_id', true ) != '' ) {
$value = '<span style="color:green;font-weight:bold;">'.get_user_meta( $user_id, '_wcv_custom_vendor_id', true ).'</span>';
} else {
$value = '<span class="" style="color:red;font-weight:bold;">No ID!</span>';
}
}
return $value;
}
// make admin colum sortable
function fc_my_sortable_cake_column( $columns ) {
$columns['wcv_custom_vendor_id'] = 'Vendor-ID';
return $columns;
}
add_filter( 'manage_users_sortable_columns', 'fc_my_sortable_cake_column' );
// second try to sort by number, no effect?!
function sort_datanowa_column( $vars ) {
if ( isset( $vars['orderby'] ) && 'Vendor-ID' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => '_wcv_custom_vendor_id', // Find correct meta field key
'orderby' => 'meta_value_num'
) );
}
return $vars;
}
add_filter( 'request', 'sort_datanowa_column' );
I checked a lot of snippets and the last function should sort by number. But I think it has no effect at all.
Is there anythin I miss?
Use pre_get_users opposite request
For empty values ​​for the meta_key we use the following: update_user_meta( $user_id, '_wcv_custom_vendor_id', 0 );
So you get:
// Add column to admin
function add_custom_vendor_id_column( $columns ) {
$columns['wcv_custom_vendor_id'] = __( 'Vendor-ID', 'woocommerce');
return $columns;
}
add_action( 'manage_users_columns', 'add_custom_vendor_id_column', 10, 1 );
// fetching the status, thanks to LoicTheAztec
function add_data_to_vendor_id_column( $value, $column_name, $user_id ) {
if ( $column_name == 'wcv_custom_vendor_id' ) {
$vendor_id = (int) get_user_meta( $user_id, '_wcv_custom_vendor_id', true );
// If empty
if ( empty ($vendor_id ) ) {
update_user_meta( $user_id, '_wcv_custom_vendor_id', 0 );
}
if ( $vendor_id && $vendor_id != 0 ) {
$value .= '<span style="color:green;font-weight:bold;">' . $vendor_id . '</span>';
} else {
$value = '<span class="" style="color:red;font-weight:bold;">No ID!</span>';
}
}
return $value;
}
add_filter( 'manage_users_custom_column', 'add_data_to_vendor_id_column', 10, 3);
// make admin colum sortable
function fc_my_sortable_cake_column( $columns ) {
$columns['wcv_custom_vendor_id'] = __( 'Vendor-ID', 'woocommerce');
return $columns;
}
add_filter( 'manage_users_sortable_columns', 'fc_my_sortable_cake_column', 10, 1 );
// orderby
function action_pre_get_users( $query ) {
if ( !is_admin() )
return;
$orderby = $query->get('orderby');
if ( $orderby == 'Vendor-ID' ) {
$query->set( 'orderby' , 'meta_value_num' );
$query->set( 'meta_key', '_wcv_custom_vendor_id' );
}
}
add_action( 'pre_get_users', 'action_pre_get_users', 10, 1 );

How do I get the value of a variable inside another

I'm trying to get the $pi1 and $pi2 variable values inside of the add_custom_price function, but nothing seems to be working.
I have looked at setting variables to be accessible from function classes but I'm not sure I understand how to access them correctly.
add_filter( 'gform_confirmation', array(gravity_pi,custom_confirmation), 10, 4 );
class gravity_pi {
public $pi1;
public $pi2;
public function custom_confirmation( $confirmation, $form, $entry, $ajax, $product_id, $pi1, $pi2 ) {
if( $form['id'] == '2' ) {
$post = get_post( $entry['post_id'] );
$this->pi1 = rgar( $entry, '20' );
$this->pi2 = rgar( $entry, '21' );
$exclude_list = array("pi24","pi64","pi65","pi66","pi67","pi68","pi69","pi70","pi71","pi72","pi73","pi74","pi75","pi76","pi77","pi78","pi79","pi80","pi81","pi82");
if(!in_array($this->pi1, $exclude_list) && !empty($this->pi1)){
$target_product_id = '86';
$pid1 = '86';
}else{
$pid1 = preg_replace("/[^0-9,.]/", "", $this->pi1 );
}
if(!in_array($pi2, $exclude_list) && !empty($this->pi2)){
$target_product_id = '87';
$pid2 = '87';
}else{
$pid2 = preg_replace("/[^0-9,.]/", "", $this->pi2);
}
$product_ids = ''.$pid1.','.$pid2.'';
$url = 'https://*****.com/cart/?add-to-cart='.$product_ids.'';
$confirmation = array( 'redirect' => $url );
}
return $confirmation;
}
public function add_custom_price( $cart_object, $entry,$form, $field, $input_id ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
if( 86 == $value['data']->id ) {
$value['data']->set_price( $this->pi1 );
}
if( 87 == $value['data']->id ) {
$value['data']->set_price( $this->pi2 );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', array(gravity_pi,add_custom_price));
You need to use $this to get/set any property in the class.
// set pi1 property value
$this->pi1 = 10;
//print/get property value
echo $this->pi1;
When you're setting the class properties (pi1 & pi2) you need to reference them in the same way you access them. Ex.
<?php
...
$this->pi1 = rgar($entry, '20');
Change all references (except where you declare them) of $pid1 to $this->pid1. Do this for $pid2 too.
Check this video out - https://www.youtube.com/watch?v=4c4nP7GLL1c

How can i change published text under date column in wordpress?

I want to change text of published under date column of post list in wordpress.
see below image
You can try using following way.
<?php
function my_custom_columns( $columns ) {
unset( $columns['date'] );
$columns['mydate'] = 'My Custom Date';
return $columns;
}
function my_format_column( $column_name , $post_id ) {
if($column_name == 'mydate'){
echo get_the_time( 'l, F j, Y', $post_id )."<br>".get_post_status( $post_id );
}
}
function my_column_register_sortable( $columns ) {
$columns['mydate'] = 'mydate';
return $columns;
}
function my_column_orderby( $vars ) {
if ( isset( $vars['orderby'] ) && 'mydate' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'orderby' => 'date'
) );
}
return $vars;
}
function my_column_init() {
add_filter( 'manage_posts_columns' , 'my_custom_columns' );
add_action( 'manage_posts_custom_column' , 'my_format_column' , 10 , 2 );
add_filter( 'manage_edit-post_sortable_columns', 'my_column_register_sortable' );
add_filter( 'request', 'my_column_orderby' );
}
add_action( 'admin_init' , 'my_column_init' );
?>

Categories