├── README.md ├── composer.json ├── core.php ├── ekino-wordpress-symfony.php ├── hook.php └── readme.txt /README.md: -------------------------------------------------------------------------------- 1 | # ekino-wordpress-symfony 2 | 3 | This is the Ekino Wordpress plugin to interact with Symfony. 4 | 5 | ## Installation 6 | 7 | 8 | 9 | 1) Put the following lines at the beginning of your `wp-config.php` file, according to your project configuration: 10 | 11 | ```php 12 | define('WP_SYMFONY_PATH', __DIR__.'/symfony/'); 13 | define('WP_SYMFONY_ENVIRONMENT', 'dev'); 14 | define('WP_SYMFONY_DEBUG', true); 15 | ``` 16 | 17 | 2) Enable the "Ekino Wordpress Symfony" plugin in WordPress Extensions list. 18 | 19 | ## Usage 20 | 21 | ### Retrieve a Symfony service 22 | 23 | You can retrieve all Symfony services registered in the dependency injection container with the following method: 24 | 25 | ```php 26 | $service = \symfony_service('my.symfony.service.identifier'); 27 | ``` 28 | 29 | ### Dispatch Wordpress hooks in Symfony using EventDispatcher 30 | 31 | This plugin allows Wordpress to dispatch some hooks on the Symfony EventDispatcher component. 32 | 33 | An example with the three following hooks has been added: 34 | 35 | * wp_login: authenticate the user in Symfony application on Wordpress login 36 | * wp_logout: logout user from Symfony when user logout of Wordpress 37 | * auth_cookie_valid: authenticate the user when the auth cookie is valid 38 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ekino-symfony", 3 | "description": "This is the Ekino Wordpress plugin to interact with Symfony (using EkinoWordpressBundle).", 4 | "repositories":[ 5 | { 6 | "type":"composer", 7 | "url":"https://wpackagist.org" 8 | } 9 | ], 10 | } 11 | -------------------------------------------------------------------------------- /core.php: -------------------------------------------------------------------------------- 1 | loadClassCache(); 24 | $kernel->boot(); 25 | 26 | $sfContainer = $kernel->getContainer(); 27 | $sfContainer->enterScope('request'); 28 | $sfContainer->set('request', new \Symfony\Component\HttpFoundation\Request(), 'request'); 29 | 30 | symfony_get_container($sfContainer); 31 | } 32 | } 33 | 34 | /** 35 | * Returns a Symfony service from its name 36 | * 37 | * @param string $name 38 | * 39 | * @return object 40 | */ 41 | function symfony_service($name) 42 | { 43 | return symfony_get_container()->get($name); 44 | } 45 | 46 | /** 47 | * Returns Symfony container from Symfony EkinoWordpressBundle if installed else from static loaded here 48 | * 49 | * @param ContainerInterface|null $sfContainer 50 | * 51 | * @return ContainerInterface 52 | */ 53 | function symfony_get_container($sfContainer = null) 54 | { 55 | static $container; 56 | 57 | if (function_exists('symfony_container')) { 58 | $container = symfony_container(); 59 | } 60 | 61 | return $container = $sfContainer ?: $container; 62 | } 63 | 64 | /** 65 | * Dispatch an event using Symfony EventDispatcher service 66 | * 67 | * @param string $name Name of the event to dispatch 68 | * @param \Ekino\WordpressBundle\Event\WordpressEvent $event A Wordpress event 69 | * 70 | * @return mixed 71 | */ 72 | function symfony_event_dispatch($name, \Ekino\WordpressBundle\Event\WordpressEvent $event) 73 | { 74 | return symfony_service('event_dispatcher')->dispatch($name, $event); 75 | } -------------------------------------------------------------------------------- /ekino-wordpress-symfony.php: -------------------------------------------------------------------------------- 1 | $user_login, 22 | 'user' => $user 23 | )); 24 | 25 | symfony_event_dispatch('ekino.wordpress.user_login', $event); 26 | } 27 | 28 | /** 29 | * Dispatch Wordpress user auth cookie validation on Symfony event dispatcher 30 | * only if trying to access the administration page 31 | * 32 | * @param array $cookie_elements Wordpress cookie data 33 | * @param \WP_User $user Wordpress user object 34 | */ 35 | function ekino_wordpress_symfony_hook_wp_login_cookie($cookie_elements, $user) { 36 | if (!is_admin()) { 37 | return; 38 | } 39 | 40 | $event = new \Ekino\WordpressBundle\Event\WordpressEvent(array( 41 | 'cookie_elements' => $cookie_elements, 42 | 'user' => $user 43 | )); 44 | 45 | symfony_event_dispatch('ekino.wordpress.user_login', $event); 46 | } 47 | 48 | /** 49 | * Dispatch Wordpress user log out hook on Symfony event dispatcher 50 | * 51 | * @see http://codex.wordpress.org/Plugin_API/Action_Reference/wp_logout 52 | */ 53 | function ekino_wordpress_symfony_hook_wp_logout() { 54 | $event = new \Ekino\WordpressBundle\Event\WordpressEvent(); 55 | 56 | symfony_event_dispatch('ekino.wordpress.user_logout', $event); 57 | } -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Ekino Symfony Wordpress === 2 | Contributors: Thomas Vial , Vincent Composieux 3 | Donate link: http://www.github.com/ekino/ekino-wordpress-symfony 4 | Tags: symfony, symfony2, wordpress, hook, event 5 | Requires at least: 3.0.1 6 | Tested up to: 3.7 7 | Stable tag: 1.0 8 | License: MIT 9 | License URI: http://opensource.org/licenses/MIT 10 | 11 | Allows to use Wordpress with Symfony [Symfony](http://www.symfony.com) framework (call symfony services, run symfony events on hooks, ...). 12 | Contribute on http://www.github.com/ekino/ekino-wordpress-symfony 13 | 14 | == Description == 15 | 16 | * When authenticated on Wordpress, authenticated on Symfony too with correct user roles. (requires EkinoWordpressBundle), 17 | * Catch some Wordpress hooks to be dispatched by Symfony EventDispatcher (requires EkinoWordpressBundle). 18 | 19 | Symfony EkinoWordpressBundle is available here: http://www.github.com/ekino/EkinoWordpressBundle 20 | 21 | == Installation == 22 | 23 | * Enable the "Ekino Wordpress Symfony" plugin, 24 | * Put the following lines in your wp-config.php file, according to your project configuration: 25 | 26 | ``` 27 | define('WP_SYMFONY_PATH', __DIR__.'/symfony/'); 28 | define('WP_SYMFONY_ENVIRONMENT', 'dev'); 29 | define('WP_SYMFONY_DEBUG', true); 30 | ``` 31 | 32 | * Install Symfony EkinoWordpressBundle, which is available here: http://www.github.com/ekino/EkinoWordpressBundle 33 | 34 | == Usage == 35 | 36 | Mix Wordpress and Symfony together to allow doing multiple things like: 37 | 38 | * Use custom Symfony services into Wordpress, 39 | * Use Symfony to manipulate Wordpress database, 40 | * Create custom Symfony routes out of Wordpress, 41 | * When authenticated on Wordpress, authenticated on Symfony too with correct user roles, 42 | * Catch some Wordpress hooks to be dispatched by Symfony EventDispatcher. 43 | 44 | == Changelog == 45 | 46 | = 1.0 = 47 | * Add initial version. 48 | --------------------------------------------------------------------------------