Skip to content

Commit

Permalink
Merge pull request #48 from Takeatea/helpers
Browse files Browse the repository at this point in the history
Added new twig helpers
  • Loading branch information
eko committed Oct 11, 2014
2 parents 5f72007 + 0369e3d commit 089a709
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 8 deletions.
3 changes: 1 addition & 2 deletions .gitignore
@@ -1,3 +1,2 @@
vendor
.idea
composer.lock
composer.lock
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -38,6 +38,7 @@ public function getConfigTreeBuilder()
->scalarNode('wordpress_directory')->end()
->scalarNode('entity_manager')->end()
->booleanNode('load_twig_extension')->defaultFalse()->end()
->booleanNode('cookie_hash')->defaultNull()->end()
->scalarNode('i18n_cookie_name')->defaultFalse()->end()
->end()
;
Expand Down
2 changes: 2 additions & 0 deletions DependencyInjection/EkinoWordpressExtension.php
Expand Up @@ -62,6 +62,8 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('i18n.xml');
}

$container->setParameter('ekino.wordpress.cookie_hash', $config['cookie_hash']);

$container->setParameter($this->getAlias() . '.backend_type_orm', true);
}

Expand Down
33 changes: 32 additions & 1 deletion Manager/PostManager.php
Expand Up @@ -11,6 +11,7 @@
namespace Ekino\WordpressBundle\Manager;

use Ekino\WordpressBundle\Model\Post;
use Symfony\Component\HttpFoundation\Request;

/**
* Class PostManager
Expand Down Expand Up @@ -40,4 +41,34 @@ public function hasComments(Post $post)
{
return 0 < $post->getCommentCount();
}
}

/**
* @param Post $post
* @param Request $request
* @param string $cookieHash
*
* @return bool
*/
public function isPasswordRequired(Post $post, Request $request, $cookieHash)
{
if (!$post->getPassword()) {
return false;
}

$cookies = $request->cookies;

if (!$cookies->has('wp-postpass_' . $cookieHash)) {
return true;
}

$hash = stripslashes($cookies->get('wp-postpass_' . $cookieHash));

if (0 !== strpos($hash, '$P$B')) {
return true;
}

$wpHasher = new \PasswordHash(8, true);

return !$wpHasher->CheckPassword($post->getPassword(), $hash);
}
}
7 changes: 7 additions & 0 deletions Model/Post.php
Expand Up @@ -839,4 +839,11 @@ public function getCategory()
return $taxonomy ? $taxonomy->getTerm() : null;
}

/**
* @return bool
*/
public function isCommentingOpened()
{
return static::COMMENT_STATUS_OPEN == $this->getCommentStatus();
}
}
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -288,3 +288,7 @@ class LanguageController extends Controller
}
}
```

## Handle with password protected post
If you use password protected posts and you have defined your own `COOKIEHASH` constant, you can provide it using the `cookie_hash` parameter in your `config.yml` file.
You will then be able to use the `wp_post_password_required` twig function that behave exactly like `post_password_required` Wordpress function.
8 changes: 8 additions & 0 deletions Resources/config/twig.xml
Expand Up @@ -5,12 +5,19 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="ekino.wordpress.twig.extension.comment.class">Ekino\WordpressBundle\Twig\Extension\CommentExtension</parameter>
<parameter key="ekino.wordpress.twig.extension.option.class">Ekino\WordpressBundle\Twig\Extension\OptionExtension</parameter>
<parameter key="ekino.wordpress.twig.extension.post.class">Ekino\WordpressBundle\Twig\Extension\PostExtension</parameter>
<parameter key="ekino.wordpress.twig.extension.post_meta.class">Ekino\WordpressBundle\Twig\Extension\PostMetaExtension</parameter>
</parameters>

<services>
<service id="ekino.wordpress.twig.extension.comment" class="%ekino.wordpress.twig.extension.comment.class%">
<tag name="twig.extension" />

<argument type="service" id="ekino.wordpress.manager.comment" />
</service>

<service id="ekino.wordpress.twig.extension.option" class="%ekino.wordpress.twig.extension.option.class%">
<tag name="twig.extension" />

Expand All @@ -22,6 +29,7 @@

<argument type="service" id="ekino.wordpress.manager.post" />
<argument type="service" id="ekino.wordpress.twig.extension.option" />
<argument>%ekino.wordpress.cookie_hash%</argument>
</service>

<service id="ekino.wordpress.twig.extension.post_meta" class="%ekino.wordpress.twig.extension.post_meta.class%">
Expand Down
69 changes: 69 additions & 0 deletions Twig/Extension/CommentExtension.php
@@ -0,0 +1,69 @@
<?php

namespace Ekino\WordpressBundle\Twig\Extension;

use Ekino\WordpressBundle\Manager\CommentManager;
use Ekino\WordpressBundle\Model\Comment;

/**
* Provide native wordpress functions into twig
*
* @author Xavier Coureau <xav@takeatea.com>
*/
class CommentExtension extends \Twig_Extension
{
/**
* @var CommentManager
*/
protected $commentManager;

/**
* @param CommentManager $commentManager
*/
public function __construct(CommentManager $commentManager)
{
$this->commentManager = $commentManager;
}

/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'ekino_wordpress_comment';
}

/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('wp_get_comment_author_link', array($this, 'getCommentAuthorLink'), array('is_safe' => array('html'))),
);
}

/**
* @param Comment $comment
*
* @return string
*/
public function getCommentAuthorLink(Comment $comment)
{
if (!$user = $comment->getUser()) {
if ((!$authorUrl = $comment->getAuthorUrl()) || !preg_match('/^http(s)?:\/\/.+$/', $authorUrl)) {
return $comment->getAuthor();
}

return sprintf('<a href="%s" rel="nofollow" target="_new">%s</a>', $authorUrl, $comment->getAuthor());
}

if ((!$userUrl = $user->getUrl()) || !preg_match('/^http(s)?:\/\/.+$/', $userUrl)) {
return $user->getDisplayName();
}

return sprintf('<a href="%s" rel="nofollow" target="_new">%s</a>', $userUrl, $user->getDisplayName());
}
}
53 changes: 51 additions & 2 deletions Twig/Extension/PostExtension.php
Expand Up @@ -17,14 +17,21 @@ class PostExtension extends \Twig_Extension
*/
protected $optionExtension;

/**
* @var string|null
*/
protected $cookieHash;

/**
* @param PostManager $postManager
* @param OptionExtension $optionExtension
* @param string|null $cookieHash
*/
public function __construct(PostManager $postManager, OptionExtension $optionExtension)
public function __construct(PostManager $postManager, OptionExtension $optionExtension, $cookieHash = null)
{
$this->postManager = $postManager;
$this->optionExtension = $optionExtension;
$this->cookieHash = $cookieHash;
}

/**
Expand All @@ -41,7 +48,10 @@ public function getName()
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('wp_comments_open', array($this, 'isCommentingOpened')),
new \Twig_SimpleFunction('wp_get_permalink', array($this, 'getPermalink')),
new \Twig_SimpleFunction('wp_have_comments', array($this, 'haveComments')),
new \Twig_SimpleFunction('wp_post_password_required', array($this, 'isPostPasswordRequired'), array('needs_context' => true)),
);
}

Expand Down Expand Up @@ -83,4 +93,43 @@ public function replacePostArguments($permalinkStructure, Post $post)

return $permalinkStructure;
}
}

/**
* @param array $context
* @param Post $post
*
* @return bool
*/
public function isPostPasswordRequired(array $context, Post $post)
{
if (null === $this->cookieHash) {
$this->cookieHash = '';

if ($siteUrlOption = $this->optionExtension->getOption('siteurl')) {
$this->cookieHash = md5($siteUrlOption->getValue());
}
}

return $this->postManager->isPasswordRequired($post, $context['app']->getRequest(), $this->cookieHash);
}

/**
* @param Post $post
*
* @return bool
*/
public function isCommentingOpened(Post $post)
{
return $post->isCommentingOpened();
}

/**
* @param Post $post
*
* @return bool
*/
public function haveComments(Post $post)
{
return 0 < $post->getCommentCount();
}
}
2 changes: 0 additions & 2 deletions Wordpress/Wordpress.php
Expand Up @@ -12,8 +12,6 @@

use Symfony\Component\HttpKernel\KernelInterface;

use Ekino\WordpressBundle\Wordpress\WordpressResponse;

/**
* Class Wordpress
*
Expand Down
19 changes: 18 additions & 1 deletion composer.json
Expand Up @@ -11,13 +11,30 @@
"email": "composieux@ekino.com"
}
],
"repositories": [
{
"type": "package",
"package": {
"name": "openwall/phpass",
"version": "0.3",
"dist": {
"url": "http://www.openwall.com/phpass/phpass-0.3.tar.gz",
"type": "tar"
},
"autoload": {
"classmap": ["PasswordHash.php"]
}
}
}
],
"require": {
"php": ">=5.3.2",
"symfony/framework-bundle": "~2.2",
"symfony/security-bundle": "~2.2",
"symfony/monolog-bundle": "~2.2",
"doctrine/doctrine-bundle": "~1.0",
"doctrine/orm": "~2.2"
"doctrine/orm": "~2.2",
"openwall/phpass": "0.3"
},
"suggest": {
"twig/twig": ""
Expand Down

0 comments on commit 089a709

Please sign in to comment.