Advertisement
fruffl

WP Options Factory

May 22nd, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.94 KB | None | 0 0
  1. <?PHP
  2.     NAMESPACE tri4m\Wp\Wtf;
  3.     USE tri4m\Wp\__const_Action;
  4.     USE tri4m\Wp\__const_Filter;
  5.     USE tri4m\Wp\__type_Action;
  6.     USE tri4m\Wp\__type_Call;
  7.     USE tri4m\Wp\__type_Filter;
  8.     USE tri4m\Wp\Inv;
  9.     USE tri4m\Wp\Theme;
  10.     USE tri4m\Wp\Wtf\__type_Settings;
  11.     USE tri4m\Wp\Wtf\Settings\__type_Messages;
  12.     USE tri4m\Wp\Wtf\Settings\__type_Field;
  13.     USE tri4m\Wp\Wtf\Settings\__type_Section;
  14.     USE tri4m\Wp\Wtf\Settings\__type_Page;
  15.     USE tri4m\Wp\Wtf\__type_SubMenuPage;
  16.     USE tri4m\Wp\Wtf\SubMenuPage;
  17.     USE ILLI\Core\Std\Def\__const_Type;
  18.     USE ILLI\Core\Std\Invoke;
  19.     USE ILLI\Core\Util\Inflector;
  20.     USE ILLI\Core\Util\String;
  21.    
  22.     CLASS Settings EXTENDS \tri4m\Wp\Wtf
  23.     {
  24.         protected $__Page   = [];
  25.         protected $__pageHook   = NULL;
  26.         protected $__cache  = [];
  27.        
  28.         protected $__actions    =
  29.         [
  30.             __const_Action::ADMIN_INIT => NULL,
  31.             __const_Action::ADMIN_MENU => NULL
  32.         ];
  33.        
  34.         function __construct(__type_Settings $__Setup)
  35.         {
  36.             static $__STATIC_fnI, $__STATIC_fnV, $__STATIC_fnF, $__STATIC_fnS, $__STATIC_fnR, $__STATIC_fnP;
  37.            
  38.             isset($__STATIC_fnI) ?: $__STATIC_fnI = function(__type_Call $T)
  39.             {
  40.                 switch(TRUE):
  41.                     case $T->isVal($T::fn, __const_Type::SPL_CLOSURE):  return Invoke::emitCallable($T->fn, $T->arguments);
  42.                     case $T->isVal($T::fn, __const_Type::SPL_FUNCTION): return Invoke::emitFunction($T->fn, $T->arguments);
  43.                     case $T->isVal($T::fn, __const_Type::SPL_METHOD):   return Invoke::emit($T->fn[0], $T->fn[1], $T->arguments);
  44.                 endswitch;
  45.             };
  46.            
  47.             isset($__STATIC_fnV) ?: $__STATIC_fnV = function(__type_Settings $__Setup, array $__cache, array $__input)
  48.             {
  49.                 if(FALSE === isset($_POST['action']))
  50.                     return;
  51.                    
  52.                 $result = [];
  53.                
  54.                 switch($_POST['action']):
  55.                     case 'reset':
  56.                    
  57.                         if($__Setup->messages instanceOf __type_Messages)
  58.                             Inv::addSettingsError($__Setup->id, 'restore_defaults', $__Setup->messages->resetComplete, 'updated fade');
  59.                        
  60.                         if(TRUE === $__Setup->deep)
  61.                         {
  62.                             foreach($__cache as $sid => &$fields)
  63.                                 foreach($fields as $fid => &$Field)
  64.                                     $result[$sid][$fid] = $Field->std;
  65.                         }
  66.                         else
  67.                         {
  68.                             foreach($__cache as $id => &$Field)
  69.                                 $result[$id] = $Field->std;
  70.                         }      
  71.                        
  72.                         break;
  73.                        
  74.                     case 'import':
  75.                    
  76.                         if($result = unserialize(stripslashes(trim($_POST['import']))))
  77.                         {
  78.                             if($__Setup->messages instanceOf __type_Messages)
  79.                                 Inv::addSettingsError($__Setup->id, 'import', $__Setup->messages->importComplete, 'updated fade');
  80.                             break;
  81.                         }
  82.                        
  83.                         if($__Setup->messages instanceOf __type_Messages)
  84.                             Inv::addSettingsError($__Setup->id, 'import', $__Setup->messages->importError, 'updated fade');
  85.                         break;
  86.                    
  87.                     case 'update':
  88.                    
  89.                         $v = function(__type_Field $Field, $i)
  90.                         {
  91.                             if(FALSE === isset($i))
  92.                                 return $Field->std;
  93.                            
  94.                             if(NULL !== ($_ = $Field->fn))
  95.                                 return $_($i, $Field);
  96.                            
  97.                             //if(Inv::hasFilter('tri4m_sanitize_'.$Field->type))
  98.                             //  $result[$id] = Inv::applyFilters('tri4m_sanitize_' . $Field->type, $__input[$id], $Field);
  99.                                 return $i;
  100.                                
  101.                             return $Field->std;
  102.                         };
  103.                        
  104.                         if(TRUE === $__Setup->deep)
  105.                         {
  106.                             foreach($__cache as $sid => &$fields)
  107.                                 foreach($fields as $fid => &$Field)
  108.                                 {
  109.                                     $i = &$__input[$sid][$fid];
  110.                                     $result[$sid][$fid] = $v($Field, $i);
  111.                                 }
  112.                         }
  113.                         else
  114.                         {
  115.                             foreach($__cache as $id => &$Field)
  116.                             {
  117.                                 $i = &$__input[$id];
  118.                                 $result[$id] = $v($Field, $i);
  119.                             }
  120.                         }
  121.                        
  122.                         break;
  123.                 endswitch;
  124.                
  125.                 return [] === $result ? NULL : $result;
  126.             };
  127.            
  128.             isset($__STATIC_fnS) ?: $__STATIC_fnS = function(__type_Settings $__Setup, __type_Section $__Section) use (&$__STATIC_fnI)
  129.             {
  130.                 Inv::addSettingsSection
  131.                 (
  132.                     $__Section->id,
  133.                     $__Section->title,
  134.                     function() use (&$__Section, &$__STATIC_fnI)
  135.                     {
  136.                         switch(TRUE):
  137.                             case is_string($c = $__Section->content):
  138.                                 print $c;
  139.                                 break;
  140.                            
  141.                             case is_array($c = $__Section->content):
  142.                                 print implode(PHP_EOL, $c);
  143.                                 break;
  144.                            
  145.                             case ($c = $__Section->content) instanceOf __type_Call:
  146.                                 print is_array($c = $__STATIC_fnI($c)) ? implode(PHP_EOL, $c) : $c;
  147.                                 break;
  148.                         endswitch;
  149.                     },
  150.                     $__Setup->id
  151.                 );
  152.             };
  153.            
  154.             isset($__STATIC_fnF) ?: $__STATIC_fnF = function(__type_Settings $__Setup, __type_Section $__Section, __type_Field $__Field)
  155.             {
  156.                 Inv::addSettingsField
  157.                 (
  158.                     $__Field->id,
  159.                     $__Field->title,
  160.                     function() use (&$__Setup, &$__Section, &$__Field)
  161.                     {
  162.                         if(TRUE === $__Setup->deep)
  163.                         {
  164.                             if(FALSE === ($options = Inv::getOption($__Setup->id))
  165.                             || FALSE === isset($options[$__Section->id][$__Field->id]))
  166.                                 $options[$__Section->id][$__Field->id] = $__Field->std;
  167.                         }
  168.                         else
  169.                         {
  170.                             if(FALSE === ($options = Inv::getOption($__Setup->id))
  171.                             || FALSE === isset($options[$__Field->id]))
  172.                                 $options[$__Field->id] = $__Field->std;
  173.                         }
  174.                            
  175.                         $pattern = [
  176.                             'id'        => '{:setupId}_{:sectionId}_{:fieldId}',
  177.                             'name'      => TRUE === $__Setup->deep
  178.                                         ? '{:setupId}[{:sectionId}][{:fieldId}]'
  179.                                         : '{:setupId}[{:fieldId}]',
  180.                             'type'      => '{:type}',
  181.                             'value'     => '{:value}'
  182.                         ];
  183.                        
  184.                         $config = [
  185.                             'fieldId'   => $__Field->id,
  186.                             'sectionId'     => $__Section->id,
  187.                             'setupId'   => $__Setup->id,
  188.                             'value'     => TRUE === $__Setup->deep
  189.                                         ? $options[$__Section->id][$__Field->id]
  190.                                         : $options[$__Field->id],
  191.                             'type'      => $__Field->type
  192.                         ];
  193.                        
  194.                         foreach($pattern as &$_)
  195.                             $_ = String::insert($_, $config);
  196.                        
  197.                         print String::insert('<input {:attr} />', ['attr' => String::attribute($pattern, ['delimeter' => ' '])]);
  198.                     },
  199.                     $__Setup->id,
  200.                     $__Section->id,
  201.                     [
  202.                         'type'      => $__Field->type,
  203.                         'desc'      => $__Field->desc,
  204.                         'id'        => String::insert('{:setupId}_{:fieldId}', ['setupId' => $__Setup->id, 'fieldId' => $__Field->id]),
  205.                         'label_for' => String::insert('{:setupId}_{:fieldId}', ['setupId' => $__Setup->id, 'fieldId' => $__Field->id]),
  206.                         'choices'   => $__Field->choices,
  207.                         'std'       => $__Field->std,
  208.                         'class'     => $__Field->cssClass
  209.                     ]
  210.                 );
  211.             };
  212.            
  213.             isset($__STATIC_fnR) ?: $__STATIC_fnR = function(__type_Settings $__Setup, array &$__cache) use (&$__STATIC_fnV)
  214.             {
  215.                 Inv::registerSetting
  216.                 (
  217.                     $__Setup->id,
  218.                     $__Setup->id,
  219.                     NULL === $__Setup->fn
  220.                         ? function($__input) use (&$__Setup, &$__cache, &$__STATIC_fnV)
  221.                         {
  222.                             return $__STATIC_fnV($__Setup, $__cache, $__input);
  223.                         }
  224.                         : $__Setup->fn
  225.                 );
  226.             };
  227.            
  228.             isset($__STATIC_fnP) ?: $__STATIC_fnP = function(__type_Settings $__Setup)
  229.             {
  230.                 print $__Setup->page->beforeForm;
  231.                 settings_fields($__Setup->id);
  232.                 do_settings_sections($__Setup->id);
  233.                 print $__Setup->page->beforeCtrl;
  234.                 print $__Setup->page->submit;
  235.                 print $__Setup->page->reset;
  236.                 print $__Setup->page->afterCtrl;
  237.                 print $__Setup->page->afterForm;
  238.             };
  239.            
  240.             $id         = $__Setup->id;
  241.             $this->__Setup      = $__Setup;
  242.             $this->__Setup->id  = Theme::slug(Inflector::variablize(Inflector::underscore($this->__Setup->id)));
  243.            
  244.             if($this->__Setup->page instanceOf __type_Page)
  245.             {
  246.                 $config = [];
  247.                
  248.                 foreach([
  249.                     __type_Page::parentId       => __type_SubMenuPage::parentId,
  250.                     __type_Page::title      => __type_SubMenuPage::title,
  251.                     __type_Page::menuTitle      => __type_SubMenuPage::menuTitle,
  252.                     __type_Page::cssClass       => __type_SubMenuPage::cssClass,
  253.                     __type_Page::capability     => __type_SubMenuPage::capability,
  254.                     __type_Page::icon       => __type_SubMenuPage::icon
  255.                 ] as $i => $k)
  256.                     $config[$k] = $this->__Setup->page->get()->toArray()[$i];
  257.                
  258.                 $config[__type_SubMenuPage::id]     = $id;
  259.                 $config[__type_SubMenuPage::content]    = new __type_Call([
  260.                     __type_Call::fn     => function() use (&$__STATIC_fnP)
  261.                     {
  262.                         $__STATIC_fnP($this->__Setup);
  263.                     }
  264.                 ]);
  265.                
  266.                 $this->__Page = new SubMenuPage(new __type_SubMenuPage($config));
  267.             }
  268.            
  269.             $this->__actions[__const_Action::ADMIN_INIT] = new __type_Action([
  270.                 __type_Action::argsNum  => 0,
  271.                 __type_Action::priority => 101,
  272.                 __type_Action::fn   => function() use (&$__STATIC_fnV, &$__STATIC_fnF, &$__STATIC_fnS, &$__STATIC_fnR)
  273.                 {
  274.                     $__STATIC_fnR($this->__Setup, $this->__cache);
  275.                    
  276.                     foreach($this->__Setup->sections->get() as $Section)
  277.                     {
  278.                         $Section->id = Inflector::variablize(Inflector::underscore($Section->id));
  279.                        
  280.                         $__STATIC_fnS($this->__Setup, $Section);
  281.                        
  282.                         foreach($Section->fields->get() as $Field)
  283.                         {
  284.                             $Field->id = Inflector::variablize(Inflector::underscore($Field->id));
  285.                            
  286.                             TRUE === $this->__Setup->deep
  287.                                 ? $this->__cache[$Section->id][$Field->id]  = $Field
  288.                                 : $this->__cache[$Field->id]            = $Field;
  289.                            
  290.                             $__STATIC_fnF($this->__Setup, $Section, $Field);
  291.                         }
  292.                     }
  293.                    
  294.                    
  295.                 }
  296.             ]);
  297.            
  298.             /*
  299.             $this->__actions[__const_Action::ADMIN_MENU] = new __type_Action([
  300.                 __type_Action::argsNum  => 0,
  301.                 __type_Action::priority => 100,
  302.                 __type_Action::fn   => function() use (&$__STATIC_fnP)
  303.                 {
  304.                     $this->__pageHook = $__STATIC_fnP($this->__Setup);
  305.                 }
  306.             ]);
  307.             /*
  308.             $this->__actions[String::insert(__const_Action::LOAD_PAGE_HOOK__, ['pageHook' => $this->__pageHook])] = new __type_Action([
  309.                 __type_Action::argsNum  => 2,
  310.                 __type_Action::priority => 105,
  311.                 __type_Action::fn   => function($__pageNow, $__WPScreen)
  312.                 {
  313.                     wp_register_style('admin', get_bloginfo('template_url') . '/styles.css');
  314.                     wp_enqueue_style( 'admin' );   
  315.                 }
  316.             ]);
  317.             */
  318.         }
  319.        
  320.         public function install()
  321.         {
  322.             $this->__Page instanceOf SubMenuPage
  323.                 ? parent::install()->__Page->install()
  324.                 : parent::install();
  325.            
  326.             return $this;
  327.         }
  328.        
  329.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement