Top Banner
© All rights reserved. Zend Technologies, Inc. Come integrare Zend Framework in Wordpress Enrico Zimuel ([email protected]) Senior PHP Engineer, Zend Technologies Zend Framework Core Team Wordpress Conference 16 Giugno 2012, Torino
38

Integrare Zend Framework in Wordpress

May 08, 2015

Download

Technology

Enrico Zimuel

Come integrare Zend Framework 1 e 2 in Wordpress. Slide del talk presentato al Wordpress Conference Italia del 16 Giugno 2012 a Torino.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Come integrare Zend Framework in Wordpress

Enrico Zimuel ([email protected])Senior PHP Engineer, Zend TechnologiesZend Framework Core Team

Wordpress Conference16 Giugno 2012, Torino

Page 2: Integrare Zend Framework in Wordpress

2

Mi presento

● Enrico Zimuel (@ezimuel)

● Software Engineer dal 1996

● Senior PHP Engineer presso Zend Technologies nel team di sviluppo di Zend Framework

● Speaker in conferenze internazionali

● Co-autore del libro “PHP best practices” FAG edizioni, 2012

● Socio del GrUSP e co-fondatore del PUG Torino

● Co-organizzatore PHP.TO.START

Page 3: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Perchè utilizzare un framework di

sviluppo?

Page 4: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Non reinventare la ruota

Page 5: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Utilizzare codice di qualità

Page 6: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Ridurre il time to market

Page 7: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Zend Framework

Page 8: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Zend Framework

● Framework PHP per lo sviluppo di applicazioni web

▶ > 15 milioni di download▶ > 500 sviluppatori▶ > 10 milioni hits su Google

● Open source, licenza basata su new BSD

● http://framework.zend.com

● © Zend Technologies Ltd.

Page 9: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Zend Technologies

● Zend Technologies Ltd, the PHP Company

● Fondata nel 1999 da Andi Gutmans e Zeev Suraski, gli sviluppatori del PHP 3.0

● Quartier generale nella Silicon Valley a Cupertino (USA)

● Uffici in: Francia, Germania, India, Irlanda, Israele, Italia

● © PHP Engine 2 (l'interprete del PHP)

● http://www.zend.com

Page 10: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Alcuni progetti basati su ZF

Page 11: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Caratteristiche di ZF● Architettura aperta

▶ Facile integrazione in progetti di terze parti

▶ Design del tipo “Use at will”

● Completamente ad oggetti in PHP 5

● Codice testato, Unit test (PHPUnit)

● Utilizzo di design pattern

▶ MVC, Factory, Singleton, etc

● Qualche numero:

▶ ≈ 4000 classi, ≈ 9000 files, ≈ 91 Mb

▶ ≈ 460'000 linee di codice

▶ Reference guide > 1000 pagine

Page 12: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Model View Controller (MVC) di ZF

Page 13: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Componenti del progetto ZF 1

Page 14: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Ciclo di sviluppo in ZF

● Proposta di un nuovo componente dalla community

● Ready for review (community)

● Ready for recommendation

● Pending recommendation (revisione da parte del CoreTeam)

● Ok per il Laboratory

● Testing e review

● Ok per il rilascio in release (standard/extras)

Page 15: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

La nuova versione 2.0

● Nuova architettura (MVC, Di, Events, Services, Modules)

● Performance migliorate● Utilizzo di PHP 5.3● Niente più CLA (Contributor License Agreement)

● Git (github) al posto di SVN● Sistema di packaging (pyrus, composer)

Page 16: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Versioni

● Stabile:▶ 1.11.11

● Beta:▶ 2.0.0beta4

● Prossime releases:▶ 1.12 (fine Giugno 2012)▶ 2.0.0beta5 (fine Giugno 2012)▶ 2.0 RC (estate 2012)

Page 17: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Zend Framework e Wordpress

Page 18: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Perchè utilizzare ZF in Wordpress?

● Migliorare lo sviluppo dei plugin

▶ Ridurre l'utilizzo di variabili globali▶ Utilizzo di codice OOP▶ Niente più codice deprecated

● Gestire progetti complessi

● Estendere le funzionalità di Wordpress

● Utilizzare uno standard di programmazione(migliorare la gestione di un team di sviluppo)

Page 19: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Possibili integrazioni

● Creazione di un semplice plugin:

▶ Inclusione della libreria ZF1▶ Gestione dell'autoloader di ZF2

● Utilizzo di plugin già pronti:

▶ Vulnero

Page 20: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Integrare ZF1 in Wordpress

Page 21: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Integrare ZF1

● Creare un file zf1.php in /wp-content/plugins:

/** * @package ZF1 *//*Plugin Name: ZF1Plugin URI: ...Description: Integrare Zend Framework 1 in WordpressVersion: 0.0.1Author: ...Author URI: ...License: GPLv2 or later*/set_include_path(get_include_path() . PATH_SEPARATOR . 'path_to_ZF_library');require_once 'Zend/Loader/Autoloader.php';$autoloader = Zend_Loader_Autoloader::getInstance();

Page 22: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Esempio: plugin per l'invio di email con ZF ad ogni aggiornamento di un post

class ZFmail { protected $mail; public function __construct() { $this->mail = new Zend_Mail(); add_action('publish_post', array($this, 'send')); } public function send($postId) { $this->mail->setBodyText("Ho appen aggiornato il blog con il post $postId"); $this->mail->addTo('email', 'Nome destinatario'); $this->mail->setSubject('Aggiornamento blog'); $this->mail->send(); return $postId; }}$zfMail = new ZFmail();

Page 23: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Esempio: admin panel con ZF view

● Creare una cartella con il nome del plugin, ad esempio test, in /wp-content/plugins/test

● Creare una sottocartella views all'interno di test/wp-content/plugins/test/views

● Inserire nella cartella views il codice HTML del pannello di amministrazione (file .phtml)

● Utilizzare l'azione admin_menu e la funzione add_options_page per aggiungere il pannello

Page 24: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Esempio: admin panel plugin

class ZFAdminPanel { protected $view; public function __construct() { $this->view = new Zend_View(); $this->view->setScriptPath(dirname(__FILE__) . '/views'); add_action('admin_menu', array($this,'admin_menu'));

add_action('admin_init', array($this,'register_options')); } public function admin_menu() { add_options_page('example', 'example', 'administrator', __FILE__,

array($this,'settings_page')); } public function settings_page() { echo $this->view->render('settings.phtml'); }

public function register_options() { register_setting('zfexample-settings-group', 'zfexample_option' ); }}$zfAdminPanel = new ZFAdminPanel();

Page 25: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Esempio: settings.phtml

<div class="wrap"> <h2><?php _e('Example', 'zfexample') ?></h2> <form method="post" action="options.php"> <?php settings_fields( 'zfexample-settings-group' ); ?> <table class="form-table"> <tr valign="top"> <th scope="row"><?php _e('Option', 'zfexample') ?></th> <td><input type="text" name="zfexample_option" value="<?php echo get_option('zfexample_option'); ?>" /></td> </tr> </table> <p class="submit"> <input type="submit" class="button-primary" value="<?php _e('Save Changes', 'zfexample') ?>" /> </p> </form></div>

Page 26: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Example panel

Page 27: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Integrare ZF2 in Wordpress

Page 28: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Integrare ZF2

● Creare un file zf2.php in /wp-content/plugins:/** * @package ZF2 *//*Plugin Name: ZF2Plugin URI: ...Description: Integrare Zend Framework 2 in WordpressVersion: 0.0.1Author: ...Author URI: ...License: GPLv2 or later*/require_once 'path_to_zf2_library/Zend/Loader/StandardAutoloader.php'; $loader = new Zend\Loader\StandardAutoloader(array( 'fallback_autoloader' => true,));$loader->register();

Page 29: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Utilizzo del plugin Vulnero

Page 30: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Vulnero

● Un plugin per l'utilizzo di Zend Framework all'interno di Wordpress

● Open source (licenza BSD-2)● Integrato con le API di Wordpress ● http://www.vulnero.com/● Sorgenti: https://github.com/andrew-kandels/vulnero

Page 31: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Cosa offre Vulnero

● Esegue il bootstrap delle funzionalità di Zend Framework in Wordpress

● Offre uno strumento di sviluppo rapido basato sulla piattaforma di content management di Wordpress

● Più di una semplice integrazione con ZF

Page 32: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Vulnero: funzionalità

● Bootstrapping (Zend_Application)● Routing Requests (Zend_Router_Route)● View Scripts (Zend_View)● Configuration (Zend_Config)● Caching (Zend_Cache)● Database (Zend_Db)● Authentication (Zend_Auth)

Page 33: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Esempio: routing

myapp.type=Zend_Controller_Router_Routemyapp.route=myapp/:controller/:actionmyapp.defaults.module=defaultmyapp.defaults.controller=indexmyapp.defaults.action=index

myapp.type=Zend_Controller_Router_Routemyapp.route=myapp/:controller/:actionmyapp.defaults.module=defaultmyapp.defaults.controller=indexmyapp.defaults.action=index

● Il routing dell'applicazione ha la precedenza● Le rotte non trovate vengono gestite da Wordpress● Le richieste vengono gestite tramite

controller/action tipiche di Zend Framework

Page 34: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Layout condivisi

● I layout di Wordpress vengono gestiti tramite Zend_Layout

● Vantaggi:

▶ Riutilizzo di migliaia di layout Wordpress▶ Separazione del codice HTML (vista) con il

codice PHP (business logic)

Page 35: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Esempio: widget

// application/widgets/Widget/SampleBox.php class Widget_SampleBox { // The widget's title (required) protected $_title = 'Sample Widget Box'; // Shown in the administration panel protected $_description = 'This is a sample widget.'; public function displayAction(array $settings) { $this->view->version = VULNERO_VERSION; }}

// application/widgets/Widget/SampleBox.php class Widget_SampleBox { // The widget's title (required) protected $_title = 'Sample Widget Box'; // Shown in the administration panel protected $_description = 'This is a sample widget.'; public function displayAction(array $settings) { $this->view->version = VULNERO_VERSION; }}

// application/views/scripts/widgets/sample-box.phtmlHello world, this is a sample widget!

// application/views/scripts/widgets/sample-box.phtmlHello world, this is a sample widget!

Controller

View

Page 36: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Esempio: integrazione con le API di Wordpress

class MyController extends Zend_Controller_Action {

public function indexAction() { $wp = $this->getInvokeArg('bootstrap') ->bootstrap('wordPress') ->getResource('wordPress'); if (!$colors = $wp->getCustomOptions('my-colors')) { $colors = array('red', 'blue'); $wp->setCustomOption('my-colors', $colors); } $this->view->colors = $colors; }}

class MyController extends Zend_Controller_Action {

public function indexAction() { $wp = $this->getInvokeArg('bootstrap') ->bootstrap('wordPress') ->getResource('wordPress'); if (!$colors = $wp->getCustomOptions('my-colors')) { $colors = array('red', 'blue'); $wp->setCustomOption('my-colors', $colors); } $this->view->colors = $colors; }}

Page 37: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Riferimenti

● Zend Framework, http://framework.zend.com/

● R. Allen, N. Lo, S. Brown, Zend Framework in Action, Manning Publications, 2009

● Writing a Wordpress plugin, http://codex.wordpress.org/Writing_a_Plugin

● B. Williams, O. Richard, J. Tadlock, Professional WordPress Plugin Development, Wrox 2011

● H. Stern, D. Damstra, B. Williams, Professional WordPress, Wrox 2010

● A. Kandels, Introducing a Zend Framework library that allows you develop applications on top of WordPress, Slideshare http://slidesha.re/LqT3yD

Page 38: Integrare Zend Framework in Wordpress

© All rights reserved. Zend Technologies, Inc.

Grazie!

Per ulteriori informazioni:[email protected]

http://framework.zend.com/http://www.zend.com