From 75644fe2e165a9328d0c6a52b864651597476e07 Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Wed, 8 Oct 2014 11:00:03 +0200 Subject: [PATCH 1/2] Added new twig helpers Changes to improve SLI rank Added doc for password protected posts --- .gitignore | 3 +- DependencyInjection/Configuration.php | 1 + .../EkinoWordpressExtension.php | 2 + Manager/PostManager.php | 33 +++++++++- Model/Post.php | 7 +++ README.md | 4 ++ Resources/config/twig.xml | 8 +++ Twig/Extension/CommentExtension.php | 61 +++++++++++++++++++ Twig/Extension/PostExtension.php | 53 +++++++++++++++- Wordpress/Wordpress.php | 2 - composer.json | 19 +++++- 11 files changed, 185 insertions(+), 8 deletions(-) create mode 100644 Twig/Extension/CommentExtension.php diff --git a/.gitignore b/.gitignore index fdb5924..7579f74 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ vendor -.idea -composer.lock \ No newline at end of file +composer.lock diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 4a0b8bf..df408fd 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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() ; diff --git a/DependencyInjection/EkinoWordpressExtension.php b/DependencyInjection/EkinoWordpressExtension.php index d216ec7..b86277b 100644 --- a/DependencyInjection/EkinoWordpressExtension.php +++ b/DependencyInjection/EkinoWordpressExtension.php @@ -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); } diff --git a/Manager/PostManager.php b/Manager/PostManager.php index fbc2d99..220d6e1 100644 --- a/Manager/PostManager.php +++ b/Manager/PostManager.php @@ -11,6 +11,7 @@ namespace Ekino\WordpressBundle\Manager; use Ekino\WordpressBundle\Model\Post; +use Symfony\Component\HttpFoundation\Request; /** * Class PostManager @@ -40,4 +41,34 @@ public function hasComments(Post $post) { return 0 < $post->getCommentCount(); } -} \ No newline at end of file + + /** + * @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); + } +} diff --git a/Model/Post.php b/Model/Post.php index a6a5e2b..d86ef96 100644 --- a/Model/Post.php +++ b/Model/Post.php @@ -839,4 +839,11 @@ public function getCategory() return $taxonomy ? $taxonomy->getTerm() : null; } + /** + * @return bool + */ + public function isCommentingOpened() + { + return static::COMMENT_STATUS_OPEN == $this->getCommentStatus(); + } } diff --git a/README.md b/README.md index 3e29239..fd6d090 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/Resources/config/twig.xml b/Resources/config/twig.xml index 06af356..04c9865 100644 --- a/Resources/config/twig.xml +++ b/Resources/config/twig.xml @@ -5,12 +5,19 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + Ekino\WordpressBundle\Twig\Extension\CommentExtension Ekino\WordpressBundle\Twig\Extension\OptionExtension Ekino\WordpressBundle\Twig\Extension\PostExtension Ekino\WordpressBundle\Twig\Extension\PostMetaExtension + + + + + + @@ -22,6 +29,7 @@ + %ekino.wordpress.cookie_hash% diff --git a/Twig/Extension/CommentExtension.php b/Twig/Extension/CommentExtension.php new file mode 100644 index 0000000..7f49ddd --- /dev/null +++ b/Twig/Extension/CommentExtension.php @@ -0,0 +1,61 @@ +commentManager = $commentManager; + } + + /** + * Returns the name of the extension. + * + * @return string The extension name + */ + public function getName() + { + return 'ekino_wordpress_comment'; + } + + 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('%s', $authorUrl, $comment->getAuthor()); + } + + if ((!$userUrl = $user->getUrl()) || !preg_match('/^http(s)?:\/\/.+$/', $userUrl)) { + return $user->getDisplayName(); + } + + return sprintf('%s', $userUrl, $user->getDisplayName()); + } +} diff --git a/Twig/Extension/PostExtension.php b/Twig/Extension/PostExtension.php index d281049..29930cd 100644 --- a/Twig/Extension/PostExtension.php +++ b/Twig/Extension/PostExtension.php @@ -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; } /** @@ -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)), ); } @@ -83,4 +93,43 @@ public function replacePostArguments($permalinkStructure, Post $post) return $permalinkStructure; } -} \ No newline at end of file + + /** + * @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(); + } +} diff --git a/Wordpress/Wordpress.php b/Wordpress/Wordpress.php index 705d098..9dfd5be 100644 --- a/Wordpress/Wordpress.php +++ b/Wordpress/Wordpress.php @@ -12,8 +12,6 @@ use Symfony\Component\HttpKernel\KernelInterface; -use Ekino\WordpressBundle\Wordpress\WordpressResponse; - /** * Class Wordpress * diff --git a/composer.json b/composer.json index 2f702a5..9b3bf7f 100644 --- a/composer.json +++ b/composer.json @@ -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": "" From 0369e3d8a571eff9e6d90941d88e8d2059fbd812 Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Fri, 10 Oct 2014 00:53:43 +0200 Subject: [PATCH 2/2] Added missing PHPDoc --- Twig/Extension/CommentExtension.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Twig/Extension/CommentExtension.php b/Twig/Extension/CommentExtension.php index 7f49ddd..9898de6 100644 --- a/Twig/Extension/CommentExtension.php +++ b/Twig/Extension/CommentExtension.php @@ -5,6 +5,11 @@ use Ekino\WordpressBundle\Manager\CommentManager; use Ekino\WordpressBundle\Model\Comment; +/** + * Provide native wordpress functions into twig + * + * @author Xavier Coureau + */ class CommentExtension extends \Twig_Extension { /** @@ -30,6 +35,9 @@ public function getName() return 'ekino_wordpress_comment'; } + /** + * {@inheritdoc} + */ public function getFunctions() { return array(