本文整理汇总了PHP中wpdb::prepare方法的典型用法代码示例。如果您正苦于以下问题:PHP wpdb::prepare方法的具体用法?PHP wpdb::prepare怎么用?PHP wpdb::prepare使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wpdb
的用法示例。
在下文中一共展示了wpdb::prepare方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: updateQuery
/**
*
* Given the sql array and the search manager, this method will update the query
*
* @param AbstractSearch $searchManager
* @param array $sql
* @param \wpdb $databaseAdapter
* @return array
* @author Tim Perry
*/
public function updateQuery(AbstractSearch $searchManager, array $sql, \wpdb $databaseAdapter)
{
if (!$searchManager->queryVarExists(self::QUERY_VAR_KEYWORDS)) {
return $sql;
}
$keywords = $searchManager->getQueryVar(self::QUERY_VAR_KEYWORDS);
if (empty($keywords)) {
return $sql;
}
if ($keywordsArray = explode(" ", $keywords)) {
$sql["select"] .= ", ";
$sql["where"] .= " and ( ";
foreach ($keywordsArray as $keyword) {
$sql["select"] .= $databaseAdapter->prepare("case when p.post_title like '%%%s%%' then 5 else 0 end + ", $keyword);
$sql["select"] .= $databaseAdapter->prepare("case when p.post_content like '%%%s%%' then 1 else 0 end + ", $keyword);
$sql["where"] .= $databaseAdapter->prepare("p.post_title like '%%%s%%' or ", $keyword);
$sql["where"] .= $databaseAdapter->prepare("p.post_content like '%%%s%%' or ", $keyword);
}
$sql["where"] = rtrim($sql["where"], "or ");
$sql["where"] .= ") ";
$sql["select"] = rtrim($sql["select"], "+ ");
$sql["select"] .= " as matches";
$sql["orderby"] = "order by matches desc, post_date desc";
}
return $sql;
}
示例2: launchkey_cron
public function launchkey_cron()
{
$table_name = $this->wpdb->prefix . 'launchkey_sso_sessions';
$dt = new DateTime("- 1 hour");
$dt->setTimezone(new DateTimeZone("UTC"));
$this->wpdb->query($this->wpdb->prepare("DELETE FROM {$table_name} WHERE seen < %s", $dt->format("Y-m-d H:i:s")));
}
示例3: getResults
public function getResults($query, $parameters = array())
{
if (!empty($parameters)) {
$query = str_replace('?', '%s', $query);
$query = $this->wpdb->prepare($query, $parameters);
}
return $this->wpdb->get_results($query, ARRAY_A);
}
示例4: query
public function query($query, $parameters = array())
{
if (!empty($parameters)) {
$query = str_replace('?', '%s', $query);
$query = $this->wpdb->prepare($query, $parameters);
}
return $this->wpdb->query($query);
}
示例5: delete_terms
/**
* Deletes all plugin terms.
*
* @return void
*/
private function delete_terms()
{
$query = "\nSELECT term_id\nFROM {$this->wpdb->term_taxonomy}\nWHERE taxonomy = %s\nLIMIT 500";
$query = $this->wpdb->prepare($query, $this->taxonomy);
while ($term_ids = $this->wpdb->get_col($query)) {
foreach ($term_ids as $term_id) {
wp_delete_term($term_id, $this->taxonomy);
}
}
}
示例6: delete_items_for_deleted_site
/**
* Deletes all remote MultilingualPress nav menu items linking to the (to-be-deleted) site with the given ID.
*
* @param int $deleted_site_id The ID of the to-be-deleted site.
*
* @return void
*/
public function delete_items_for_deleted_site($deleted_site_id)
{
$query = "\nSELECT blog_id\nFROM {$this->wpdb->blogs}\nWHERE blog_id != %d";
$query = $this->wpdb->prepare($query, $deleted_site_id);
foreach ($this->wpdb->get_col($query) as $site_id) {
switch_to_blog($site_id);
$query = "\nSELECT p.ID\nFROM {$this->wpdb->posts} p\nINNER JOIN {$this->wpdb->postmeta} pm\nON p.ID = pm.post_id\nWHERE pm.meta_key = %s\n\tAND pm.meta_value = %s";
$query = $this->wpdb->prepare($query, $this->meta_key, $deleted_site_id);
foreach ($this->wpdb->get_col($query) as $post_id) {
wp_delete_post($post_id, true);
}
restore_current_blog();
}
}
示例7:
function global_site_search_page_setup()
{
$page_id = get_option('global_site_search_page', false);
if (empty($page_id) || !is_object(get_post($page_id)) && is_super_admin()) {
// a page hasn't been set - so check if there is already one with the base name
$page_id = $this->db->get_var($this->db->prepare("SELECT ID FROM {$this->db->posts} WHERE post_name = %s AND post_type = 'page'", $this->global_site_search_base));
if (empty($page_id)) {
// Doesn't exist so create the page
$page_id = wp_insert_post(array("post_content" => '', "post_title" => __('Site Search', 'globalsitesearch'), "post_excerpt" => '', "post_status" => 'publish', "comment_status" => 'closed', "ping_status" => 'closed', "post_name" => $this->global_site_search_base, "post_type" => 'page'));
flush_rewrite_rules();
}
update_option('global_site_search_page', $page_id);
}
}
示例8: retrieve
/**
* @param string $query
* @param array $args
* @param int $elements_num
*
* @return array
*/
public function retrieve($query, $args, $elements_num)
{
$result = array();
$offset = 0;
while ($offset < $elements_num) {
$new_query = $query . sprintf(' LIMIT %d OFFSET %s', $this->chunk_size, $offset);
$new_query = $this->wpdb->prepare($new_query, $args);
$rowset = $this->wpdb->get_results($new_query, ARRAY_A);
if (is_array($rowset) && count($rowset)) {
$result = array_merge($result, $rowset);
}
$offset += $this->chunk_size;
}
return $result;
}
示例9: get_translation_id
/**
* @param string $cms_id
* @param bool|TranslationProxy_Service $translation_service
*
* @return int|null translation id for the given cms_id's target
*/
public function get_translation_id($cms_id, $translation_service = false)
{
list($post_type, $element_id, , $target_lang) = $this->parse_cms_id($cms_id);
$translation = $this->wpdb->get_row($this->wpdb->prepare("\n\t\t\t\t\t\t\t\t\t\t\t\t\tSELECT t.translation_id, j.job_id, t.element_id\n\t\t\t\t\t\t\t\t\t\t\t\t\tFROM {$this->wpdb->prefix}icl_translations t\n\t\t\t\t\t\t\t\t\t\t\t\t\tJOIN {$this->wpdb->prefix}icl_translations o\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tON o.trid = t.trid\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND o.element_type = t.element_type\n\t\t\t\t\t\t\t\t\t\t\t\t\tLEFT JOIN {$this->wpdb->prefix}icl_translation_status st\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tON st.translation_id = t.translation_id\n\t\t\t\t\t\t\t\t\t\t\t\t\tLEFT JOIN {$this->wpdb->prefix}icl_translate_job j\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tON j.rid = st.rid\n\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE o.element_id=%d\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND t.language_code=%s\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND o.element_type LIKE %s\n\t\t\t\t\t\t\t\t\t\t\t\t\tLIMIT 1", $element_id, $target_lang, '%_' . $post_type));
$translation_id = $this->maybe_cleanup_broken_row($translation, $translation_service);
if ($translation_service && !isset($translation_id) && $translation_service) {
$job_id = $this->job_factory->create_local_post_job($element_id, $target_lang);
$job = $this->job_factory->get_translation_job($job_id, false, false, true);
$translation_id = $job ? $job->get_translation_id() : 0;
if ($translation_id) {
$this->tm_records->icl_translation_status_by_translation_id($translation_id)->update(array('status' => ICL_TM_IN_PROGRESS, 'translation_service' => $translation_service->id));
}
}
return $translation_id;
}
示例10: persist
private function persist()
{
foreach (array_chunk($this->data, self::INSERT_CHUNK_SIZE) as $chunk) {
$query = "INSERT IGNORE INTO {$this->wpdb->prefix}icl_strings " . '(`language`, `context`, `gettext_context`, `domain_name_context_md5`, `name`, `value`, `status`) VALUES ';
$i = 0;
foreach ($chunk as $string) {
if ($i > 0) {
$query .= ',';
}
$query .= $this->wpdb->prepare("('%s', '%s', '%s', '%s', '%s', '%s', %d)", $this->get_source_lang($string['name'], $string['domain']), $string['domain'], $string['gettext_context'], md5($string['domain'] . $string['name'] . $string['gettext_context']), $string['name'], $string['value'], ICL_TM_NOT_TRANSLATED);
$i++;
}
$this->wpdb->query($query);
}
}
示例11: getTopFailedLogins
/**
* @param int $limit
* @return mixed
*/
public function getTopFailedLogins($limit = 10)
{
$interval = 'UNIX_TIMESTAMP(DATE_SUB(NOW(), interval 7 day))';
switch (wfConfig::get('email_summary_interval', 'weekly')) {
case 'daily':
$interval = 'UNIX_TIMESTAMP(DATE_SUB(NOW(), interval 1 day))';
break;
case 'monthly':
$interval = 'UNIX_TIMESTAMP(DATE_SUB(NOW(), interval 1 month))';
break;
}
$failedLogins = $this->db->get_results($this->db->prepare(<<<SQL
SELECT wfl.*,
sum(wfl.fail) as fail_count
FROM {$this->db->base_prefix}wfLogins wfl
WHERE wfl.fail = 1
AND wfl.ctime > {$interval}
GROUP BY wfl.username
ORDER BY fail_count DESC
LIMIT %d
SQL
, $limit));
foreach ($failedLogins as &$login) {
$exists = $this->db->get_var($this->db->prepare(<<<SQL
SELECT !ISNULL(ID) FROM {$this->db->base_prefix}users WHERE user_login = '%s' OR user_email = '%s'
SQL
, $login->username, $login->username));
$login->is_valid_user = $exists;
}
return $failedLogins;
}
示例12: pingback_extensions_getPingbacks
/**
* Retrieve array of URLs that pingbacked the given URL.
*
* Specs on http://www.aquarionics.com/misc/archives/blogite/0198.html
*
* @since 1.5.0
*
* @param string $url
* @return array|IXR_Error
*/
public function pingback_extensions_getPingbacks($url)
{
/** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
do_action('xmlrpc_call', 'pingback.extensions.getPingbacks');
$url = $this->escape($url);
$post_ID = url_to_postid($url);
if (!$post_ID) {
// We aren't sure that the resource is available and/or pingback enabled
return $this->pingback_error(33, __('The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.'));
}
$actual_post = get_post($post_ID, ARRAY_A);
if (!$actual_post) {
// No such post = resource not found
return $this->pingback_error(32, __('The specified target URL does not exist.'));
}
$comments = $this->db->get_results($this->db->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM {$this->db->comments} WHERE comment_post_ID = %d", $post_ID));
if (!$comments) {
return array();
}
$pingbacks = array();
foreach ($comments as $comment) {
if ('pingback' == $comment->comment_type) {
$pingbacks[] = $comment->comment_author_url;
}
}
return $pingbacks;
}
示例13: get_existing_translation_ids
/**
* @param $source_site_id
* @param $target_site_id
* @param $source_content_id
* @param $target_content_id
* @param $type
* @return mixed
*/
private function get_existing_translation_ids($source_site_id, $target_site_id, $source_content_id, $target_content_id, $type)
{
$sql = "\n\t\t\tSELECT DISTINCT `ml_source_blogid`, `ml_source_elementid`\n\t\t\tFROM {$this->link_table}\n\t\t\tWHERE (\n\t\t\t\t ( `ml_blogid` = %d AND `ml_elementid` = %d )\n\t\t\t\tOR ( `ml_blogid` = %d AND `ml_elementid` = %d )\n\t\t\t\t)\n\t\t\t\tAND `ml_type` = %s";
$query = $this->wpdb->prepare($sql, $source_site_id, $source_content_id, $target_site_id, $target_content_id, $type);
$result = $this->wpdb->get_results($query, ARRAY_A);
return $result;
}
示例14: select_translation_id
private function select_translation_id($where, $prepare_args)
{
$this->translation_id = $this->wpdb->get_var("SELECT translation_id FROM {$this->wpdb->prefix}{$this->table}\n\t\t\t WHERE" . $this->wpdb->prepare($where, $prepare_args) . " LIMIT 1");
if (!$this->translation_id) {
throw new InvalidArgumentException('No translation entry found for query: ' . serialize($where) . serialize($prepare_args));
}
}
示例15: save
/**
* Save entity to database.
*
* @return int|false
*/
public function save()
{
// Prepare query data.
$set = array();
$values = array();
foreach ($this->values as $field => $value) {
if ($field == 'id') {
continue;
}
if ($value === null) {
$set[] = sprintf('`%s` = NULL', $field);
} else {
$set[] = sprintf('`%s` = %s', $field, $this->formats[$field]);
$values[] = $value;
}
}
// Run query.
if ($this->values['id']) {
$res = $this->wpdb->query($this->wpdb->prepare(sprintf('UPDATE `%s` SET %s WHERE `id` = %d', $this->table_name, implode(', ', $set), $this->values['id']), $values));
} else {
$res = $this->wpdb->query($this->wpdb->prepare(sprintf('INSERT INTO `%s` SET %s', $this->table_name, implode(', ', $set)), $values));
if ($res) {
$this->values['id'] = $this->wpdb->insert_id;
}
}
return $res;
}