function wp_parse_api_sync() { $numberposts = 10; $_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); } //wp_redirect( $url ); echo "Page:" . ($_GET['wp-parse-api-page'] - 1) . "<script> setTimeout(\"document.location='{$url}'\", 1000); </script>"; exit; }
/** * Create/Update the post on parse.com * */ public function save_post($post_id) { WpParseApiHelpers::log("WpParseApi::save_post({$post_id}) | START"); // Verify post is a revision if (wp_is_post_revision($post_id)) { return; } // Check if the parse api app id is defined if (!defined('WP_PARSE_API_APP_ID') || WP_PARSE_API_APP_ID == null) { return; } WpParseApiHelpers::log("WpParseApi::save_post({$post_id}) | WP_PARSE_API_APP_ID passed"); // Verify post is an autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } WpParseApiHelpers::log("WpParseApi::save_post({$post_id}) | DOING_AUTOSAVE passed"); // Verify post nonce // if (!wp_verify_nonce( $_POST[ $this->option_name . '_nonce' ], $this->action)) return; // WpParseApiHelpers::log("WpParseApi::save_post($post_id) | nonce passed"); // Verify post status if (get_post_status($post_id) != 'publish') { return; } WpParseApiHelpers::log("WpParseApi::save_post({$post_id}) | status passed"); $post = WpParseApiHelpers::postToObject($post_id); // Creates a new post on parse.com if (!get_post_meta($post_id, 'wp_parse_api_code_run', true)) { update_post_meta($post_id, 'wp_parse_api_code_run', true); $categories = array(); foreach ($post->data['categories'] as $row) { $row = trim(preg_replace('/[^a-zA-Z]/', '', $row)); if ($row != '') { $categories[] = $row; } } // Check if there is no categories or push notifications are disabled if (is_array($categories) && count($categories) > 0 && get_option('app_push_notifications') != 'Off') { try { $push = new parsePush(); $push->alert = $post->data['title']; $push->channels = $categories; $push->badge = "increment"; $push->sound = "example.caf"; $push->post_id = $post->data[wpId]; $push->url = $post->data['guid']; $push->category = "ACTIONABLE"; $push->send(); } catch (Exception $e) { // do nothing, this was added because // parse lib throws an exception if the account // has not been configured // special thanks to raymondmuller for find the issue } } $post->save(); // Update an existin post on parse.com } else { $q = new parseQuery(WP_PARSE_API_OBJECT_NAME); $q->where('wpId', (int) $post_id); $r = $q->find(); if (is_array($r->results)) { $r = array_shift($r->results); } if ($r != null) { $post->update($r->objectId); } } WpParseApiHelpers::log("WpParseApi::save_post({$post_id}) | END"); }