Uniquely Identy Wordpress Installation - wordpress

I am working on an API for wordpress and I am looking for a way to uniquely identify a wordpress installation that is requesting the API. Does anyone know of a install id or unique wordpress blog id? Is that even a thing?

I have written 2 APIs and have worked with several, I stumbled upon this question long time back...
I like to keep the stuff simple... so I use site_url() as identity of the blog, since I'm sure no other installation would run on the same site url so whenever there is an issue to debug, I know site ID is the url.
Saved one extra field in my DB ( If I had ID different, I'd need another field for site url ).
You can also go for complex solutions like using md5 of site_url along with some random generated strings to name a few.
Update
This will generate an ID on activation and save it in the database.
function generate_password($length = 12) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$password = '';
for ($i = 0; $i < $length; $i++) {
$password. = substr($chars, rand(0, strlen($chars) - 1), 1);
}
return $password;
}
function my_plugin_activated() {
$id = generate_password();
add_option('my_plugin_installation_id', $id);
}
register_activation_hook( __FILE__, 'my_plugin_activated' );
Code in my_plugin_activated() is executed only once on activation of plugin ( you can do the same for theme šŸ˜‰ ), you can, in activation hook itself, make a call to your api and get the site registered in your db.
Hope that helps.

Related

Determine which Avada Fusion Builder Elements are being used on a Wordpress site

Is there any way to determine which Fusion Builder Elements are being currently used on a WordPress site?
I have a lot of the elements enabled for a site I'm working on, and I'd like to disable the ones I don't need. Problem is, it's a full site I've taken on from another developer and I REAAAALLY don't want to have to scour the entire site and check for potentially broken elements every time I disable something.
Is this a functionality somewhere within Avada I'm completely missing and I can't find documented anywhere?
I was thinking of writing a tiny plugin that checks the wp_posts table and searching for [fusion_xxxx] elements and displaying a list of used builder elements. Then I can determine which are used throughout the site.
Is this a bad time for any reason?
I wrote a plugin that will run a query on the DB and display used elements and the number of times each is used. Simple, but I think it gets the job done. I very-well might be missing something very obvious about how the info is stored.
This is done in an ajax call, displayed in the browser in a formatted list, etc.
Anywho, here's the part of the plugin that actually gathers the info:
protected function getElementsFromTable($table, $field, &$element_collection)
{
global $wpdb;
$sql = "SELECT * FROM " . $table . " WHERE `" . $field . "` LIKE '%[/fusion_%';";
$results = $wpdb->get_results($sql);
foreach ($results as $item) {
preg_match_all("/\[\/fusion_(.+?)\]/", $item->{$field}, $elements);
// No results found, bail!
if (!count($elements[1])) {
continue;
}
// Store the results
foreach ($elements[1] as $element) {
if (!array_key_exists('fusion_' . $element, $element_collection)) {
$element_collection["fusion_" . $element] = 1;
} else {
$element_collection["fusion_" . $element]++;
}
}
}
}
arsort($element_collection);
Usage:
$this->getElementsFromTable("wp_posts", "post_conent", $element_collection);

How to get Pay Now URL with custom order status in WooCommerce?

I want to get the URL from where the customer can directly pay for their Invoice and also it should work with wc-cancelled and wc-transaction-declined (custom order status).
My Solution
What I'm doing now is created a custom page with my custom get parameters and processing the whole Payment Process as Documentation in Gateway provider Website.
My Problem
But the problem is whenever they update their doc file and plugin I also have to update my code; but if I get the Pay Now URL then WooCommerce and Gateway Plugin will take care of it.
Is there a better solution?
I got the solution in WooCommerce templates/emails/customer-invoice.php file. The function that I was looking for is get_checkout_payment_url().
Usage
$order = wc_get_order($order_id);
$pay_now_url = esc_url( $order->get_checkout_payment_url() );
echo $pay_now_url; //http://example.com/checkout/order-pay/{order_id}?pay_for_order=true&key={order_key}
//http://example.com will be site_url and protocol will depending upon SSL checkout WooCommerce setting.
But this url only works with pending, failed order status; So I used filter woocommerce_valid_order_statuses_for_payment
if (!function_exists('filter_woocommerce_valid_order_statuses_for_payment')) {
//http://woocommerce.wp-a2z.org/oik_api/wc_abstract_orderneeds_payment/
//http://hookr.io/filters/woocommerce_valid_order_statuses_for_payment/
// define the woocommerce_valid_order_statuses_for_payment callbackĀ 
function filter_woocommerce_valid_order_statuses_for_payment( $array, $instance ) {
$my_order_status = array('cancelled', 'transaction-declined');
return array_merge($array, $my_order_status);
}
// add the filterĀ 
add_filter('woocommerce_valid_order_statuses_for_payment', 'filter_woocommerce_valid_order_statuses_for_payment', 10, 2);
}
^^ I added this in my active theme's functions.php file.
Reference:
get_checkout_payment_url()
wc_abstract_orderneeds_payment
woocommerce_valid_order_statuses_for_payment
You can get url with below code but it will work for wc-pending status order only, with the help of order_id or post_id
$order = wc_get_order($order_id);
echo $pay_now_url = $order->get_checkout_payment_url();

How to get data from array object in WordPress plugin api

http://localhost/wordpress/give-api/forms/?key=15443f18029e6f5d3b65d04e1640ffbe&token=c3de770a410282359413c74a588c5c74
The above link is a plugin api link. Above link won't work to your browser.
when I set the above link in the browser , it returns array object like http://postimg.org/image/6ozmjy0e7/ .
My question is , how can I set this url in a variable in wordpress and how can I get the data from that array object. I just want to get the data from that array object. If any other process is available, then please suggest me. Thanks...
In functions.php:
function displayApiUrl() {
global $apiUrl; // you probably don't actually need to set it global as it is a function
$apiUrl = 'http://localhost/wordpress/give-api/forms/?key=15443f18029e6f5d3b65d04e1640ffbe&token=c3de770a410282359413c74a588c5c74';
return $apiUrl;
}
In your theme you can now use:
<?php $api = displayApiUrl(); ?>
With that you can process your array in a foreach loop:
<?php
$json_url = file_get_contents($api);
$json_data = json_decode($json_url, true);
foreach ($json_data['forms'] as $form) {
$form_id = $form['info']['id'];
echo $form_id;
}
?>
The new "standard" for WordPress rest apis is Json Rest API, which will be partially integrated into WordPress core in the next release.
You can get it here https://wordpress.org/plugins/json-rest-api/ and documentation at http://wp-api.org/
In terms of the question how to put array information into the URL, the format is
http://www.example.com/wp-json/endpoint?array_1[key1]=Pensacola&array_1[key2]=Florida
The URL of course changes, and the wp-json/endpoint is replaced with whatever the final endpoint is for which ever rest api you choose to use.

how can we get details from another database inside a plugin in wordpress

I created a plugin in wordpress to display earnings by using a shortcode.The details to display while using shortcode are stored in another database.I used direct database connection in plugin to fetch details from the that database.I used the following code
function earnings_shortcode($atts, $content, $tag)
{ //echo $atts[0];echo '<br>';
$str=base64_encode(1);
base64_decode($str);
$length = 4;
$res = trim(preg_replace("/[^0-9]/", "", $atts[0]));
$mydb1 = new wpdb('root','','db_test','localhost');
$rows = $mydb1->get_row("SELECT total,paydate FROM `tbl_shotcode` WHERE userid = $res", ARRAY_A);
echo "Payout on -" .$rows['paydate']; echo '<br/>';
echo "Total for next Pay Period:-" .$rows['total'];
}
Is there any better option to access another database inside a plugin with out hard coding the username and password.please suggest a solution.
No, you have to access the database and be authenticated to do queries. You can't get into a properly configured database without login in.
You can, however, make your $mydb1 variable global and define it at the top of your file (or in your constructor class) to be accessed by all your functions. I recommend putting it in a class to manage it more easily.

Module field with feeds, module generating data

I have an issue with triming a field before it is saved. I wanted to use substr(), or regex() with preg_match(). I have built a Drupal 7 module, but it can't work at all. I have tried using the trim plugin in feeds tamper module, but it doesn't seem to work. The data I am using is from a feed from Google Alerts. I have posted this issue here.
This is what I have done so far, and I know my regular expression is wrong; I was trying to get it do anything, just to see if I could get it to work, but I am pretty lost on how to add this type of function to a Drupal module.
function sub_node_save() {
$url = $node->field_web_screenhot['und'][0]['url'];
$url = preg_match('~^(http|ftp)(s)?\:\/\/((([a-z0-9\-]*)(\.))+[a-z0-9]*)($|/.*$)~i',$url );
$node->field_web_screenhot['und'][0]['url'] =$url;
return ;
}
I used the Devel module to get the field.
If there's an easy way to use substr(), I would consider that or something else.
Basically, I just want to take the Google redirect off the URL, so it is just the basic URL to the web site.
Depending on your question and later comments, I'd suggesting using node_presave hook (http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_presave/7) for this.
It's called before both insert (new) and update ops so you will need extra validations to prevent it from executing on node updates if you want.
<?php
function MYMODULE_node_presave($node) {
// check if nodetype is "mytype"
if ($node->type == 'mytype'){
// PHP's parse_url to get params set to an array.
$parts = parse_url($node->field_web_screenhot['und'][0]['url']);
// Now we explode the params by "&" to get the URL.
$queryParts = explode('&', $parts['query']);
$params = array();
foreach ($queryParts as $param) {
$item = explode('=', $param);
$params[$item[0]] = $item[1];
}
//valid_url validates the URL (duh!), urldecode() makes the URL an actual one with fixing "//" in http, q is from the URL you provided.
if (valid_url(urldecode($parms['q']))){
$node->field_web_screenhot['und'][0]['url'] = urldecode($parms['q']);
}
}
}

Resources