ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

ObjectSetting.php (3594B)


      1 <?php
      2 
      3 namespace Materialis\Customizer\Settings;
      4 
      5 class ObjectSetting extends \Materialis\Customizer\BaseSetting
      6 {
      7     private $settingPattern = "";
      8 
      9     public function init()
     10     {
     11         $this->settingPattern = '/'.$this->id.'\[(.*)\]/s';
     12 
     13         $controls = isset($this->cpData['controlsMap']) ? $this->cpData['controlsMap'] : array();
     14 
     15         if (isset($this->cpData['title'])) {
     16             $titleControl =array(
     17                'cp__element__title' => array(
     18                     "class" => "\\Materialis\\Customizer\\Controls\\LabelControl",
     19                     "wp_data" => array(
     20                         "label" => $this->cpData['title']
     21                     )
     22                  )
     23             );
     24 
     25             $controls = array_merge($titleControl, $controls);
     26         }
     27 
     28         foreach ($controls as $key => $value) {
     29             $this->companion()->customizer()->registerSettings(
     30                 $this->manager,
     31                 array(
     32                     "{$this->id}[{$key}]"=> array(
     33                        "section"=>$this->cpData['section'],
     34                        "wp_data"=>array(
     35                            'default'=> $this->companion()->getCustomizerData("customizer:settings:{$this->id}:wp_data:default:{$key}"),
     36                            'tranport'=> $this->transport
     37                        ),
     38                        "control"=>$value
     39                     )
     40                 )
     41             );
     42         }
     43 
     44 
     45         add_filter('cloudpress\customizer\temp_mod_exists', array($this, 'tempKeyExists'), 10, 2);
     46         add_filter('cloudpress\customizer\temp_mod_content', array($this, '__tempContent'), 10, 2);
     47     }
     48 
     49     public function tempKeyExists($value, $mod)
     50     {
     51         return ($value || ($mod === $this->id));
     52     }
     53 
     54 
     55     public function __tempContent($value, $mod)
     56     {
     57         if($mod === $this->id){
     58             return $this->tempContent();
     59         }
     60 
     61         return $value;
     62     }
     63 
     64     public function tempContent(){
     65         $settings = $this->manager->unsanitized_post_values();
     66 
     67 
     68         $result = array();
     69         foreach ($settings as $setting => $value) {
     70             if (strpos($setting, $this->id . "[") ===0) {
     71                 $matches = array();
     72                 preg_match($this->settingPattern, $setting, $matches);
     73 
     74                 $key = $matches[1];
     75                 $result[$key] = $value;
     76             }
     77         }
     78 
     79         $savedData = get_theme_mod($this->id);
     80         $result = array_merge($savedData,$result);
     81 
     82         return $result;
     83     }
     84 
     85 
     86     public function setControl()
     87     {
     88     }
     89 
     90 
     91     public function update($value)
     92     {
     93         $value = $this->tempContent();
     94         $value = array_merge($value, $currentValue);
     95 
     96 
     97         if (isset($value['cp__element__title'])) {
     98             unset($value['cp__element__title']);
     99         }
    100 
    101         if (isset($value["{$this->id}[cp__element__title]"])) {
    102             unset($value["{$this->id}[cp__element__title]"]);
    103         }
    104 
    105         set_theme_mod($this->id, $value);
    106     }
    107 
    108 
    109     public function value()
    110     {
    111         if ($this->is_previewed) {
    112             $value = $this->tempContent();
    113         } else {
    114             $mod = $this->id;
    115             $value = get_theme_mod($mod, false);
    116         }
    117 
    118         if (isset($value['cp__element__title'])) {
    119             unset($value['cp__element__title']);
    120         }
    121 
    122         if (isset($value["{$this->id}[cp__element__title]"])) {
    123             unset($value["{$this->id}[cp__element__title]"]);
    124         }
    125 
    126         return $value;
    127     }
    128 }