function recent_posts($pageSize) { global $WPGLOBAL, $loop; $prismic = $WPGLOBAL['prismic']; $pageSize = $pageSize ? $pageSize->getValue() : 5; $posts = $prismic->form()->query(Predicates::at('document.type', 'post'))->pageSize($pageSize)->fetchLinks('post.date', 'category.name', 'author.full_name', 'author.first_name', 'author.surname', 'author.company')->orderings('my.post.date desc')->submit(); $loop->setResponse($posts); }
/** * Return the URL to display a given preview * @param string $token as received from Prismic server to identify the content to preview * @param \Prismic\LinkResolver $linkResolver the link resolver to build URL for your site * @param string $defaultUrl the URL to default to return if the preview doesn't correspond to a document * (usually the home page of your site) * @return string the URL you should redirect the user to preview the requested change */ public function previewSession($token, $linkResolver, $defaultUrl) { $response = $this->getHttpAdapter()->get($token); $response = json_decode($response->getBody(true)); if (isset($response->mainDocument)) { $documents = $this->forms()->everything->query(Predicates::at("document.id", $response->mainDocument))->ref($token)->submit()->getResults(); if (count($documents) > 0) { return $linkResolver->resolveDocument($documents[0]); } } return $defaultUrl; }
$homeId = $prismic->get_api()->bookmark('home'); $blogHomeId = $prismic->get_api()->bookmark('bloghome'); if ($blogHomeId == null) { not_found($app); } else { if ($blogHomeId == $homeId) { redirect('/'); return; } } $blogHome = $prismic->get_page($blogHomeId); if ($blogHome == null) { not_found($app); return; } $posts = $prismic->form()->query(Predicates::at('document.type', 'post'))->fetchLinks('post.date', 'category.name', 'author.full_name', 'author.first_name', 'author.surname', 'author.company')->page(current_page($app))->orderings('[my.post.date desc]')->submit(); $skin = $prismic->get_skin(); render($app, 'bloghome', array('bloghome' => $blogHome, 'posts' => $posts, 'skin' => $skin)); }); // Archive $app->get('/archive/:year(/:month(/:day))', function ($year, $month = null, $day = null) use($app, $prismic) { global $WPGLOBAL; $posts = $prismic->archives(array('year' => $year, 'month' => $month, 'day' => $day), current_page($app)); $date = array('year' => $year, 'month' => $month, 'day' => $day); $skin = $prismic->get_skin(); render($app, 'archive', array('posts' => $posts, 'date' => $date, 'skin' => $skin)); }); // Previews $app->get('/preview', function () use($app, $prismic) { $token = $app->request()->params('token'); $url = $prismic->get_api()->previewSession($token, $prismic->linkResolver, '/');
public function testDateTimestamp() { $api = Api::get("https://lesbonneschoses.cdn.prismic.io/api"); $results = $api->forms()->everything->query(Predicates::at("document.id", "UlfoxUnM0wkXYXbl"))->ref($api->master())->submit()->getResults(); $doc = $results[0]; // startgist:c6a5d3d570a585ea6941:prismic-dateTimestamp.php // Date and Timestamp predicates $dateBefore = Predicates::dateBefore("my.product.releaseDate", new DateTime('2014-6-1')); $dateAfter = Predicates::dateAfter("my.product.releaseDate", new DateTime('2014-1-1')); $dateBetween = Predicates::dateBetween("my.product.releaseDate", new DateTime('2014-1-1'), new DateTime('2014-6-1')); $dayOfMonth = Predicates::dayOfMonth("my.product.releaseDate", 14); $dayOfMonthAfter = Predicates::dayOfMonthAfter("my.product.releaseDate", 14); $dayOfMonthBefore = Predicates::dayOfMonthBefore("my.product.releaseDate", 14); $dayOfWeek = Predicates::dayOfWeek("my.product.releaseDate", "Tuesday"); $dayOfWeekAfter = Predicates::dayOfWeekAfter("my.product.releaseDate", "Wednesday"); $dayOfWeekBefore = Predicates::dayOfWeekBefore("my.product.releaseDate", "Wednesday"); $month = Predicates::month("my.product.releaseDate", "June"); $monthBefore = Predicates::monthBefore("my.product.releaseDate", "June"); $monthAfter = Predicates::monthAfter("my.product.releaseDate", "June"); $year = Predicates::year("my.product.releaseDate", 2014); $hour = Predicates::hour("my.product.releaseDate", 12); $hourBefore = Predicates::hourBefore("my.product.releaseDate", 12); $hourAfter = Predicates::hourAfter("my.product.releaseDate", 12); // Accessing Date and Timestamp fields $date = $doc->getDate("blog-post.date"); $dateYear = $date->asDateTime()->format('Y'); $updateTime = $doc->getTimestamp("blog-post.update"); if ($updateTime) { $updateHour = $updateTime->asDateTime()->format('H'); } // endgist $this->assertEquals($dateYear, '2013'); }
public function testImage() { $api = Api::get("https://lesbonneschoses.cdn.prismic.io/api"); $documents = $api->forms()->everything->query(Predicates::at("document.id", "UlfoxUnM0wkXYXbO"))->ref($api->master())->submit()->getResults(); $doc = $documents[0]; // startgist:c202e1c27ddedf88aa7f:prismic-images.php // Accessing image fields $image = $doc->getImage("product.image"); // Most of the time you will be using the "main" view $url = $image->getView("main")->getUrl(); // endgist $this->assertEquals("https://d2aw36oac6sa9o.cloudfront.net/lesbonneschoses/f606ad513fcc2a73b909817119b84d6fd0d61a6d.png", $url); }
public function testFetchLinks() { $api = Api::get(self::$testRepository); $masterRef = $api->master()->getRef(); $documents = $api->forms()->everything->ref($masterRef)->fetchLinks('blog-post.author')->query(Predicates::at('document.id', 'UlfoxUnM0wkXYXbt'))->submit()->getResults(); $link = $documents[0]->getLink('blog-post.relatedpost[0]'); $this->assertEquals('John M. Martelle, Fine Pastry Magazine', $link->getText('blog-post.author')); }
public function testAtPredicate() { $predicate = Predicates::at("document.type", "blog-post"); $this->assertEquals('[:d = at(document.type, "blog-post")]', $predicate->q()); }
public function get_calendar() { $calendar = array(); $page = 1; do { $posts = $this->form()->page($page)->query(Predicates::at('document.type', 'post'))->fetch('post.date')->orderings('my.post.date desc')->submit(); foreach ($posts->getResults() as $post) { if (!$post->getDate('post.date')) { continue; } $date = $post->getDate('post.date')->asDateTime(); $key = $date->format('F Y'); $last = end($calendar); if ($key != $last['label']) { array_push($calendar, array('label' => $key, 'link' => $this->archive_link($date->format('Y'), $date->format('m')))); } $page++; } } while ($posts->getNextPage()); return $calendar; }
public function getDocumentByUidAndMask($uid, $mask) { if (!isset($this->byUidAndMask[$mask])) { $this->byUidAndMask[$mask] = array(); } if (isset($this->byUidAndMask[$mask][$uid])) { return $this->byUidAndMask[$mask][$uid]; } $query = [Predicates::at("document.type", $mask), Predicates::at("my.{$mask}.uid", $uid)]; $api = $this->getPrismicApi(); $documents = $api->forms()->everything->query($query)->ref($this->getRefAsString())->submit(); if (count($documents->getResults())) { // There should be only one! $this->byUidAndMask[$mask][$uid] = current($documents->getResults()); return $this->byUidAndMask[$mask][$uid]; } return null; }
/** * Search a document by its uid * * @param string $type the custom type of the requested document * @param string $id the requested uid * * @return \Prismic\Document the resulting document (null if no match) */ public function getByUID($type, $uid) { return $this->queryFirst(Predicates::at("my." . $type . ".uid", $uid)); }