1

So I'm putting together a WordPress plugin for a client based on this Github project, which would allow you to pull in posts from WordPress.

Basically I pulled down the project and I had several errors. I've been tackling them one at a time. I had posted a question here before in relation to the first error in the bunch. I took the advice from user 'dipmala' and address that issue but now have this new issue that pops up when I try to run the 'sync' function.

Notice: Undefined variable: options in /opt/bitnami/apps/wordpress/htdocs/wp-content/plugins/WP-Plugin/includes/class-wp-parse-api-admin-settings.php on line 42

It appears that the sync fails. I'm thinking that one of the fixes I implemented is causing one of the variables to not be assigned.

(UPDATE - I looked at the the link provided by 'cale_b', however I do believe the real underlining problem is beyond an undefined variable, as mentioned by 'benito', in his response. I'd like to get the underlining problem fixed, not just side step the error.)

I would appreciate very much if someone could offer me some insight on how to fix this issue so that the plugin successfully syncs to the Parse Server instance.

The Parse Server is being hosted by Back4App.

I've posted the code from 'class-wp-parse-api-admin-settings.php' here on PasteBin and below...

<?php
if (!defined('WP_PARSE_API_PATH')) die('.______.');

if (is_admin()){     // admin actions
    add_action('admin_menu', 'wp_parse_api_menu');
    add_action('admin_post_wp_parse_api_sync', 'wp_parse_api_sync');

    function wp_parse_api_menu() {
        add_options_page('Parse Api Options', 'Parse Api', 'manage_options', 'wp-parse-api-options', 'wp_parse_api_page');
        add_action('admin_init', 'wp_parse_api_admin_init');
    }

    function wp_parse_api_admin_init() {
        //register our settings
        register_setting('wp-parse-api-settings-group', 'app_id');
        register_setting('wp-parse-api-settings-group', 'app_masterkey');
        register_setting('wp-parse-api-settings-group', 'app_restkey');
        register_setting('wp-parse-api-settings-group', 'app_url');
        register_setting('wp-parse-api-settings-group', 'app_push_notifications');
        register_setting('wp-parse-api-settings-group', 'object_name');
        register_setting('wp-parse-api-settings-group', 'lang');
    }

    function wp_parse_api_page() {
        require WP_PARSE_API_PATH .'includes/class-wp-parse-api-admin-settings-template.php';
    }

    function wp_parse_api_sync() {
        $numberposts = 10;

        if (isset($_GET['wp-parse-api-page'])) {
            $_GET['wp-parse-api-page'] = (int)$_GET['wp-parse-api-page'];
            if ($_GET['wp-parse-api-page'] < 1) $_GET['wp-parse-api-page'] = 1;

            $options = array(
            'numberposts' => $numberposts,
            'offset' => ($_GET['wp-parse-api-page'] * $numberposts) - $numberposts,
            );
        }


        $wp_posts = get_posts($options);    
        if (count($wp_posts) == 0) {
            wp_redirect( 'options-general.php?page=wp-parse-api-options' );
            exit;
        }

        foreach ($wp_posts as $wp) {
            if ($wp->post_status != 'publish') continue;

            $post = WpParseApiHelpers::postToObject($wp->ID);
            $q = new parseQuery(WP_PARSE_API_OBJECT_NAME);
            $q->whereEqualTo('wpId', $wp->ID);
            $q->setLimit(1);
            $result = $q->find();

            if ($result->results[0]) {
                $post->update($result->results[0]->objectId);
            } else {
                $post->save();
            }
        }

        ++$_GET['wp-parse-api-page'];
        $url = $_SERVER['PHP_SELF'];

        foreach ($_GET as $k=>$v):
            $qs = (strpos($url, '?') === false ? '?' : '&');
            $url .= sprintf("%s%s=%s", $qs, $k, $v);
        endforeach;

        //wp_redirect( $url );
        echo "Page:". ($_GET['wp-parse-api-page']-1) ."<script> setTimeout(\"document.location='$url'\", 1000); </script>";
        exit;   
    }
}
L. King
  • 39
  • 7
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – random_user_name Jan 04 '18 at 02:11
  • Pro troubleshooting tip: The error message contains super-useful info. It mentions line 42. Which line, in the code you posted, is line 42? That would be good to know... – random_user_name Jan 04 '18 at 02:12
  • Pro troubleshooting tip #2: Proper indenting of the code makes it _much much easier_ to troubleshoot. Take the time to format / indent code, or better yet, use an IDE such as PHPStorm which will do that for you. – random_user_name Jan 04 '18 at 02:13
  • Thanks cale_b for the links! I actually put in Pastebin that has the numbered line as I see them. Hope that helps! – L. King Jan 04 '18 at 02:57

1 Answers1

0

The code inside your block:

if (isset($_GET['wp-parse-api-page'])) {
   // ...
}

is not called. So obviously $options does not exist when you reach get_posts($options). Try putting this just after:

else {
    $options = array();
}

Then you probably also want to investigate why the wp-parse-api-page GET parameter is not defined. As someone replied to your previous question, you should have wp-parse-api-page= preceded by ? or & and followed by a number in your URL. You have to take care of this, especially since later on you do this without checking again if it exists:

++$_GET['wp-parse-api-page'];

Last, I'd suggest you use consistent indentation and formatting in your code. Your block that checks the GET param would be more readable this way:

    if (isset($_GET['wp-parse-api-page'])) {
        $_GET['wp-parse-api-page'] = (int)$_GET['wp-parse-api-page'];
        if ($_GET['wp-parse-api-page'] < 1) {
            $_GET['wp-parse-api-page'] = 1;
        }

        $options = array(
            'numberposts' => $numberposts,
            'offset' => ($_GET['wp-parse-api-page'] * $numberposts) - $numberposts
        );
    }
Benito
  • 710
  • 5
  • 10
  • Thanks Benito! Couple questions... You said... "Try putting this just after: else { $options = array(); }" Where exactly should I put it? After the closing take of "if (isset($_GET['wp-parse-api-page'])) { // ... }"? – L. King Jan 04 '18 at 01:37
  • Yes, exactly there. Oh wait, I have a better idea: you could just put `$options = array();` (without else) just *before* this block. – Benito Jan 04 '18 at 02:00
  • Awesome! That worked! Now could you please elaborate a little more about the wp-parse-api-page? Would this be the WordPress page that's generated by the plugin? What would I put in there I guess? The link to the WordPress site or the Parse Server? Thanks again – L. King Jan 04 '18 at 02:28
  • Cool to know that it worked! But as for your secound question, that seems to be quite specific to how this plugin works. It might take a while to dive in the docs, and I'm short on time! Also, why do you have to modify the plugin? Is it because it seems quite old? – Benito Jan 04 '18 at 23:08
  • Hey Benito! I really appreciate any help you can provide. I can upload the code to PasteBin so you can see it but basically the plugin was setup to go to Parse.com which got shutdown by Facebook. So I have to change all the endpoints to go to the Parse-Server instance which is the open source version that Facebook released. – L. King Jan 05 '18 at 00:18