Home > Flex, PHP, Zend Framework > AMF Server class for WordPress

AMF Server class for WordPress

January 2nd, 2009 Leave a comment Go to comments

After browsing through WordPress’ code I quickly found that there’s no sane way to create AMF support as a WP plugin. At least not for someone who hasn’t done any old-skool procedural PHP in years. Instead of writing a plugin, I decided to write a standalone server script. It’s still very basic and currently setup to work for me. To get it working for your WP setup you should probably make some minor modifications. Click the read more link to check out the code. I’ve released it under the generous BSD license, so knock yourself out! Use it at your own risk… I’m not going to support it. Any updates will be posted in this post. Also, please note that I haven’t tested it yet. If you access the script directly it should output “Zend Amf Endpoint” just fine, but that’s all I can guarantee at this point ;)

<?php
/**
 * BSD LICENSE
 *
 * Copyright (c) 2009, norm2782
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of norm2782 nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY norm2782 ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL norm2782 BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * Set production mode.
 * If set to false, exceptions will bubble through to the Flex frontend
 *
 * @var bool
 */
$production = false;

/**
 * Determine the absolute path of the AMF server
 *
 * @var string
 */
define('ABSPATH', dirname(__FILE__) . '/');

/**
 * One directory below docroot. Your config file and library dir should be here.
 *
 * @var string
 */
define('SUBPATH', dirname(ABSPATH));

/**
 * You should make sure Zend Framework is in your include path
 */
set_include_path(
    implode(PATH_SEPARATOR, array(
        SUBPATH . '/library',
        get_include_path()
    ))
);

/**
 * Include the WordPress config file
 */
$configFile = SUBPATH . '/wp-config.php';

if (!file_exists($configFile)) {
    throw new Exception('WordPress config file was not found!');
}

require_once $configFile;

/**
 * No need to config more stuff from this point on
 */

/**
 * @see Zend_Amf_Server
 */
require_once 'Zend/Amf/Server.php';

/**
 * @see Zend_Db_Adapter_Pdo_Mysql
 */
require_once 'Zend/Db/Adapter/Pdo/Mysql.php';

/**
 * @see Zend_Paginator
 */
require_once 'Zend/Paginator.php';

/**
 * @see Zend_Paginator_Adapter_DbSelect
 */
require_once 'Zend/Paginator/Adapter/DbSelect.php';

/**
 * Simple class to expose wordpress data through AMF
 *
 * @author norm2782
 */
class Wp_Amf_Gateway
{
    /**
     * Database adapter
     *
     * @var Zend_Db_Adapter_Pdo_Mysql
     */
    private $_db = null;

    /**
     * WordPress table prefix
     *
     * @var string
     */
    private $_prefix = null;

    /**
     * Constructor
     *
     * @param array $dbConfig
     * @param string $prefix
     * @return void
     */
    public function __construct(array $dbConfig, $prefix)
    {
        $this->_db = new Zend_Db_Adapter_Pdo_Mysql($dbConfig);
        $this->_db->query('SET NAMES `utf8`');

        $this->_prefix = $prefix;
    }

    /**
     * Get paginated results for the provided query
     *
     * @param Zend_Db_Select $select
     * @param int $page
     * @param int $itemsPerPage
     * @return array
     */
    private function _getPaginated(Zend_Db_Select $select, $page, $itemsPerPage)
    {
        $paginator = new Zend_Paginator(
            new Zend_Paginator_Adapter_DbSelect($select)
        );

        $paginator->setCurrentPageNumber($page)
                  ->setItemCountPerPage($itemsPerPage);

        return array(
            'info'  => $paginator->getPages(),
            'items' => $paginator->getCurrentItems()
        );
    }

    /**
     * Get the comments for the specified post ID
     *
     * @param int $postId
     * @param int $page
     * @param int $itemsPerPage
     * @return array
     */
    public function getCommentsForPost($postId, $page = 1, $itemsPerPage = 10)
    {
        $select = $this->_db->select()->from($this->_prefix . 'comments')
                                      ->where('comment_post_ID = ?', $postId);

        return $this->_getPaginated($select, $page, $itemsPerPage);
    }

    /**
     * Get the meta data for the specified post ID
     *
     * @param $postId
     * @return unknown_type
     */
    public function getMetaForPost($postId)
    {
        $select = $this->_db->select()->from($this->_prefix . 'postmeta')
                                      ->where('post_id = ?', $postId);

        return $this->_db->fetchAll($select);
    }

    /**
     * Get a post by specifying its ID
     *
     * @param int $postId
     * @return array
     */
    public function getPost($postId)
    {
        $select = $this->_db->select()->from($this->_prefix . 'posts')
                                      ->where('ID = ?', $postId);

        return $this->_db->fetchOne($select);
    }

    /**
     * Get posts per page
     *
     * @param int $page
     * @param int $itemsPerPage
     * @return array
     */
    public function getPosts($page = 1, $itemsPerPage = 10)
    {
        $select = $this->_db->select()->from($this->_prefix . 'posts');

        return $this->_getPaginated($select, $page, $itemsPerPage);
    }
}

/**
 * Pass the values from wp-config.php to the Wp_Amf_Gateway class.
 */
$gateway = new Wp_Amf_Gateway(
    array(
        'host'     => DB_HOST,
        'username' => DB_USER,
        'password' => DB_PASSWORD,
        'dbname'   => DB_NAME
    ),
    $table_prefix
);

$server = new Zend_Amf_Server();
$server->setProduction($production)
       ->setClass($gateway)
       ->handle();
Categories: Flex, PHP, Zend Framework Tags: