create wordpress plugin with zend framework 2 - php

I want to develop a membership plugin under wordpress and for this I want to use zend framework 2.
Does anyone managed to create a wordpress plugin using zend framework 2?
I'm new to zf and I do not know how and where to start from.
I tried to start from zend skeleton application but got stuck at add_menu_pages and displaying a simple dashboard.
Can anyone give me some ideas or links.
Thanks!
Updated!
I managed to get this working! I just needed to use a PhpRenderer. For those who need a little more help here is how I did:
I created a class that manages all admin area. On class init I called a method that created menu pages( in this method simply add_menu_pages() and instead of callback_function I called a new method, manage_pages, that, wel... manages pages, but you can do it as you desire) and then I initiated the view, like this:
$this->view = new PhpRenderer();
$this->map = new Resolver\TemplateMapResolver(array(
'template_name' => 'template_path',
'template2_name'=> 'template2_path')); //this is for handling view templates a little easier
$this->resolver = new Resolver\TemplateMapResolver($this->map);
$this->view->setResolver($this->resolver);
$this->model = new ViewModel();
Further, in manage_pages method, for each page I have, I added its own template and variables I needed
$this->model->setTemplate('template_name');
$this->model->setVariable('variable_name', value);
As for displaying template, you just have to write this piece of code:
echo $this->view->render($this->model);
In the template files you can access variables using $this->variable_name
Also you can insert another template using $this->partial( 'template2_name', assoc_arrray_of_variables_to_be_passed_to_template ).
And this is it! If you have any questions, please let me know!

There is a wordpress plugin you can use, search "wopzen2" or "wordpress and zend framework 2 integration" on Google, with this solution, you can use the following code inside the php wordpress code:
global $wpzf2plugin;
$render=$wpzf2plugin->render('/application/index/contactform');
echo $render;
This code calls the contactform action, if you are familiar with zendframework I think you are going to understand it.
This plugin is dedicated to developers.
You can get a free version of the plugin via support center.
I hope this answer can be helpful for you
reference link:
Example codes

Related

SilverStripe 4 custom error page not shown

In my last project on SS 4, I'm trying to create my usual custom error pages - with same approach used on v. 3:
Creating ErrorPage.ss inside /themes/mysite/templates/Layout;
Including $Content inside template;
After a successful /dev/build?flush=all I don't see any content, neither right template (I see the default Page.ss one instead).
In back-end there are error pages saved and published. In /assets/ there are the static ones too.
Maybe the procedure is changed in this new version? Any advice?
Thanks in advance everyone.
Your ErrorPage.ss folder structure needs to match the core PHP class's namespace (SilverStripe\ErrorPage\ErrorPage), i.e. themes/mytheme/templates/SilverStripe/ErrorPage/Layout/ErrorPage.ss
For Silverstripe 4 ErrorPage.
To display the ErrorPage you need to match the namespace of ErrorPage into your template by adding this into your app folder. E.g.template/SilverStripe/ErrorPage/ErrorPage.ss
Don't forget to dev/build?flush afterwards

call member function out of Yii?

I know it's not legal, and looking odd. But it's necessary for me.
I have two projects, one is custom Open-Cart, and the second is in Yii. My main project is opencart. My Yii project is kept in the Main project root.
Now I want to call a Yii function in my open cart.
please anyone help me and tell How to call the Yii function in my main project?
this is my Yii function:-
$sm=Yii::app()->getSecurityManager();
if ($salt === null)
$salt = Yii::app()->params['password_security_salt'];
if($salt==null)
$salt=md5 (mt_rand ().mt_rand ().mt_rand ().mt_rand ());
$pass=sha1($salt.$pass.$salt);
return $sm->hashData($pass,$key).':'.$salt;
.....................
I want to create a new function manually for my opencart project.
please help me for creating a new project for the same functionality as Yii (upper function) function.
You can use Yii functionality outside of the Yii project by initialising the application like so:
// this is in someotherfile.php outside of the yii project
require_once('framework/yii.php');
$config = require_once('protected/config/main.php');
Yii::createWebApplication($config);
// call your function
Yii::app()->getSecurityManager();
Obviously the paths to the yii.php file and the application config (main.php) will need to change to fit your project structure

Cant access Magento session in Wordpress

I am making use of the Wordpress Plugin MWI - Mage/WP Integration to create a site that has Wordpress at its heart and Magento in a sub-folder.
I want to be able to access the Magento session in Wordpress pages. Thanks to the MWI plugin I am able to access a "customer/session", however it appears to be a different session to the one that is used when I go to the Magento part of the website.
I know this because I have added data to the session in Magento, but when I come to the homepage (powered by Wordpress) that custom data is not there!
Here is how Im adding the extra data in Magento:
Mage::getSingleton('customer/session')->setData("foo","bar");
Then getting it with:
Mage::getSingleton('customer/session')->getData("foo");
This returns NULL.
Any ideas?
So, after a little more trial & error I was able to resolve this.
Essentially, by switching to core/session rather than customer/session, I can then access the same session in Wordpress. So setting & getting become:
Mage::getSingleton('core/session')->setData("foo","bar");
Mage::getSingleton('core/session')->getData("foo");
In Wordpress it is important to use:
Mage::getSingleton('core/session', array('name' => 'frontend'))->getData("foo");
There is a plugin which does exactly that:
http://wordpress.org/plugins/mage-enabler/
Also, as alternative solution, we can create simple module with the helper for getting session variables. Here's helper funtcion that returns Magento session:
class Namespace_Modulename_Helper_Data extends Mage_Core_Helper_Abstract
{
public function getSession()
{
return return Mage::getSingleton('core/session', array('name' => 'frontend'));
}
}
Then we can access session data via such code:
require_once('path_to/Mage.php');
Mage::app();
var_dump(Mage::helper('namespace_modulename')->getSession()->getData());

widget within module in Yii

I'm trying to create a widget within the module and then load that widget from 'outside' of the module. More particularly I'm using user module written by someone else. I don't want to have a separate page for displaying a login form, therefore I tried to make a CPortlet/widget (confusion) displaying the login form. Basically, I've moved the code from LoginController into that widget. Then I try to display the widget on some random page by
<?php $this->widget('user.components.LoginForm'); ?>
However, I get an error
CWebApplication does not have a method named "encrypting".
in UserIdentity class in this line:
else if(Yii::app()->controller->module->encrypting($this->password)!==$user->password)
This happens, because I'm basically trying to execute this code within context of the app and not the module. Thus the "Yii::app()->controller->module" trick doesn't really work as expected.
What am I doing wrong:-\
Is there a better way to achieve this. I.e. display that login form in some other page, which is normally displayed by accessing login controller within user module (user/login) or is a widget the right way of doing it?
Thanks.
The quick solution
Ok, so I simply ended up doing
Yii::app()->getModule('user')->encrypting($this->password)
instead of
Yii::app()->controller->module->encrypting($this->password)
Notice that now the module must be called 'user' in the main config, but I think this allows for more flexibility. I.e. we're not bound to only use module functionality within the module.
Additional insight on displaying widget outside of the module scope
After playing more with it that's what I did. In the UserModule.php I've created a method
public static function id() {
return 'user';
}
Then everywhere where I need the module I use
Yii::app()->getModule(UserModule::id())->encrypting($this->password)
I don't like having many imports related to the module like:
'application.modules.user.models.*',
'application.modules.user.components.*',
Because we already have those imports in the UserModule.php:
public function init()
{
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'user.models.*',
'user.components.*',
));
}
Therefore whenever you know that some piece of functionality will be used outside of the module it's important to make sure the module is loaded. For example, in the LoginForm widget that I am trying to display NOT in one of the module controllers, I have this line of code:
$model = new UserLogin;
However, UserLogin is a model inside of the User module, and in order to be able to autoload this model we first have to make sure the module was initialised:
$module = Yii::app()->getModule(UserModule::id());
$model = new UserLogin;
I hope this will be helpful if you were stuck with the whole modules concept the way I was.
http://www.yiiframework.com/forum/index.php?/topic/6449-access-another-modules-model/ was useful but hard to find =)
You better move that encrypting() into a MyUserIdentiy class which extends CUserIdentity. Whatever the code you take to use, they putting the method in controller is a bad idea and as a result you cannot reuse that code.
The login form should still post to User/Login controller but I guess they use Yii's standard login code and you might want to modify it to use the MyUserIdentity.

How to make a PHP script that's pluginable?

I'm creating my own script using the CodeIgniter MVC framework. Now, i want users to easily modify the site functionality and adding their own without modifying the code which i've already written.
How do i make my site pluginable ?
EDIT: The users would be the site admins. Not the end user. Basically just like drupal or joomla. Want the admin to be able to create/add plugins to extend site functionality.
There may be a better way that's specific to CodeIgniter, but this is what I would do:
First, create functions for various "hook points" in your code. Say, a function named PreArticle that you call in your code, before an article is displayed.
Allow the user to write code like this:
addHook_PreArticle('funcToCall');
function funcToCall( &$articleText ) {
$articleText = str_replace('Hello', 'World', $articleText);
}
addHook_PreArticle is a function you've defined, which would add the passed string to some internal list. Then when the PreArticle function is called, each of those functions are executed, passing in any appropriate parameters that you define.
Many CMS's Like Joomla and Blogs like Wordpress use variable function names:
$function="phpinfo";
$function();
You could load this into an array to create a list of functions that can be overridden.
That's a perfect case to use the Observer Pattern.
http://devzone.zend.com/article/5

Categories