Developed with love by KnpLabs Hire us for your project!
21

WordpressBundle

by Goutte

This bundle is a simple bridge between an existing WordPress and Symfony2, providing repositories for Posts, Pages and Attachments, and that's it !

What is it ?

This is a bridge between WordPress and Symfony2.

This bundle will allow you to :
- find published posts/pages
- all of them
- by slug
- find attachments by mime-type, specifically images

That's all !

Inspired (a lot!) by :
- https://github.com/kayue/WordpressBundle
- and its active fork https://github.com/101medialab/WordpressBundle
- https://github.com/PolishSymfonyCommunity/PSSBlogBundle

I re-did the bundle from scratch because I wanted working and clean tests at all times. Plus, it's a learner's project.

You should not use this bundle, as using both WordPress AND Symfony2 is a bad practice,
but until the CMF is media-ready this is a good enough alternative in some fast-food cases.

How to use

  1. Create your wordpress as usual
  2. Add this bundle to your composer.json
  3. Register this bundle in your AppKernel.php
  4. Configure in app/config.yml

    goutte_wordpress:
        # WP tables prefix, default is 'wp_'
        table_prefix: wp_
    
  5. Configure your parameters.yml to point towards the wordpress database

Usage examples

You'll need the Entity Manager

$em = $this->get('doctrine')->getEntityManager(); // whichever way you're using to get the em

Posts

Will only fetch posts, not pages nor attachments. (see below on how to get those)

$postRepository = $em->getRepository('GoutteWordpressBundle:Post');

$posts = $postRepository->findPublished(); // finds all published posts

// finds a maximum of 5 published posts after omitting the first 3
$posts = $postRepository->findPublished(5,3);

// finds one post by its slug, or returns false
$post = $postRepository->findPublishedBySlug('hello-word');

Pages

$pageRepository = $em->getRepository('GoutteWordpressBundle:Page');

$pages = $pageRepository->findPublished(); // finds all published pages

// finds a maximum of 5 published pages after omitting the first 3
$pages = $pageRepository->findPublished(5,3);

// finds one page by its slug, or returns false
$page = $pageRepository->findPublishedBySlug('hello-word');

Images

<?php
$attachmentRepository = $em->getRepository('GoutteWordpressBundle:Attachment')

// Find all images (attachments whose mime-type starts with 'image/')
$allImages = $attachmentRepository->findImages();

// any mime subtype works as parameter, juste make sure to spell it as wordpress does (eg: jpeg vs jpg)
$pngImages = $attachmentRepository->findImages('png');
$jpgImages = $attachmentRepository->findImages('jpeg');

// you can also pass an array, for convenience
$transparentImages = $attachmentRepository->findImages(array('gif','png', 'webp'));

Attachments

// you may also use directly the query builder, for flexibility
$documentsQb = $attachmentRepository->cqbForTypeAndSubtypes('application', array('pdf','msword'));
$documentsQb = $documentsQb->orderBy('a.comment_count', 'DESC'); // see BasePost Entity for field name
$documents   = $documentsQb->getQuery->getResult();

How to setup tests

  1. Copy phpunit.xml.dist to phpunit.xml
  2. Configure KERNEL_DIR
  3. Register this bundle in your AppKernel.php
  4. Update your composer.json, as we are using Doctrine Mocks. This is not optimized, how can I restrict this to the test env ?

    "autoload": {
     "psr-0": {
       "Doctrine\\Tests\\DBAL": "vendor/doctrine/dbal/tests/"
     }
    }
    
  5. /!\ Make sure you have another database setup for your tests, because the suite will ruin the database !

  6. Run !

Requirements

  • WordPress >=3.4.2 and <=3.5

Caveats

  • WordPress assumes it will be run in the global scope, so some of its code doesn't even bother explicitly globalising variables. The required version of WordPress core marginally improves this situation (enough to allow us to integrate with it), but beware that other parts of WordPress or plugins may still have related issues.
goutte_wordpress:
table_prefix: wp_
  • Merge pull request #3 from greg0ire/patch-1
    By Goutte, 3 years ago
  • detect single_inheritance
    By greg0ire, 3 years ago
  • Merge remote-tracking branch 'origin/master'
    By Goutte, 4 years ago
  • Fixing doctrine mapping inconsistencies
    By Goutte, 4 years ago
  • Merge branch 'master' of github.com:Goutte/WordpressBundle
    By Goutte, 4 years ago
  • Trying to fix doctrine mapping inconsistencies
    By Goutte, 4 years ago
  • Comments while strolling through code
    By Goutte, 4 years ago
  • Strip a line of one character to avoid horizontal scrolling
    By Goutte, 4 years ago
  • More doc in README
    By Goutte, 4 years ago
  • Renamed README, some consistency cleanup
    By Goutte, 4 years ago
  • Upgraded the query builders scope to public
    By Goutte, 4 years ago
  • Some more comments
    By Goutte, 4 years ago
  • Tests Refactoring, Testing BasePost->getMetaValue()
    By Goutte, 4 years ago
  • findImages is now fully tested
    By Goutte, 4 years ago
  • Cleaner README
    By Goutte, 4 years ago
  • Trimming README down
    By Goutte, 4 years ago
  • Adding findImages($subtype) to Attachment Repository
    By Goutte, 4 years ago
  • Adding obscurely needed use (doctrine's voodoo)
    By Goutte, 4 years ago
  • Doctrine SINGLE_TABLE inheritance for Post implementation
    By Goutte, 4 years ago
  • Make sure the WP prefix is added only if not already there
    By Goutte, 4 years ago
  • Tests tests tests
    By Goutte, 4 years ago
  • Database preparation in RepositoryTestCase
    By Goutte, 4 years ago
  • Updating boostrap with some "remember" comments
    By Goutte, 4 years ago
  • Adding minimalistic test fixtures
    By Goutte, 4 years ago
  • Renamed config file (eventually will be reversed => config.yml.dist)
    By Goutte, 4 years ago
  • Adding wordpress 3.4.2 sql export file
    By Goutte, 4 years ago
  • Cleanup TablePrefixSubscriber
    By Goutte, 4 years ago
  • Refactoring Tests with TestCases
    By Goutte, 4 years ago
  • Preparing Repository tests
    By Goutte, 4 years ago
  • Hotfix for bug introduced in previous commit
    By Goutte, 4 years ago