コード例 #1
0
 public function test()
 {
     if (empty($xml)) {
         $xml = $this->xml_data;
     }
     $module = $this->get_module()->option('module_name');
     $dom = new DOMDocument();
     $item = $dom->createElement('item');
     $dom->appendChild($item);
     $data = array('_wpcf7-form' => 'sfsdf', '_mail_2' => array('active' => 'A', 'subject' => 'B', 'sender' => '1', 'body' => '', 'recipient' => '', 'additional_headers' => ''));
     $ele = HW_Export::array_to_xml_params($data, 0, false);
     #$ele=new DOMElement('a','B');
     $i = $dom->importNode($ele, true);
     $item->appendChild($i);
 }
コード例 #2
0
 /**
  * add item
  * @param $data
  * @param $atts
  * @return string post name
  */
 public function addItem($data, $atts = array())
 {
     if (!isset($data['post_type'])) {
         return;
     }
     //invalid
     $post_identify = '';
     $item = $this->createElement('item');
     if (count($atts)) {
         //set attributes
         foreach ($atts as $name => $value) {
             if ($name == 'name') {
                 $post_identify = sanitize_title($value);
             }
             $item->setAttribute($name, $value);
         }
     }
     $user_info = get_userdata(1);
     //add more elements
     $title = isset($data['title']) ? $data['title'] : '';
     $description = isset($data['description']) ? $data['description'] : '';
     $time = date('Y-m-d H:i:s');
     $status = isset($data['status']) ? $data['status'] : 'publish';
     $post_parent = isset($data['post_parent']) ? $data['post_parent'] : '0';
     $post_password = isset($data['post_password']) ? $data['post_password'] : '';
     $menu_order = isset($data['menu_order']) ? $data['menu_order'] : '0';
     $is_sticky = isset($data['is_sticky']) ? $data['is_sticky'] : '0';
     $content = isset($data['content']) ? $data['content'] : '';
     $excerpt = isset($data['excerpt']) ? $data['excerpt'] : '';
     //valid
     if ($post_identify === '') {
         $post_identify = sanitize_title($title);
     }
     if ($this->get(sanitize_title($title))) {
         return $post_identify;
     }
     //already exists
     //or item->ownerDocument->createElement
     $item->appendChild($this->createElement('title', $this->cdata($title)));
     //title
     $item->appendChild($this->createElement('pubDate', $this->cdata(date('l, F j, Y'))));
     //pubDate
     $item->appendChild($this->createElement('dc:creator', $this->cdata($user_info->user_login)));
     //creator
     $item->appendChild($this->createElement('description', $this->cdata($description)));
     //description
     $item->appendChild($this->createElement('wp:post_date', $this->cdata($time)));
     //post_date
     $item->appendChild($this->createElement('wp:post_date_gmt', $this->cdata($time)));
     //post_date_gmt
     $item->appendChild($this->createElement('wp:status', $this->cdata($status)));
     //status
     $item->appendChild($this->createElement('wp:post_parent', $this->cdata($post_parent)));
     //$post_parent
     $item->appendChild($this->createElement('wp:menu_order', $this->cdata($menu_order)));
     //menu_order
     $item->appendChild($this->createElement('wp:post_password', $this->cdata($post_password)));
     //post_password
     $item->appendChild($this->createElement('wp:is_sticky', $this->cdata($is_sticky)));
     //is_sticky
     $item->appendChild($this->createElement('wp:post_type', $this->cdata($data['post_type'])));
     //post_type
     if (!is_object($content)) {
         $item->appendChild($this->createElement('content:encoded', $this->cdata($content)));
     } else {
         $content_encoded = $this->createElement('content:encoded', null);
         $content_encoded->appendChild($content_encoded->ownerDocument->importNode(HWIE_Param::get_hw_element($content, false), true));
         $item->appendChild($content_encoded);
     }
     $item->appendChild($this->createElement('excerpt:encoded', $this->cdata($excerpt)));
     //excerpt
     //attachment
     if (isset($data['attachments'])) {
         foreach ($data['attachments'] as $attach) {
             $item->appendChild($item->ownerDocument->importNode($attach, true));
         }
     }
     //post meta
     if (isset($data['post_metas']) && is_array($data['post_metas']) && count($data['post_metas'])) {
         foreach ($data['post_metas'] as $key => $value) {
             $post_meta = $this->createElement('wp:postmeta', null);
             $meta_key = $this->createElement('wp:meta_key', $key);
             $meta_value = $this->createElement('wp:meta_value');
             $post_meta->appendChild($meta_key);
             $post_meta->appendChild($meta_value);
             if (is_string($value) || is_numeric($value)) {
                 $meta_value->nodeValue = $this->cdata($value);
                 //appendChild($doc->createTextNode(..))
                 #$meta_value = $this->createElement('wp:meta_value', $this->cdata($value));
             } elseif (is_array($value)) {
                 #$meta_value = $this->createElement('wp:meta_value');
                 $ele = HW_Export::array_to_xml_params($value, $this, false);
                 $meta_value->appendChild($ele);
             } elseif (HWIE_Param::get_hw_element($value) instanceof DOMElement) {
                 $meta_value->appendChild(HW_Export::element_to_wxr_params(HWIE_Param::get_hw_element($value)->get(), $this));
             }
             $item->appendChild($post_meta);
             //add to doc
         }
     }
     //category,tag, term
     if (isset($data['terms']) && is_array($data['terms'])) {
         foreach ($data['terms'] as $domain => $terms) {
             if (is_array($terms)) {
                 foreach ($terms as $nicename => $name) {
                     $category = $this->createElement('category', $name);
                     $category->setAttribute('domain', $domain);
                     $category->setAttribute('nicename', $nicename);
                     $item->appendChild($category);
                     //add to doc
                 }
             } elseif (HWIE_Param::get_hw_element($terms, false) instanceof DOMElement) {
                 $item->appendChild(HWIE_Param::get_hw_element($terms, false));
             }
         }
     }
     #$this->appendChild($item);
     if ($title && !$this->get(sanitize_title($title))) {
         $this->posts->appendChild($item);
         if ($title) {
             $this->add(sanitize_title($title), $item);
         }
     }
     return $post_identify;
 }