/plugins/wpbridge/lib/wptools.php

https://github.com/demental/M · PHP · 63 lines · 51 code · 4 blank · 8 comment · 3 complexity · 08fe0155f8268e0c96f0d8e50ca6b482 MD5 · raw file

  1. <?php
  2. /**
  3. * Wordpress integration into the M framework
  4. * Requires that yuo install the extendedapi plugin into wordpress.
  5. * You also need to activate the following :
  6. * * get_page_by_path
  7. * * do_shortcode
  8. * * wpautop
  9. */
  10. class WPTools {
  11. protected static $_inited = false;
  12. protected static $_settings = array(
  13. 'wp_filepath'=>''
  14. );
  15. public static $namespace = 'extapi';
  16. public static $wp_root;
  17. public static $wp_login;
  18. public static $wp_password;
  19. public function getPage($pageID, $apply_shortcodes = false)
  20. {
  21. $options = array(
  22. 'caching' =>true,
  23. 'cacheDir' => APP_ROOT.'app/'.APP_NAME.'/cache/',
  24. 'lifeTime' => 3600,
  25. 'fileNameProtection'=>true,
  26. );
  27. $cache = new Cache_Lite($options);
  28. $cacheName = 'wptool'.self::$wp_root.$pageID.'_'.$apply_shortcodes;
  29. if($_cachedData = $cache->get($cacheName)) {
  30. return unserialize($_cachedData);
  31. }
  32. $c = self::_fetchPage($pageID, $apply_shortcodes);
  33. $cache->save(serialize($c));
  34. return $c;
  35. }
  36. public static function on_switch_lang($new_lang) {
  37. foreach(array('wp_root','wp_login','wp_password') as $varname) {
  38. $constname = strtoupper($new_lang.'_'.$varname);
  39. if(!defined($constname)) {
  40. $constname = strtoupper('default_'.$varname);
  41. }
  42. WPTools::$$varname = constant($constname);
  43. }
  44. }
  45. protected static function _fetchPage($pageID, $apply_shortcodes = false)
  46. {
  47. try {
  48. $client = new Zend\XmlRpc\Client(self::$wp_root.'/xmlrpc.php');
  49. $c = (object)$client->getProxy(self::$namespace)->callWpMethod(self::$wp_login, self::$wp_password,'get_page_by_path', array($pageID));
  50. $c->post_content = $client->getProxy(self::$namespace)->callWpMethod(self::$wp_login, self::$wp_password, 'wpautop', array($c->post_content));
  51. if($apply_shortcodes) {
  52. $c->post_content = $client->getProxy(self::$namespace)->callWpMethod(self::$wp_login, self::$wp_password,'do_shortcode', array($c->post_content));
  53. }
  54. } catch(Exception $e) {
  55. $c = new Stdclass();
  56. }
  57. return $c;
  58. }
  59. }