function addUserFolders($userfoldernames) { $rootfolder = $this->options['root']; $this->client->setUseBatch(true); $batch = new Google_Http_Batch($this->client); /* Find folders */ foreach ($userfoldernames as $key => $userfoldername) { if ($rootfolder === $this->options['base']) { $params = array('q' => "'root' in parents and title='" . $userfoldername . "' and mimeType='application/vnd.google-apps.folder' and trashed = false", "userIp" => $this->userip); } else { $params = array('q' => "'" . $rootfolder . "' in parents and title='" . $userfoldername . "' and mimeType='application/vnd.google-apps.folder' and trashed = false", "userIp" => $this->userip); } $batch->add($this->googleDriveService->files->listFiles($params), 'find-' . $key); } try { usleep(250000); // Don't fire multiple queries fast $batch_result = $batch->execute(); } catch (Exception $ex) { return false; } /* Create folders */ $batch = new Google_Http_Batch($this->client); $newfolders = false; foreach ($userfoldernames as $key => $userfoldername) { if (isset($batch_result['response-find-' . $key])) { $fileslist = $batch_result['response-find-' . $key]; $files = $fileslist->getItems(); if (count($files) === 0) { $newfolder = new Google_Service_Drive_DriveFile(); $newfolder->setTitle($userfoldername); $newfolder->setMimeType('application/vnd.google-apps.folder'); $newParent = new Google_Service_Drive_ParentReference(); $newParent->setId($rootfolder); $newfolder->setParents(array($newParent)); $newfolders = true; $batch->add($this->googleDriveService->files->insert($newfolder, array("userIp" => $this->userip)), 'create-' . $key); } } else { $newfolder = new Google_Service_Drive_DriveFile(); $newfolder->setTitle($userfoldername); $newfolder->setMimeType('application/vnd.google-apps.folder'); $newParent = new Google_Service_Drive_ParentReference(); $newParent->setId($rootfolder); $newfolder->setParents(array($newParent)); $newfolders = true; $batch->add($this->googleDriveService->files->insert($newfolder, array("userIp" => $this->userip)), 'create-' . $key); } } if ($newfolders) { $parententry = $this->cache->removeFromCache($rootfolder); try { usleep(250000); // Don't fire multiple queries fast $batch_result = $batch->execute(); } catch (Exception $ex) { return false; } } }
$client = new Google_Client(); $client->setApplicationName("Client_Library_Examples"); $apiKey = "<YOUR_API_KEY>"; if ($apiKey == '<YOUR_API_KEY>') { echo missingApiKeyWarning(); } else { $client->setDeveloperKey($apiKey); $service = new Google_Service_Books($client); /************************************************ To actually make the batch call we need to enable batching on the client - this will apply globally until we set it to false. This causes call to the service methods to return the query rather than immediately executing. ************************************************/ $client->setUseBatch(true); /************************************************ We then create a batch, and add each query we want to execute with keys of our choice - these keys will be reflected in the returned array. ************************************************/ $batch = new Google_Http_Batch($client); $optParams = array('filter' => 'free-ebooks'); $req1 = $service->volumes->listVolumes('Henry David Thoreau', $optParams); $batch->add($req1, "thoreau"); $req2 = $service->volumes->listVolumes('George Bernard Shaw', $optParams); $batch->add($req2, "shaw"); /************************************************ Executing the batch will send all requests off at once. ************************************************/
/** * Upload WooCommerce products to the Google Merchant Center * @param WC_Product[] $wc_products * @param Google_Client $client * @param string $merchant_id * @return array Product batch results */ function woogle_upload_wc_products($wc_products, $client, $merchant_id) { // Shopping Content Service require_once plugin_dir_path(woogle_get_plugin_file()) . 'vendor/google-api-php-client/src/Google/Service/ShoppingContent.php'; $service = new Google_Service_ShoppingContent($client); // Batch $client->setUseBatch(true); require_once plugin_dir_path(woogle_get_plugin_file()) . 'vendor/google-api-php-client/src/Google/Http/Batch.php'; $product_batch = new Google_Http_Batch($client); // Loop through products foreach ($wc_products as $post) { // Get WC Product $wc_product = $post instanceof WC_Product ? $post : wc_get_product($post); // Get Google Product if ($wc_product->is_type('variable')) { $variations = $wc_product->get_available_variations(); foreach ($variations as $variation) { $wc_product_variable = wc_get_product($variation['variation_id']); $product = woogle_build_product($wc_product_variable); // Add request to batch $request = $service->products->insert($merchant_id, $product); $product_batch->add($request, $variation['variation_id']); } } else { $product = woogle_build_product($wc_product); // Add request to batch $request = $service->products->insert($merchant_id, $product); $product_batch->add($request, $wc_product->id); } // Product updated $update_count++; } // Execute batch $product_batch_results = $product_batch->execute(); return $product_batch_results; }