RCCWP_CustomWritePanel::GetCapabilityName PHP Method

GetCapabilityName() public static method

Create a capability name for a write panel given its name. This function is copied from WP's sanitize_title_with_dashes($title) (formatting.php)
public static GetCapabilityName ( string $customWritePanelName ) : string
$customWritePanelName string panel name
return string capability name
    public static function GetCapabilityName($customWritePanelName)
    {
        // copied from WP's sanitize_title_with_dashes($title) (formatting.php)
        $capabilityName = strip_tags($customWritePanelName);
        // Preserve escaped octets.
        $capabilityName = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $capabilityName);
        // Remove percent signs that are not part of an octet.
        $capabilityName = str_replace('%', '', $capabilityName);
        // Restore octets.
        $capabilityName = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $capabilityName);
        $capabilityName = remove_accents($capabilityName);
        if (seems_utf8($capabilityName)) {
            if (function_exists('mb_strtolower')) {
                $capabilityName = mb_strtolower($capabilityName, 'UTF-8');
            }
            $capabilityName = utf8_uri_encode($capabilityName, 200);
        }
        $capabilityName = strtolower($capabilityName);
        $capabilityName = preg_replace('/&.+?;/', '', $capabilityName);
        // kill entities
        $capabilityName = preg_replace('/[^%a-z0-9 _-]/', '', $capabilityName);
        $capabilityName = preg_replace('/\\s+/', '_', $capabilityName);
        $capabilityName = preg_replace('|-+|', '_', $capabilityName);
        $capabilityName = trim($capabilityName, '_');
        return $capabilityName;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Updates the properties of a write panel
  *
  * @param integer $customWritePanelId panel id
  * @param string $name write panel name
  * @param string $description write panel description
  * @param array $standardFields a list of standard fields ids that are to be displayed in
  *                                                      in the panel. Use $STANDARD_FIELDS defined in MF_Constant.php
  * @param array $categories array of category ids that are checked by default when the user
  *                                                      opens Write tab for that panel.
  * @param integer $display_order the order of the panel in Magic Fields > Write Panels tab
  * @param string $type 'post' or 'page'
  */
 public static function Update($customWritePanelId, $name, $description = '', $standardFields = array(), $categories = array(), $display_order = 1, $type = FALSE, $createDefaultGroup = true, $single_post = 0, $default_theme_page = NULL, $default_parent_page = NULL, $expanded = 0)
 {
     include_once 'RC_Format.php';
     global $wpdb;
     $capabilityName = RCCWP_CustomWritePanel::GetCapabilityName($name);
     $name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
     $description = htmlspecialchars($description, ENT_QUOTES, 'UTF-8');
     $sql = $wpdb->prepare("UPDATE " . MF_TABLE_PANELS . " SET name = %s" . " , description = %s" . " , display_order = %d" . " , capability_name = %s" . " , type = %s" . " , single = %s" . " , expanded = %d" . " where id = %d", array($name, $description, $display_order, $capabilityName, $_POST['radPostPage'], $single_post, $expanded, $customWritePanelId));
     $wpdb->query($sql);
     if (!isset($categories) || empty($categories)) {
         $wpdb->delete(MF_TABLE_PANEL_CATEGORY, array('panel_id' => $customWritePanelId), array('%d'));
     } else {
         $wpdb->delete(MF_TABLE_PANEL_CATEGORY, array('panel_id' => $customWritePanelId), array('%d'));
         foreach ($categories as $cat_id) {
             $wpdb->insert(MF_TABLE_PANEL_CATEGORY, array('panel_id' => $customWritePanelId, 'cat_id' => $cat_id), array('%d', '%s'));
         }
     }
     if (!isset($standardFields) || empty($standardFields)) {
         $wpdb->delete(MF_TABLE_PANEL_STANDARD_FIELD, array('panel_id' => $customWritePanelId), array('%d'));
     } else {
         $currentStandardFieldIds = array();
         $currentStandardFieldIds = RCCWP_CustomWritePanel::GetStandardFields($customWritePanelId);
         Debug::log("currentStandardFieldIds");
         Debug::log($currentStandardFieldIds);
         $keepStandardFieldIds = array_intersect($currentStandardFieldIds, $standardFields);
         $deleteStandardFieldIds = array_diff($currentStandardFieldIds, $keepStandardFieldIds);
         $insertStandardFieldIds = array_diff($standardFields, $keepStandardFieldIds);
         Debug::log("standardFields");
         Debug::log($standardFields);
         Debug::log("keepStandardFieldIds");
         Debug::log($keepStandardFieldIds);
         Debug::log("deleteStandardFieldIds");
         Debug::log($deleteStandardFieldIds);
         Debug::log("insertStandardFieldIds");
         Debug::log($insertStandardFieldIds);
         foreach ($insertStandardFieldIds as $standard_field_id) {
             $wpdb->insert(MF_TABLE_PANEL_STANDARD_FIELD, array('panel_id' => $customWritePanelId, 'standard_field_id' => $standard_field_id), array('%d', '%d'));
         }
         if (!empty($deleteStandardFieldIds)) {
             $ids = implode(',', $deleteStandardFieldIds);
             $sql = $wpdb->prepare("DELETE FROM " . MF_TABLE_PANEL_STANDARD_FIELD . " WHERE panel_id = %d" . " AND standard_field_id IN ({$ids})", array($customWritePanelId));
             $wpdb->query($sql);
         }
     }
     if ($default_theme_page) {
         $theme_key = "t_" . $name;
         //check if exist template in postmeta
         $check_template = $wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE meta_key = %s", array($theme_key));
         $query_template = $wpdb->query($check_template);
         if ($query_template) {
             $sql = $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE meta_key = %s AND post_id = %s ", array($default_theme_page, $theme_key, "0"));
         } else {
             $sql = $wpdb->prepare("INSERT INTO {$wpdb->postmeta} (meta_key, meta_value) VALUES (%s, %s)", array($theme_key, $default_theme_page));
         }
         $wpdb->query($sql);
     }
     if ($default_parent_page && $default_parent_page >= 0) {
         $parent_key = "p_" . $name;
         //check if exist parent in postmeta
         $check_parent = $wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE meta_key = %s", array($parent_key));
         $query_parent = $wpdb->query($check_parent);
         if ($query_parent) {
             $sql = $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE meta_key = %s AND post_id = %s", array($default_parent_page, $parent_key, "0"));
         } else {
             $sql = $wpdb->prepare("INSERT INTO {$wpdb->postmeta} (meta_key, meta_value) VALUES (%s, %s)", array($parent_key, $default_parent_page));
         }
         $wpdb->query($sql);
     } elseif ($default_parent_page == -1) {
         delete_post_meta(0, "p_" . $name);
     }
 }
All Usage Examples Of RCCWP_CustomWritePanel::GetCapabilityName