PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/links/class-link-storage.php

https://bitbucket.org/carloskikea/helpet
PHP | 152 lines | 77 code | 19 blank | 56 comment | 4 complexity | c5505f2ef85bb01f8eafb3ea8be08f01 MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO\Admin\Links
  6 */
  7
  8/**
  9 * Represents the storage of an seo link.
 10 */
 11class WPSEO_Link_Storage implements WPSEO_Installable {
 12
 13	const TABLE_NAME = 'yoast_seo_links';
 14
 15	/** @var WPSEO_Database_Proxy */
 16	protected $database_proxy;
 17
 18	/** @var null|string */
 19	protected $table_prefix;
 20
 21	/**
 22	 * Sets the table prefix.
 23	 *
 24	 * @param string $table_prefix Optional. The prefix to use for the table.
 25	 */
 26	public function __construct( $table_prefix = null ) {
 27		if ( null === $table_prefix ) {
 28			$table_prefix = $GLOBALS['wpdb']->get_blog_prefix();
 29		}
 30
 31		$this->table_prefix   = $table_prefix;
 32		$this->database_proxy = new WPSEO_Database_Proxy( $GLOBALS['wpdb'], $this->get_table_name(), true );
 33	}
 34
 35	/**
 36	 * Returns the table name to use.
 37	 *
 38	 * @return string The table name.
 39	 */
 40	public function get_table_name() {
 41		return $this->table_prefix . self::TABLE_NAME;
 42	}
 43
 44	/**
 45	 * Creates the database table.
 46	 *
 47	 * @return boolean True if the table was created, false if something went wrong.
 48	 */
 49	public function install() {
 50		return $this->database_proxy->create_table(
 51			array(
 52				'id bigint(20) unsigned NOT NULL AUTO_INCREMENT',
 53				'url varchar(255) NOT NULL',
 54				'post_id bigint(20) unsigned NOT NULL',
 55				'target_post_id bigint(20) unsigned NOT NULL',
 56				'type VARCHAR(8) NOT NULL',
 57			),
 58			array(
 59				'PRIMARY KEY (id)',
 60				'KEY link_direction (post_id, type)',
 61			)
 62		);
 63	}
 64
 65	/**
 66	 * Returns an array of links from the database.
 67	 *
 68	 * @param int $post_id The post to get the links for.
 69	 *
 70	 * @return WPSEO_Link[] The links connected to the post.
 71	 */
 72	public function get_links( $post_id ) {
 73		global $wpdb;
 74
 75		$results = $this->database_proxy->get_results(
 76			$wpdb->prepare( '
 77				SELECT url, post_id, target_post_id, type
 78				FROM ' . $this->get_table_name() . '
 79				WHERE post_id = %d',
 80				$post_id
 81			)
 82		);
 83
 84		if ( $this->database_proxy->has_error() ) {
 85			WPSEO_Link_Table_Accessible::set_inaccessible();
 86		}
 87
 88		$links = array();
 89		foreach ( $results as $link ) {
 90			$links[] = WPSEO_Link_Factory::get_link( $link->url, $link->target_post_id, $link->type );
 91		}
 92
 93		return $links;
 94	}
 95
 96	/**
 97	 * Walks the given links to save them.
 98	 *
 99	 * @param integer      $post_id The post id to save.
100	 * @param WPSEO_Link[] $links   The link to save.
101	 *
102	 * @return void
103	 */
104	public function save_links( $post_id, array $links ) {
105		array_walk( $links, array( $this, 'save_link' ), $post_id );
106	}
107
108	/**
109	 * Removes all records for given post_id.
110	 *
111	 * @param int $post_id The post_id to remove the records for.
112	 *
113	 * @return int|false The number of rows updated, or false on error.
114	 */
115	public function cleanup( $post_id ) {
116		$is_deleted = $this->database_proxy->delete(
117			array( 'post_id' => $post_id ),
118			array( '%d' )
119		);
120
121		if ( $is_deleted === false ) {
122			WPSEO_Link_Table_Accessible::set_inaccessible();
123		}
124
125		return $is_deleted;
126	}
127
128	/**
129	 * Inserts the link into the database.
130	 *
131	 * @param WPSEO_Link $link     The link to save.
132	 * @param int        $link_key The link key. Unused.
133	 * @param int        $post_id  The post id to save the link for.
134	 *
135	 * @return void
136	 */
137	protected function save_link( WPSEO_Link $link, $link_key, $post_id ) {
138		$inserted = $this->database_proxy->insert(
139			array(
140				'url'            => $link->get_url(),
141				'post_id'        => $post_id,
142				'target_post_id' => $link->get_target_post_id(),
143				'type'           => $link->get_type(),
144			),
145			array( '%s', '%d', '%d', '%s' )
146		);
147
148		if ( $inserted === false ) {
149			WPSEO_Link_Table_Accessible::set_inaccessible();
150		}
151	}
152}