Fatal error: Uncaught Error: Using $this when not in object context inside PHP Class [closed]

enter image description here

So I have the below class that I’m working on and I’m trying to reach the object getters within my cURL method but I’m getting the following error: Fatal error: Uncaught Error: Using $this when not in object context

The problem that it’s pointing too is the following:

Could someone tell me what I might be missing? Thanks a bunch!

<?php

namespace SCPT;

class Instagram
{
    private $title;
    private $post_count;
    private $id;
    private $access_token;
    private $transient;

    public function __construct()
    {
//        $this->set_title(get_field('title'));
//        $this->set_post_count(get_field('post_count'));
//        $this->set_id(get_field('instagram_id'));
//        $this->set_access_token(get_field('access_token'));
//        $this->set_transient(get_field('media_refresh'));
    }

    public static function init()
    {
        // @todo: Do something here with the Instagram stuff
        $instagram = new Instagram();

        return $instagram;
    }

    // @todo: Build the cache button into the options page
    public static function transient_display() {
        add_action('admin_init', function() {
            ?>
            <label for="accesstoken">
                <a href="?page=instagram_page">Clear cache</a>
            </label>
            <?php
        });
    }

    public static function register() {
        add_action('widgets_init', function() {
            register_widget('Instagram_Widget');
        });

        add_action('wp_enqueue_scripts', function() {
            wp_register_style('instagram-script', content_url() . '/mu-plugins/sacfoodies/_scripts/instagram-auth.js');
            wp_enqueue_style('instagram-script');
        });

        add_action('acf/init', function() {
            if(function_exists('acf_add_options_sub_page')) {
                acf_add_options_sub_page(array(
                    'page_title'  => __('Instagram'),
                    'menu_title'  => __('Instagram'),
                    'parent_slug' => 'options-general.php',
                ));
            }
        });
    }

    public static function cURL()
    {
        $cached_result = get_transient('instagram');
        if (empty($cached_result))
        {
            $curl = curl_init();
            curl_setopt(
                    $curl,
                    CURLOPT_RETURNTRANSFER,
                    true
            );
            curl_setopt(
                    $curl,
                    CURLOPT_URL,
                "https://graph.facebook.com/v5.0/" . $this->get_id() . "/?fields=id&access_token=" . $this->get_access_token(),
            );
            $array = json_decode(curl_exec($curl));
            set_transient('instagram', $array, $this->get_transient() * HOUR_IN_SECONDS);

            return $array;
        }
        $array = $cached_result;

        return $array;
    }

    public static function getMedia($array)
    {
        $IG_Media = array_map(function($call) {
            return [
                'id' => $call['id'],
            ];
        }, $array['data']);

        foreach ($IG_Media as $image) {
            var_dump($image);
            // @todo: Do something more.
        }
    }

    public function set_title($title)
    {
        $this->title = $title;
    }

    public function get_title()
    {
        return $this->title;
    }

    public function set_post_count($post_count)
    {
        $this->post_count = $post_count;
    }

    public function get_post_count()
    {
        return $this->post_count;
    }

    public function set_id($id)
    {
        $this->id = $id;
    }

    public function get_id()
    {
        return $this->id;
    }

    public function set_access_token($access_token)
    {
        $this->access_token = $access_token;
    }

    public function get_access_token()
    {
        return $this->access_token;
    }

    public function set_transient($transient)
    {
        $this->transient = $transient;
    }

    public function get_transient()
    {
        return $this->transient;
    }
}

Read more here:: Fatal error: Uncaught Error: Using $this when not in object context inside PHP Class [closed]

Leave a Reply

Your email address will not be published. Required fields are marked *