Call to undefined function request_currencies() - wordpress

WordPress 5.2.4
class ved_currencies extends WP_Widget {
function __construct() {
parent::__construct(
‘ved_currencies’,
"Ved currencies",
array( 'description' =>'Show currencies'));
}
private function request_currencies(){
$date_req= time();
$currencies = ["USD", "CNY", "EUR", "JPY", "BYN", "KZT", "UAH"];
}
public function widget( $args, $instance ){
request_currencies(); // Line 30.
echo "test";
}
}
Result:
Uncaught Error
: Call to undefined function request_currencies() in
C:\OSPanel\domains\ved\wp-content\plugins\ved-currencies\ved-currencies.php
on line
30
Line 30 is marked in the code example.
Could you help me understand why this error appeared?

this keyword is used inside a class, generally withing the member functions to access non-static members of a class(variables or functions) for the current object.
class ved_currencies extends WP_Widget {
function __construct() {
parent::__construct(
‘ved_currencies’,
"Ved currencies",
array( 'description' =>'Show currencies'));
}
private function request_currencies(){
$date_req= time();
$currencies = ["USD", "CNY", "EUR", "JPY", "BYN", "KZT", "UAH"];
}
public function widget( $args, $instance ){
$this->request_currencies(); // Line 30.
echo "test";
}
}
I have call the function using $this->request_currencies();
For your better understanding please visit this link

Related

register_rest_route is not passing me the callback function parameters

I have a class to register and execute my custom endpoints.
If I register my endpoints outside a class I can get the parameters of my endpoint in my callback function. But inside a class the parameters are not passing
Here the code:
class MYPLUGIN_RestAPI {
public function __construct() {
add_action('rest_api_init', array($this, register_routes));
}
public function register_routes (){
register_rest_route (
'myplugin/v1',
'/book/(?P<id>[a-z0-9 .\-]+)',
array(
'methods' => 'GET',
'callback' => array($this, "endpoint_book_cb")
)
);
}
public function endpoint_book_cb($data){
$result['code'] = 200;
$result['message'] = "Horrayyy!!!!!!!";
$result['data'] = $data;
return $result;
}
}
new MYPLUGIN_RestAPI();
Here what I get when I run this endpoint
{"code":200,"message":"Horrayyy!!!!!!!","data":{}}
Any Ideas why?
Found the solution:
public function wpfm_endpoints_cb(WP_REST_Request $request){
$result['code'] = 200;
$result['message'] = "Horrayyy!!!!!!!";
$result['data'] = $request->get_params();
return $result;
}

Unit Testing action hook

I want to perform unit tests on a Class, my goal is: I want to check if the plugin is activated or not by using the function: is_plugin_active
class WC_Custom_Variable_Products_Dependencies {
public function __construct() {
add_action( 'admin_init', [$this, 'check_environment']);
}
public function check_environment(){
return is_plugin_active(
'woocommerce-custom-variable-products/woocommerce-custom-variable-products.php'
);
}
}
CLass de test :
require_once 'class-wc-custom-variable-products-dependencies.php';
class WC_Custom_Variable_Products_DependenciesTest extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
$this->class_instance = new WC_Custom_Variable_Products_Dependencies();
}
public function test_check_environment(){
$result = $this->class_instance->check_environment();
$this->assertTrue($result);
}
The assertion return always False .
My plugin is activated, and the function is_plugin_active returns True if I execute it from the browser:
add_action('admin_init', function(){
var_dump(is_plugin_active(
'woocommerce-custom-variable-products/woocommerce-custom-variable-products.php'
));
});
I think the admin_init hook is not executed in the test. is it true or not?
I found out why. here is the solution: you have to activate the plugin in the tests / bootstrap.php file:
$GLOBALS[ 'wp_tests_options' ] = array(
'active_plugins' => array(
'YOUR-PLUGIN/YOUR-PLUGIN.php'
)
)

Error while adding custom widget in sage theme

I am new to sage theme. I am trying to add custom widget in lib/setup.php file, but i get Class 'Roots\Sage\Setup\WP_Widget' not found in {path} error.
Following is my code :
class Banner_Widget extends WP_Widget {
function __construct() {
$this->WP_Widget('Banner-Widget', __('Banner Widget', 'blogerist'), $widget_ops);
add_action('save_post', array(&$this, 'flush_widget_cache'));
add_action('deleted_post', array(&$this, 'flush_widget_cache'));
add_action('switch_theme', array(&$this, 'flush_widget_cache'));
}
public function form($instance) {
//form content
}
function widget($args, $instance) {
//widget content
}
function update($new_instance, $old_instance) {
$instance = array_map('strip_tags', $new_instance);
$this->flush_widget_cache();
$alloptions = wp_cache_get('alloptions', 'options');
if (isset($alloptions['Banner-Widget'])) {
delete_option('Banner-Widget');
}
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('Banner-Widget', 'widget');
}
}
When you want to create your class by extending the WP_Widget then you should use the global namespace.
Add a \ sign before WP_Customize_Control.
class Banner_Widget extends \WP_Widget {
//....
}

How to extend a custom widget in wordpress?

I am talking about making a widget which extends a child widget of WP_Widget.
Using this as reference: https://wordpress.stackexchange.com/questions/101438/how-to-extend-a-wp-widget-twice
Example:
My_WidgetBase extends WP_Widget
My_Widget extends My_WidgetBase
I want to know how to get My_Widget to show up with my other widgets (it is not currently). I have gotten My_WidgetBase to work.
This is important for my framework. I understand that widget(), update(), and form() must be overridden, but I could not find anything about making a grandchild of WP_Widget anywhere.
Example:
class My_WidgetBase extends WP_Widget {
public function __construct($id = 'my-widget-base', $desc = 'My WidgetBase', $opts = array()) {
$widget_ops = array();
parent::__construct( $id, $desc, $widget_ops );
}
public function widget( $args, $instance ) {
die('function WP_Widget::widget() must be over-ridden in a sub-class.');
}
public function update( $new_instance, $old_instance ) {
return $new_instance;
}
public function form( $instance ) {
echo '<p class="no-options-widget">' . __('There are no options for this widget.') . '</p>';
return 'noform';
}
}
function RegisterMyWidgets() {
register_widget('My_WidgetBase');
}
add_action( 'widgets_init', 'RegisterMyWidgets' );
Solved digging further into this thread: https://wordpress.stackexchange.com/questions/101438/how-to-extend-a-wp-widget-twice
The problem was not with my implementation of the classes, but how I included the parent in my framework. I was not working with both child and grandchild in the same php file, but two separate files with different directories.
Solving this problem was a matter of using require_once() correctly in the directory of my grandchild class.

Extend wpdb in other class - when use get_results for select gives me null

I've added custom plugin (created by me) in WP in that plugin I have Class named BaseModel, which extends wpdb.
The problem here is everytime when I try to run query I get false or null or empty array as result.
class BaseModel extends wpdb{
public function __construct(){
parent::__construct(DB_HOST, DB_USER, DB_PASS, DB_NAME);
}
function get_destinations($limit, $order){
$query = "SELECT * FROM wp_relations";
$result = $this->get_results($query, ARRAY_A);
var_dump($result);
}
function get_total_destinations(){
}}
Can some one tell me what is wrong?
Thanks.
Actually it is not a full OOP solution but I solve this by adding global $wpdb into my functions.
class BaseModel {
function get_destinations($limit, $order){
global $wpdb;
$query = "SELECT * FROM wp_relations";
$result = $wpdb->get_results($query, ARRAY_A);
var_dump($result);
}
function get_total_destinations(){
}}
I hope you will find this helpful.
More Info WordPress tests with wpdb
<?php
class testWPDB extends wpdb {
function prepare( $query, $arguments ){
return vsprintf( $query, $arguments );
}
}
class UTCW_Test_Data extends WP_UnitTestCase {
protected $utcw;
function setUp(){
$this->utcw = UTCW_Plugin::get_instance();
}
function getWPDBMock(){
return $this->getMock( 'testWPDB', array( 'get_results' ), array(), '', false );
}
function test_author(){
$instance[ 'authors' ] = array( 1, 2, 3 );
$config = new UTCW_Config( $instance, $this->utcw );
$db = $this->getWPDBMock( 'get_results' );
$db->expects( $this->once() )
->method( 'get_results' )
->with( $this->stringContains( 'post_author IN (1,2,3)' ) );
$data = new UTCW_Data( $config, $db );
$data->get_terms();
}
}
I dont think you want to extend from it? If this class will always be loaded inside Wordpress files then you will have access to the global $wpdb.
class RandomClass {
private $wpdb = false;
public function __construct() {
global $wpdb;
if (is_object($wpdb)) {
$this->wpdb = $wpdb;
}
}
public function get_results($data) {
return $this->wpdb->get_results($data);
}
}

Resources